Search code examples
phpwordpresswoocommercecart

Woocommerce login redirect if cart not empty


I want to redirect the user to cart page after he logs in if the cart is not empty. I am trying this:

function woocommerce_custom_redirects() {

    if(!WC()->cart->is_empty() )
        wp_redirect( "https://edkasa.com/checkout" );
}
add_action('wp_login', 'woocommerce_custom_redirects');

But this is not working, I am using buddypress plugin, any idea where am I going wrong.


Solution

  • With the wp_redirect() Function, it is suggested to follow that up with an explicit call to exit. For further Information on that, see Documentation.

    You may thus try adding that to your Code... Something like:

    function woocommerce_custom_redirects() {
        ob_start();     // COULD BE A GOOD IDEA
        if ( WC()->cart->get_cart_contents_count() !== 0 || !WC()->cart->is_empty() ) {
            wp_redirect( "https://edkasa.com/checkout" );
            exit;       // VERY VITAL HERE TO EXPLICITLY CALL exit...
        }
    
    }
    add_action('wp_login', 'woocommerce_custom_redirects');