Magento: get product quantity in cart

In this post, I will show you how you can get product quantity information of the shopping cart. After this post, you will know how to get:

  • Total items in shopping cart
  • Total quantity of all items in shopping cart
  • Total quantity of a product in shopping cart

There are numerous posts about this on the Internet. But if you get here, let's go :)

1. Get Total items in shopping cart: or in the other way, it is the total number of unique items within your shopping cart

$totalItems = Mage::getSingleton('checkout/cart')->getQuote()->getItemsCount();

2. Total quantity of all items in shopping cart:

$totalItems = Mage::getSingleton('checkout/cart')->getQuote()->getItemsQty();

3. Total quantity of a product in shopping cart
I could find some posts about this. They use a foreach loop to count total quantity of a product. But I think the following way is much better. Just try it yourself if you want:

$cartItem = Mage::getSingleton('checkout/cart')->getQuote()->getItemByProduct($_product);
if ($cartItem) {
	return (int) $cartItem->getQty();
}

One thought on “Magento: get product quantity in cart”

1 Response to Magento: get product quantity in cart

  • mrudangi

    i put qty text fileld on homepage but if i put in text box '3' that qty is not add to cart in magento................please reply me as soon as possible

Comments