简体   繁体   English

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

[英]Magento get last product added to cart

i need to get the last product added to cart, with all informations: image, name, sku and custom options if product is configurable. 我需要将最后的产品添加到购物车,并提供所有信息:图像,名称,sku和自定义选项(如果产品是可配置的)。 I try with this code but is not working. 我尝试使用此代码,但无法正常工作。 My Magento version is 1.9.1 我的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();
    }

Thank you. 谢谢。

The problem with Mage::getSingleton('checkout/session')->getLastAddedProductId(true); Mage::getSingleton('checkout/session')->getLastAddedProductId(true); that the true will unset the data. true将取消数据。

This: 这个:

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

should also work. 应该也可以。

        $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 这有效。.感谢Marius

Reference - MAGENTO - Load last created Product to Cart 参考-MAGENTO-将最后创建的产品加载到购物车

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM