Get Cart Quantity in Magento

Share

You can get the number of items currently in the cart using the following code.

$cart = Mage::getModel('checkout/cart')->getQuote()->getData();
echo (int)$cart['items_qty'];

However,if the cart is empty, writing only the above code will show error (Because $cart[‘items_qty’] is only set when there are items on the cart). For that you need to check if the value is set or not. Hence the total code will be like –

$cart = Mage::getModel('checkout/cart')->getQuote()->getData();
if(isset($cart['items_qty'])){
echo (int)$cart['items_qty'];
} else {
echo '0';
}
This entry was posted in Magento, PHP and tagged , . Bookmark the permalink.
 

12 Responses to Get Cart Quantity in Magento

  1. eXe says:

    thanks a lot – I was looking for this everywhere!

  2. eXe says:

    PS – it throws an error when the cart is empty (because 'items'qty' is not set then), therefore I encapsulated it within a function:

    function getCartQuantity () {
    $cart = Mage::getModel('checkout/cart')->getQuote()->getData();
    if (isset($cart['items_qty']))
    return (int)$cart['items_qty'];
    else
    return 0;
    }

    Echo like this:
    echo getCartQuantity ();

  3. Sumit Surai says:

    Thanks eXe for pointing out the blank cart error and nice to know this post helped you. I have updated the post.

  4. Meynard says:

    you can use also this to get the quantity

    echo Mage::getModel('checkout/cart')->getItemsQty();

  5. tmacedo says:

    Very Good,
    But what I really need is the value (in $$$) of these items, someone could help me ??
    Thanks,

  6. Brett says:

    I had bugs after the cart uncookied my items. The solution turned out to be using this if test:

    if (isset($cart['items_qty']) && $cart['items_qty'] > 0) {

    The "isset" will check to see if the value is null / empty, which is what throws the error.

  7. jess says:

    Awesome! Thanks so much!

  8. Tomás Aliaga says:

    tmacedo-> If you need the total amount try this:
    echo Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal());
    Then you can format that number as you like

  9. von Wellean says:

    Has anyone experienced any problems when using Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() to return items in the cart. I have a problem whereby if a customer leaves items in the cart and logs out and I then change that item in the backend the customer cannot log back in until I reset their account in admin. The problem seems to lie with some custom tier pricing code that uses Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() to pull items out of the cart. Any suggestions or ideas as to what might be happening would be cool!

  10. Mališa says:

    Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal()); – will return you totla card+tax+shipping

    if you need each value of price you can read from:

    Mage::getModel('checkout/cart')->getQuote()->getData(); — will return you array of each price

  11. Ivo Barbosa says:

    Malisa how return only total cart whith tax but no shipping?
    Thanks

  12. ranga says:

    Thank a lot, this did help me a lot.

Leave a Reply

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

*