Magento 1.9 – How to Get Quantity of Each Product in Cart

cartmagento-1.9qty

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

Best Answer

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();
Related Topic