0

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

8
  • do you want qty on cart page i.e in phtml ?
    – Pawan
    Commented Sep 12, 2018 at 2:53
  • Usually magento doesn't add same product twice, like in your screenshot. By default Magento will always increase the qty on "add to cart", if product is already in cart. Commented Sep 12, 2018 at 2:55
  • @AdarshKhatri My product is buy 1 get 1 free, so it will be add twice
    – Thanh TN
    Commented Sep 12, 2018 at 2:58
  • @Pawan hmm, yes...
    – Thanh TN
    Commented Sep 12, 2018 at 3:01
  • 1
    please try: <?php $cart = Mage::helper('checkout/cart')->getCart()->getQuote(); foreach ($cart->getAllItems() as $item) { echo $qty = $this->escapeHtml($item->getQty()); } ?>
    – Pawan
    Commented Sep 12, 2018 at 3:04

2 Answers 2

2

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();
0
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.