问题
i need to get the last product added to cart, with all informations: image, name, sku and custom options if product is configurable. I try with this code but is not working. My Magento version is 1.9.1
$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();
}
Thank you.
回答1:
$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();
}
This works.. Say thanks to Marius
Reference - MAGENTO - Load last created Product to Cart
回答2:
The problem with Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
that the true
will unset the data.
This:
Mage::getSingleton('checkout/session')
->getQuote()
->getItemsCollection()
->getLastItem()
->getProduct();
should also work.
来源:https://stackoverflow.com/questions/28495068/magento-get-last-product-added-to-cart