获取购物车magento中单个项目的数量

获取购物车magento中单个项目的数量,magento,product,cart,Magento,Product,Cart,您好,我需要获取产品详细信息中查看的商品在购物车中添加的商品数量 我在用这个 <?php $cart = Mage::getModel('checkout/cart')->getQuote(); $result = array(); foreach ($cart->getAllItems() as $

您好,我需要获取产品详细信息中查看的商品在购物车中添加的商品数量

我在用这个

<?php  $cart = Mage::getModel('checkout/cart')->getQuote();
                                            $result = array();

                                            foreach ($cart->getAllItems() as $item) {

                                                $result['qty'] = $item->getQty();

                                            }
                                            $qty=$result['qty'];
                                            //echo $qty ; 

                                            if(!$qty == 0){
                                               echo '<span>'.$qty.'</span>';
                                            }else{
                                                 echo "<span>0</span>";
                                            }?>

但它只返回购物车中添加/更新的最后一个数量,我需要具体的单产品数量,请尝试以下操作:

//$product is current product that you are viewing
$qty = null;
foreach ($cart->getAllItems() as $item) {
    if ($product->getId() == $item->getProductId()) {
        $qty = $item->getQty();
    }
}
if (!is_null($qty)) {
    //$qty is what you are looking for
}
else {
    //the current product is not in the cart
}