2
votes

How to output added-to-cart messages or add to cart error message on the current page Woocommerce after add-to-cart button had been fired? Since error messages usally redirected in class-wc-cart.php? Because I allowed add to cart button on content-product.php page. The code of redirect after add to car on class-wc-cart AJAX look like this:

           // If there was an error adding to the cart, redirect to the product page to show any errors
            $data = array(
                'error'       => true,
                'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
            );// it's redirect link to individual product page

            wp_send_json( $data );

the website link is: http://gotheelz.com

2

2 Answers

0
votes

Try changing the settings of woocommerce,

WooCommerce > Settings > Products > Display Tab > Add to cart behaviour -- Untick Redirect to the cart page after successful addition and [✓] Enable AJAX add to cart buttons on archives.

This solution worked for me. Let me know if the same worked for you.

0
votes
          //Redirect any other role to the previous visited page or current on page
          $redirect = wp_get_referer() ? wp_get_referer() : home_url();
          $data = array(
            'error'       => true,
            'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', $redirect, $product_id )
          );// it's redirect link to individual product page
        wp_send_json( $data );