Woocommerce condition if cart is empty

A user recently asked us an intriguing question about whether it was feasible to perform a specific action only when the WooCommerce cart was empty. There are a number of helpful methods in the WooCommerce core that can assist you with this, but many tutorials demonstrate a cart-empty check using the $woocommerce global. However, if using WooCommerce 2.0 or a previous version, this is just necessary. With WooCommerce 2.1 or newer, you can utilise the WC() global function to reduce this a little.

Using the global function WC() and the count of the cart\’s items, we can do our check. There are a number of ways we might check this, but I prefer this approach because it returns an integer, thus all we need to do to see if the cart is empty is check if it equals zero.

if ( WC()->cart->get_cart_contents_count() == 0 ) {
// your code here
}

You just need that information to determine whether the WooCommerce cart is empty in your own snippet or plugin.

Let\’s examine a genuine uc.If the cart is empty, let\’s say you want to display a notification in your store. This can stimulate a promotion or alert customers to coupon codes. It also complements our WooCommerce Cart Notices extension quite well, which shows notices if things are in the cart.

You can select where you\’d want to display the message (depending on the actions you choose to add it to), and it will only appear if there are no products in the cart.

function coderazaa_empty_cart_notice() {

if ( WC()->cart->get_cart_contents_count() == 0 ) {
wc_print_notice( __( \’Get free shipping if your order is over $60!\’, \’woocommerce\’ ), \’notice\’ );
// Change notice text as desired
}

}
// Add to cart page
add_action( \’woocommerce_check_cart_items\’, \’coderazaa_empty_cart_notice\’ );
// Add to shop archives & product pages
add_action( \’woocommerce_before_main_content\’, \’coderazaa_empty_cart_notice\’ );

 

The shop archives and product pages, as well as the rest of the WooCommerce pages, will then display this after adding the second action.

Once an item is placed to the WooCommerce cart, our message will no longer be visible, making it the perfect opportunity to let Cart Notices take over.

 

 

Related Posts
Learn How to Fix WordPress Search Not Working (3+ Major Issues Resolved)

Are you looking to solve WordPress search issues on your website? Troubleshooting WordPress search issues may be difficult. This is Read more

How to Fix WordPress\’s White Screen of Death issue

Don\'t be alarmed if you get a WordPress error message or a white screen. Someone has most certainly seen the Read more

WordPress Installation

WordPress Installation Procedures Get the package at http://www.wordpress.org. Open your root web server and install WordPress. When you extract it, Read more

How to Customize WordPress Site

Customize Your WordPress Site Navigate to http://localhost/thedemostore/wp - admin. You should get something like this: admin is the user name. Read more

About WordPress Post

What exactly is a WordPress Post? How to Create and Update a WordPress Post? A CMS can have various types Read more

Image Shared on WordPress to Facebook Not Coming Up? Here\’s what to do.

  When you share your blog posts or web pages on Facebook, the picture might not show up the way Read more

What are WordPress wp_head & wp_footer functions

The wp_head and wp_footer methods are two important things to add to a WordPress theme. \"Action hooks\" are used to Read more

How to fix There was an error trying to send your message. Please try again later.

You make contact forms to make it easy for your audience to get in touch with you. Whether you use Read more

Scroll to Top