Magento 获取最后添加到购物车的产品

Magento get last product added to cart

我需要获取添加到购物车的最后一个产品,以及所有信息:图片、名称、sku 和自定义选项(如果产品是可配置的)。我尝试使用此代码但无法正常工作。我的 Magento 版本是 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();
    }

谢谢。

        $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();
        }

这有效.. 向 Marius

表示感谢

参考 - MAGENTO - Load last created Product to Cart

Mage::getSingleton('checkout/session')->getLastAddedProductId(true); 的问题是 true 将取消设置数据。

这个:

Mage::getSingleton('checkout/session')
   ->getQuote()
   ->getItemsCollection()
   ->getLastItem()
   ->getProduct();

应该也可以。