We had an interesting question the other day from a user who wanted to know if it’s possible to do something only when the WooCommerce cart is empty. WooCommerce core includes several handy methods that can help you out with this, but lots of tutorials show a cart-empty check with the $woocommerce global. However, this is only necessary if working with WooCommerce 2.0 or earlier. You can instead use the WC() global function to cut this down a bit with WooCommerce 2.1 or newer.

We can do our check by using the WC() global function and getting the cart contents count. While there are several ways we could check this, I prefer this method since it returns as an integer, so we simply need to check if it’s equal to zero to determine if the cart is empty.

if ( WC()->cart->get_cart_contents_count() == 0 ) {
        // Do something fun
}

That’s all you need to check if the WooCommerce cart is empty in your own snippet or plugin.

Let’s take a look at a real use-case. Suppose you want to show a notice in your store if the cart is empty – perhaps this could encourage a promotion or inform customers of coupon codes. It also works really well as a companion to our WooCommerce Cart Notices extension, as this displays notices if items are in the cart.

We’ll create a notice that will only display if there are no products in the cart, and you can determine where you’d like to display the notice (based on which actions you choose to add it to).

Adding the first action will add this to the empty cart template:

WooCommerce Empty Cart Notice | cart

Cart page

While adding the second action will then display this throughout the rest of the WooCommerce pages, such as the shop archives and product pages.

WooCommerce Empty Cart Notice | shop

Shop Archive

WooCommerce Empty Cart Notice | Product

Product Page

Our notice will display while the WooCommerce cart is empty, then disappear once an item has been added to the cart (which is a great time to let Cart Notices take over!).

Published by Beka Rice

Beka leads product direction for SkyVerge, focusing on new features for our plugins and Jilt. She spends a lot of time on research and interviews, but likes to write so she has an excuse to spend more time jamming out to anything from The Clash to Lady Gaga.

16 Comments

  1. […] a tutorial on how to check if the WooCommerce cart is empty and do something, such as display a […]

  2. Question

    how to change the look and feel of the empty cart , for example if empty is empty a kind of css opacity and if filled highlighted

    • Unfortunately this would be different for every theme :/

      • Great tutorial to check the cart status! I’m wondering if it’s possible to add a class to a specific element (ie. a menu item) when cart is not empty?

        I’m trying to make a cart icon to be in solid colour when cart is not empty, and in its outline mode when cart is empty.

        Thanks a lot!

  3. Love this…but what if we wanted to take it a step further by doing something if a specific item is in the cart? For instance…gift wrap. I have a hidden product for that added service. On the cart page, I have a notice below the table mentioning it with a button to add it to your cart. I would like to hide that notice if, and only if, that product is already in the cart.

    I have tried several times and I just can’t figure it out… :/

    • Hey Kahil, I’d recommend checking out this tutorial, which has some examples on conditionally adding info to checkout fields. While you may not use all of the code, it does include examples on how to check for a product.

  4. This is handy for adding something to the empty cart page. If I want to change the “Your cart is currently empty” text to something else, do I have to copy cart-empty.php from woocommerce to my theme folder? Or is there a filter? I’ve been trying to find a decent filter and hook reference and am coming up empty.

  5. Ah, cool. For some reason I’m forever forgetting about that plugin. Thanks, Beka.

  6. This won’t work if we removed last product from the cart and trace of that product is still in the cart, like undo removal of product. Technically cart isn’t empty because there is trace of last deleted product but from end user’s perspective cart should be empty and the respective code should work because user removed last product from cart.

  7. How to check if woocommerce cart is more than a certain value?

  8. Nice little bit of code! Thanks a lot 🙂

Hmm, looks like this article is quite old! Its content may be outdated, so comments are now closed.