Php Magento将最后一个产品添加到购物车
我需要得到最后一个产品添加到购物车,所有信息:图像,名称,sku和自定义选项,如果产品是可配置的。我尝试使用此代码,但不起作用。我的Magento版本是1.9.1Php Magento将最后一个产品添加到购物车,php,jquery,ajax,magento,Php,Jquery,Ajax,Magento,我需要得到最后一个产品添加到购物车,所有信息:图像,名称,sku和自定义选项,如果产品是可配置的。我尝试使用此代码,但不起作用。我的Magento版本是1.9.1 $xitems = Mage::getSingleton('checkout/session')->getQuote()->getAllItems(); $max = 0; $lastItem = null; foreach ($xitems as $xitem){
$xitems = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$max = 0;
$lastItem = null;
foreach ($xitems as $xitem){
if ($xitem->getId() > $max) {
$max = $xitem->getId();
$lastItem = $xitem;
}
}
if ($lastItem){
$_xproduct = $lastItem->getProduct();
$xproductsku = $_xproduct->getSku();
$xproductname = $_xproduct->getName();
$xproductqty = $_xproduct->getQty();
$xproductprice = $_xproduct->getPrice();
$xproducturl = $_xproduct->getUrl();
}
$xhtml .='<a href="'.$lastItem->getUrl().'" title="'.$lastItem->getName().'" class="product-image"><img src="'. Mage::helper('catalog/image')->init($lastItem->getProduct(), 'thumbnail').'" width="50" height="50" alt="" /></a>';
$xhtml .='<a class="item-name" href="'.$lastItem->getUrl().'">'.$lastItem->getName().'</a> <span class="item-price"><span class="price">$'.$lastItem->getPrice().'</span></span>';
$_response = Mage::getModel('ajaxminicart/response')
->setMessage($xhtml);
//append updated blocks
$_response->addUpdatedBlocks($_response);
$_response->send();
}
$xitems=Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$max=0;
$lastItem=null;
foreach($xitems作为$xitem){
如果($xitem->getId()>$max){
$max=$xitem->getId();
$lastItem=$xitem;
}
}
如果($lastItem){
$\u xproduct=$lastItem->getProduct();
$xproductsku=$\u xproduct->getSku();
$xproductname=$\u xproduct->getName();
$xproductqty=$\u xproduct->getQty();
$xproductprice=$\u xproduct->getPrice();
$xproducturl=$\u xproduct->getUrl();
}
$xhtml.='';
$xhtml.='$'.$lastItem->getPrice()';
$\u response=Mage::getModel('ajaxminicart/response')
->setMessage($xhtml);
//附加更新的块
$\u响应->添加更新块($\u响应);
$\响应->发送();
}
多谢各位
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$max = 0;
$lastItem = null;
foreach ($items as $item){
if ($item->getId() > $max) {
$max = $item->getId();
$lastItem = $item;
}
}
if ($lastItem){
$_product = $lastItem->getProduct();
$xproductsku = $_product->getSku();
$xproductname = $_product->getName();
$xproductqty = $_product->getQty();
$xproductprice = $_product->getPrice();
$xproducturl = $_product->getUrl();
}
这很有效。。
感谢马吕斯
参考-
这很有效。。
感谢马吕斯
参考-Mage::getSingleton('checkout/session')->getLastAddedProductId(true)的问题确认
true
将取消设置数据
这:
Mage::getSingleton('checkout/session')
->getQuote()
->getItemsCollection()
->getLastItem()
->getProduct();
也应该有效。Mage::getSingleton('checkout/session')->getLastAddedProductId(true)的问题确认
true
将取消设置数据
这:
Mage::getSingleton('checkout/session')
->getQuote()
->getItemsCollection()
->getLastItem()
->getProduct();
也应该有效。我能知道目的吗?您可以通过创建购物车对象并获取购物车项目来获得它。但这取决于会话。嗨,Grish,我需要的是获取ajax弹出窗口的所有信息,因此当您在购物车中添加产品时,此弹出窗口将显示您在购物车中添加的产品,而不是默认文本,您在购物车中添加了%s,我想显示整个产品信息。我可以知道目的吗?您可以通过创建购物车对象并获取购物车项目来获得它。但这取决于会话。嗨,Grish,我需要的是获取ajax弹出窗口的所有信息,因此当您在购物车中添加产品时,此弹出窗口将显示您在购物车中添加的产品,而不是默认文本,您在购物车中添加了%s,我想显示整个产品信息,与我在购物车中看到的内容相同,在$xhtml中,这个获取购物车中的所有产品不是最后一个,所以我只需要获取最后一个产品所有详细信息:基本图像、名称、价格、url、颜色、大小。。。对于简单或可配置产品抱歉,configurable中所有相关简单产品的代码显示图像对于可配置产品的图像是一个问题,您知道它是什么吗?当然,您到底想要什么?让我试试我的本地产品。和我的一样,在$xhtml中,这个获取购物车中的所有产品不是最后一个,所以我只需要获取最后一个产品所有详细信息:基本图像、名称、价格、url、颜色、大小。。。对于简单或可配置产品抱歉,configurable中所有相关简单产品的代码显示图像对于可配置产品的图像是一个问题,您知道它是什么吗?当然,您到底想要什么?让我在本地试试。与公认的答案相比,这看起来是一个更好的实现。同意上面的评论。本应接受此答案,但请记住,如果之前已将产品添加到购物车中(例如,数量从1增加到2),则此答案不起作用。假设我在购物车中有产品A和B,B是最后添加的。然后我向购物车中添加另一个产品单元A,这段代码将返回最后添加的产品B。与接受的回答相比,这看起来是一个更好的实现。同意上面的评论。本应接受此答案,但请记住,如果之前已将产品添加到购物车中(例如,数量从1增加到2),则此答案不起作用。假设我在购物车中有产品A和B,B是最后添加的。然后我将产品A的另一个单元添加到购物车中,此代码将返回最后添加的产品B。