Magento将最新产品添加到购物车

Rob*_*ert 1 php ajax jquery magento

我需要将最后的产品添加到购物车,并提供所有信息:图像,名称,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();
    }
Run Code Online (Sandbox Code Playgroud)

谢谢。

小智 5

        $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();
        }
Run Code Online (Sandbox Code Playgroud)

这有效..感谢马吕斯

参考 - MAGENTO - 将最后创建的产品加载到购物车


smi*_*gle 5

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

这个:

Mage::getSingleton('checkout/session')
   ->getQuote()
   ->getItemsCollection()
   ->getLastItem()
   ->getProduct();
Run Code Online (Sandbox Code Playgroud)

应该也可以。