Many websites show a “total number of items in your basket”. So, how do you get that information out of Magento if you’re not in the checkout process?
You can simply call the Mage Helper file checkout/cart.
If all you’re after is a total number, all you need is:
1 |
echo Mage::helper('checkout/cart')->getCart()->getItemsCount(); |
This code grabs the helper object, gets the current cart object, and then calls the Magento count function on that.
If you want to do something with the items, then you’ll need to return the items, rather than the counter. To do this, you need to look at the helper checkout/session instead.
Something along the lines of:
1 |
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems(); |
If you wanted to get the total for the basket – so that it could be displayed elsewhere on your site, then you can get this from the checkout/cart class. Note, we don’t get this from the helper.
Simply call:
1 2 3 |
echo Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal(); // format the number - you couldn't probably use number_format, but use Magento's functions echo $this->helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()); |
Image Credit: i k o
Comment or tweet @douglasradburn