1

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Giuls
  • 570
  • 11
  • 33
  • @LoicTheAztec Thanks - yes that will work, however the `wc_add_notice` (which I am trying to replace with Javascript) does not display... – Giuls May 23 '18 at 12:48
  • You can't replace everything in Woocommerce with you sweet alert js… You should better think it different or use something else than woocommerce. I have added my code line as an answer, so you can approve it as it works. It will be useful for other people – LoicTheAztec May 23 '18 at 12:53
  • 1
    Remember that filter hooks will not allow you to add any JS as they only return a variable that can be altered... – LoicTheAztec May 23 '18 at 12:59
  • Understood, however I am only trying to use javascript alerts for two aspects (when max items in cart and to stop user adding more than max items)... the function `woocommerce_add_to_cart_validation` should still return the `wc_add_notice` on the same page, however it doesn't? – Giuls May 23 '18 at 13:06
  • What you can do is to make changes on action hook `woocommerce_add_to_cart` instead, where you can make whatever you want, like in [**this recent answer example that I have maid**](https://stackoverflow.com/questions/50467051/replace-specific-cart-item-based-on-product-quantity-in-woocommerce/50474731#50474731) … This code is not the same thing than you, but it shows you the way to make changes and to include your sweet alert. – LoicTheAztec May 23 '18 at 13:20
  • Okay Thanks @LoicTheAztec - will `woocommerce_add_to_cart` validate the cart items before added? I thought it may be easier to validate the item before adding to cart with `add_action('woocommerce_add_to_cart_validation')`(set a max) and then display js sweet alert. – Giuls May 23 '18 at 14:51
  • woocommerce_add_to_cart_validation is a **FILTER** hook (but not an action hook) and you can't replace its behavior by your sweet alert JS. It's simply not possible. End. – LoicTheAztec May 23 '18 at 15:02
  • Ah I see, so what you said with `woocommerce_add_to_cart` is the right way to go in order to validate the cart items...I'll take a look and try it. I asked as it was shown [here](https://wpbackoffice.com/adding-custom-add-cart-errors-woocommerce/) with `add_action` preceeding the hook. – Giuls May 23 '18 at 15:05
  • Maybe, It's a possible complicated turn around, but not sure… Now the linked thread in your comment is very old (from NOVEMBER 8, 2013). You should forget your sweet alert JS and use woocommerce system. I will not comment that anymore. – LoicTheAztec May 23 '18 at 15:15
  • I cannot get `wc_add_notice` to trigger for ajax events, that's why I looked at sweet alert JS. – Giuls May 23 '18 at 15:28

1 Answers1

4

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399