質問

After added products to cart, we can get all qty in cart. But I want to get qty of each product, how to do? Thanks.

enter image description here

This is my cart I added product. I want to get qty of each product, example: product 1( item 1 and 2) qty = 4

役に立ちましたか?

解決 2

In this case, I use this code:

        $sku = find_your_product_sku;
        $sum =0;
        $quote = Mage::helper('checkout/cart')->getCart()->getQuote();

        foreach ($quote->getAllItems() as $item) {
            $product = array (
                'id' => $item->getSku(),
                'quantity' => $item->getQty()
            );
            if ($product['id']==$sku) {
                $sum += $product['quantity'];
            }
        }
  

Thanks Rohan and Pawan for helping me.

他のヒント

You can get product quantity like this :

$quote = Mage::helper('checkout/cart')->getCart()->getQuote();
$product = [];

foreach ($quote->getAllItems() as $item) {
        $product[]= array (
                'id' => $item->getSku(),
                'quantity' => $item->getQty()
        );
}

$product_json = json_encode($product);

For get qty of specific product :

$quote = Mage::getSingleton('checkout/session')->getQuote();
$product = Mage::getModel('catalog/product')->load($product_id);
$_item = $quote->getItemByProduct($product);
$qty = $_item->getQty();
ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top