获取购物车magento中单个商品的数量

时间:2014-07-14 12:35:45

标签: magento product cart

您好我需要在产品详细信息中查看我在商品中添加的商品数量

atm我正在使用这个

<?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>";
                                            }?>

但它只返回购物车中添加/更新的最后一个数量,我需要特定的单品qty

1 个答案:

答案 0 :(得分:0)

试试这个:

//$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
}