Boosting WooCommerce UX: How to Redirect an Empty Cart to the Shop Page
During a recent conversation with Pitch Black, a leading web advertising agency, we learned that sometimes the smallest tweaks can generate the biggest impact. One such change that has proven incredibly effective is the empty cart redirect. According to Pitch Black, this minor adjustment has had a surprisingly large effect on sales by improving user experience and reducing frustration.
Imagine a customer clicking to view their cart, only to find it empty—leaving them confused and without a clear path forward. Instead of risking abandonment, this redirect smoothly guides users back to your shop or a more relevant page. It’s an easy win, and today, we’ll walk you through exactly how to implement it on your WooCommerce store.
Why Redirect Users from an Empty Cart?
Here are a few reasons to add this handy feature to your WooCommerce store:
- Improved User Journey: Customers are redirected to a meaningful page instead of encountering an empty cart.
- Encourages Exploration: Instead of a dead-end, users land on the shop page where they can browse and find products.
- Reduces Bounce Rates: A smooth experience helps keep potential buyers on your site longer, increasing the likelihood of sales.
- Prevents Confusion: Users might wonder if there’s an issue with their purchase attempt. Redirecting clears any ambiguity.
How to Implement the Empty Cart Redirect
Here’s the PHP code snippet you can add to your site to automatically redirect users from an empty cart page:
add_action( 'template_redirect', 'fluxcms_redirect_empty_cart', 9999 );
function fluxcms_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;
}
}
Where to Add This Code
You can insert the above snippet into your theme’s functions.php file, or even better, create a child theme to ensure future updates don’t override your customisations. Alternatively, you can use a plugin like Code Snippets, which allows you to safely add and manage custom code on your WordPress site.
Understanding the Code
- add_action( ‘template_redirect’, … ):
This hook ensures the function is triggered at the right moment—just before the template loads. This makes it ideal for checking and redirecting pages. - is_cart():
This conditional function checks whether the user is currently viewing the cart page. - WC()->cart->is_empty():
It confirms if the WooCommerce cart is empty. - wp_safe_redirect():
This function safely redirects users to a new URL. Here, we usewc_get_page_permalink( 'shop' )
to send them to the shop page, but you can replace it with any valid URL. - exit;
Always includeexit;
after a redirect to ensure no further code executes.
Customising the Redirect URL
If you want to redirect users to a page other than the shop, simply replace this part:
wp_safe_redirect( wc_get_page_permalink( 'shop' ) );
with the URL of your choice:
wp_safe_redirect( 'https://example.com' );
Make sure to use a meaningful page, such as a product category, a landing page with featured products, or a special offer page.
Final Thoughts
Implementing an empty cart redirect is a small change that can have a significant impact on user experience. It ensures that visitors don’t feel stuck and encourages them to continue exploring your offerings. By taking this proactive approach, you can reduce frustration, improve navigation, and potentially increase conversions on your WooCommerce store.
Give this snippet a try, and let us know how it works for you! Have any other WooCommerce tips? Share them in the comments below.