How to Redirect an Empty WooCommerce Cart
If you’re looking for a simple way to guide your customers back to shopping when their cart is empty, I’ve got a neat little code snippet for woocommerce that will do just that. It’s like giving your customers a warm nudge to keep browsing your awesome products. Let’s dive into this user-friendly solution and see how it works.
// Redirecting Empty WooCommerce Cart
add_action( 'template_redirect', function() {
if ( class_exists( 'WooCommerce' ) && is_cart() && WC()->cart->is_empty() ) {
$shop_url = add_query_arg( 'cart_empty', '1', wc_get_page_permalink( 'shop' ) ?: home_url() );
wp_redirect( $shop_url, 302 );
exit;
}
});
add_action( 'woocommerce_before_shop_loop', function() {
if ( isset( $_GET['cart_empty'] ) ) {
wc_add_notice( __( 'Your cart is empty! Browse our products to add items.', 'woocommerce' ), 'notice' );
}
});How the Code Snippet Works & What to Expect
This snippet is like a friendly shop assistant who notices when a customer’s cart is empty and gently guides them back to the shop with a helpful message. Here’s the breakdown:
- Checking for an Empty Cart: The first part of the code hooks into WordPress’s template_redirect action. It checks if WooCommerce is active, if the user is on the cart page, and if the cart is empty. If all those conditions are met, it’s time to act!
- Redirecting to the Shop with a 302: Instead of leaving customers staring at an empty cart, the code redirects them to your shop page (or the homepage if the shop page isn’t set). It uses a 302 redirect, which is a temporary redirect. This tells browsers and search engines, “Hey, this move is just for now, not permanent.” It’s perfect here because an empty cart is a temporary situation—once the customer adds items, they can visit the cart page again without issues. The redirect also adds a little marker (cart_empty=1) to the URL to let the system know why they’re being sent back.
- Showing a Friendly Message: The second part of the snippet uses the woocommerce_before_shop_loop hook to check for that cart_empty marker in the URL. When it’s there, it displays a warm notice: “Your cart is empty! Browse our products to add items.” This message pops up at the top of the shop page, encouraging customers to keep exploring.
What to Expect
When you add this snippet to your site (typically in your theme’s functions.php file or a custom plugin), here’s what happens:
- If a customer visits the cart page and it’s empty, they’ll be smoothly redirected to your shop page using a 302 redirect, which is temporary and won’t confuse search engines or browsers.
- Once they land on the shop page, they’ll see a polite, encouraging message reminding them to browse and add items to their cart.
- The redirect feels seamless, and the notice is styled to match WooCommerce’s default look, keeping everything professional and user-friendly.
This little trick keeps the shopping experience positive and engaging, gently guiding customers back to your products without any frustration. The 302 redirect ensures the process is temporary and flexible, perfect for a dynamic situation like an empty cart. It’s a small touch that can make a big difference in keeping your visitors excited about shopping with you.
Written by Milan on 10th July 2025

