Set adding_to_cart trigger to variable. If false, do not fire ajax. Closes #25759.

This commit is contained in:
Kathy Daring 2020-02-25 13:12:27 -07:00
parent 4138aecdcd
commit 38e081e590
1 changed files with 25 additions and 23 deletions

View File

@ -76,33 +76,35 @@ jQuery( function( $ ) {
}); });
// Trigger event. // Trigger event.
$( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] ); var valid = $( document.body ).triggerHandler( 'adding_to_cart', [ $thisbutton, data ] );
e.data.addToCartHandler.addRequest({ if( valid !== false ) {
type: 'POST', e.data.addToCartHandler.addRequest({
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), type: 'POST',
data: data, url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ),
success: function( response ) { data: data,
if ( ! response ) { success: function( response ) {
return; if ( ! response ) {
} return;
}
if ( response.error && response.product_url ) { if ( response.error && response.product_url ) {
window.location = response.product_url; window.location = response.product_url;
return; return;
} }
// Redirect to cart option // Redirect to cart option
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) { if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
window.location = wc_add_to_cart_params.cart_url; window.location = wc_add_to_cart_params.cart_url;
return; return;
} }
// Trigger event so themes can refresh other areas. // Trigger event so themes can refresh other areas.
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] ); $( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
}, },
dataType: 'json' dataType: 'json'
}); });
}
} }
}; };