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!
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' ) . "';
});
" ) ;
}
}