10

I am trying to check if the shopping cart is empty or not. I am trying to do this from a static block and from a phtml file.

Anyone know how to do this?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Wesley Smits
  • 1,314
  • 6
  • 23
  • 37
  • Well i tried $this->getOrders(); But of course then i would have to extend from History.php. But in the toplinks i want to display 'Shoppingbag' when it's empty. and when it is not empty: 'Shopping bag(3) | Order now'. (if there are three items in the shopping bag.) Well i don't think my code would be of any help here. :P – Wesley Smits Oct 08 '12 at 09:54
  • Go to the Question you have asked and mark your Questions as solved if they are correct. – René Höhle Oct 08 '12 at 09:57
  • Ah thanks for the tip, i found it :) – Wesley Smits Oct 08 '12 at 10:04

3 Answers3

33

I was able to find the total count of items in the shopping cart using the following code:

$totalItemsInCart = Mage::helper('checkout/cart')->getItemsCount();

If it does not work, let me know.

joeybab3
  • 295
  • 2
  • 7
  • 24
Mukesh
  • 7,630
  • 21
  • 105
  • 159
  • 2
    Ah thanks this did the trick for me :) I would upvote you but my reputation is too low.. – Wesley Smits Oct 08 '12 at 11:25
  • How can i find if cart has selected category of products? I have category id's how can i check if cart has selected categories of products? @Mukesh – Gem Aug 05 '19 at 04:56
4

You can try this.

$cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemsQty();

if($cart_qty) {
    // Not empty.
} else {
    // Empty.
}
Cj Belo
  • 51
  • 7
3

I think this could help:

http://blog.decryptweb.com/empty-cart-magento/

You can try something like this:

$checkout_cart = Mage::getSingleton('checkout/cart');
$items = $checkout_cart->getItems();
René Höhle
  • 26,716
  • 22
  • 73
  • 82