Giuls
Giuls

Reputation: 580

Stop add to cart redirection after an error in Woocommerce

Is there a way to return false when hooking into the function woocommerce_cart_redirect_after_error? Instead of redirecting to a given page I want to remain on the current page and display a javascript alert.

I have researched the function on Hookr.io but can't figure how to stop redirection...

Code:

add_filter( 'woocommerce_cart_redirect_after_error', 'stop_error_redirect' );
function stop_error_redirect( $url ) {
  global $wp;
        //Stop Redirect to cart Here and stay on current page
        $url = WC()->cart->get_cart_url();
        return $url;
    }

Reason is I am trying to display a javascript alert on the shop page when the woocommerce_add_to_cart_validation fires, instead of redirecting to the cart and displaying the standard wc_add_notice

Upvotes: 1

Views: 2073

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253919

To stop add to cart redirection error just use this line:

add_filter( 'woocommerce_cart_redirect_after_error', '__return_false' );

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Upvotes: 4

Related Questions