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

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

PHP:对数组排序 - php

请如何排序以下数组Array ( 'ben' => 1.0, 'ken' => 2.0, 'sam' => 1.5 ) 至Array ( 'ken' => 2.0, 'sam' => 1.5, 'ben' =&…

php Singleton类实例将在多个会话中保留吗? - php

举一个简单的例子,如果我想计算一个不使用磁盘存储的脚本的命中次数,我可以使用静态类成员来执行此操作吗?用户1:<?php $test = Example::singleton(); $test->visits++; ?> 用户2:<?php $test = Example::singleton(); $test->visits+…

PHP-复选框组 - php

我有一个需要发布的表单复选框组。<input type="checkbox" value="true" checked name="chk0[]"> <input type="checkbox" value="false" name=…

转义字符无法正常工作 - php

我试图在PHP中创建html内容,并且为onclick事件提供了一个名为uchat的div函数。该函数采用一个字符串的名称参数。如下所示:$name = "Php string"; $echostr .= "<div onClick='uchat(\'$name\')'> &l…

PHP PDO组按列名称查询结果 - php

以下PDO查询返回以下结果:$db = new PDO('....'); $sth = $db->prepare('SELECT ...'); 结果如下: name curso ABC stack CDE stack FGH stack IJK stack LMN overflow OPQ overflow RS…