Redirect an Empty Cart to the Shop
Sends visitors who open the cart page with nothing in it straight to the shop page instead of showing an empty cart.
Opens WPIDE on
Code
add_action( 'template_redirect', function () {
if ( ! function_exists( 'is_cart' ) || ! is_cart() || ! WC()->cart || ! WC()->cart->is_empty() ) {
return;
}
$target = wc_get_page_permalink( 'shop' ); // Where empty carts go.
wp_safe_redirect( $target );
exit;
} );
Notes
Change $target to redirect to another page such as a category or the homepage.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.