in ,

Return total number of items in Cart

Show Products in Cart

Sometimes you run into tiny idiosyncracies in Magento templates. It makes you question many things. Such as: why would a theme developer choose to only show the total amount of different types of products in the minicart.phtml?

Today I’m showing you different ways to return the total amount of items in your customer’s shopping cart for usage anywhere within your store’s template.

Example: you have a store that sells fruit and bicycles. I suppose we’re serving a niche there, which doesn’t make any sense. But it doesn’t make any sense either to show your customer this receipt after purchase:

Purchases:
- 2 types of fruit
- 3 types of bicycles
Total: 2 different types of products

Besides showing a total price. A total of 5 items would make much more sense than showing the total amount of different producttypes, wouldn’t it?

Appearantly some theme developers disagree, because they tend to use the following Magento function in their mini-cart:

$_cartQty = $this->helper('checkout/cart')->getItemsCount(); // Shows the different kind of items in cart (NOT TOTAL!)

When the more logical choice would be:

$_cartQty = $this->helper('checkout/cart')->getSummaryCount(); // Shows the total amount of items in cart

This should work in most cases. But if you’re unlucky, like me, this doesn’t work. In that case, try:

$_cartQty = Mage::getModel('checkout/cart')->getQuote()->getItemsQty(); // Alternative method

This is a solid way of displaying the total amount of items in Magento’s shopping cart. But it needs a little tweaking, as it could display the value followed by four decimals, e.g. 3.0000.

You can strip the decimals (everything after the dot) by wrapping it in a round-function:

$_cartQty = round(Mage::getModel('checkout/cart')->getQuote()->getItemsQty(), 0); // You can strip the decimals (everything after the dot) by wrapping it in a round-function

To increase readability you could also do the following:

$_cartQty = Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
$_cartQty = round($_cartQty, 0);

There you have it. A bulletproof way of returning the total amount of items in Magento’s cart for usage anywhere on your site. Including the mini-cart in the top-right of your rwd based theme.

Buy me a beer?

Do you appreciate my work and support? Please consider supporting me by donating, so I can continue to develop and write useful solutions for you.

Choose amount

Personal Message

Thank you! 🙂

Written by Daan van den Bergh

Magento 2 Back-end Developer with a passion for trainlifting, airplane-gliding, hunting trees and creating fake hobbies.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.