The WooCommerce Add Cart Ajax Button loads page on finish request

WooCommerce Logo

I faced a issue working with WooCommerce where I used ajax buttons for my add to cart actions in the shop page and when I clicked on them instead of kept me in the same page (as ajax is for) they reloaded the shop page, I had to figure out why.

When add to cart ajax buttons are used the async request code in javascript has some lines in what to do when request ends. The path to the JS file is “plugins/woocommerce/assets/js/frontend/add-to-cart.js”; here is the code block I’m talking about:

// Ajax action.
$.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), data, function( response ) {
if ( ! response ) {

if ( response.error && response.product_url ) {
window.location = response.product_url;

// Redirect to cart option
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
window.location = wc_add_to_cart_params.cart_url;

// Trigger event so themes can refresh other areas.
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );

If it had some trouble proccessing the request the user is redirected to product page (if available). I found some things that can trigger errors in this StackOverflow post, it’s said:

If you have inventory, order maximums, product in cart maximums and any other limitations set this will happen.

I would add two more usual suspects that can trigger the redirect or prevent add cart ajax buttons to work async: there are two options located at “WooCommerce tab (from WP dashboard) => Settings => Products tab”.

I think they are self-explanatory. Asure you have this configured as you want.

So the thing is that nothing of this applies to me :(. What happened to me is that the redirect is also triggered if the product post visibility setting is set to private. So changing to public resolved the issue.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.