WooCommerce: Empty Cart Redirect

An empty WooCommerce Cart page can be frustrating for customers, especially if they’ve been browsing and adding items to their cart.

Redirecting them to a relevant page, like the Shop page, can help them continue shopping and potentially complete a purchase.

The redirect page can be used to showcase ongoing sales, promotions, or new products that customers might be interested in. This can help generate interest and encourage customers to make a purchase.

So, let’s see how we can redirect users from the empty Cart page to another page on the same website. Enjoy!

The empty Cart page is somewhat annoying and doesn’t add value to the customer whatsoever. What if we skip it completely?

PHP Snippet: Redirect Empty Cart Page To Other URL (e.g. Shop Page)

/**
 * @snippet       Redirect Empty Woo Cart Page
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     Join https://businessbloomer.com/club/
 */

add_action( 'template_redirect', 'bbloomer_redirect_empty_cart', 9999 );

function bbloomer_redirect_empty_cart() {
	if ( is_cart() && WC()->cart->is_empty() ) {
		wp_safe_redirect( wc_get_page_permalink( 'shop' ) );
		// OR wp_safe_redirect( 'https://example.com' );
		exit;
	}
}

add_action( 'wp_footer', 'bbloomer_redirect_empty_cart_after_ajax' );

function bbloomer_redirect_empty_cart_after_ajax() {
   if ( is_cart() ) {
      wc_enqueue_js( "
            $(document.body).on('wc_cart_emptied', function(){
               window.location.href = '" . wc_get_page_permalink( 'shop' ) . "';
            });
      " ) ;
   }
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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