How to output added to cart messsage or add to cart error on the current page Woocommerce?

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

Related posts

2 comments

  1. 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.

  2.           //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 );
    

Comments are closed.