In WooCommerce I want to redirect from cart to shop page when cart is empty and I am using this code:
function cart_empty_redirect_to_shop(){
global $woocommerce;
if ( is_page('cart') and !sizeof($woocommerce->cart->cart_contents) ) {
wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) ); exit;
}
}
add_action( 'wp_head', 'cart_empty_redirect_to_shop' );
This code works only if cart is empty and I try to go on cart page. But if I am already on cart page and I remove all cart items, I have to reload the page to get redirected. So I guess I have to add something in the code to reload the page. Any thoughts?
I would appreciate if anyone could help me with this.