Merge pull request #25760 from helgatheviking/issues/25759-conditionally-disable-ajax-add-to-cart

Conditionally disable ajax add to cart
This commit is contained in:
Christopher Allford 2020-05-11 19:46:50 -07:00 committed by GitHub
commit 7206365250
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 2 deletions

View File

@ -17,6 +17,7 @@ jQuery( function( $ ) {
.on( 'click', '.add_to_cart_button', { addToCartHandler: this }, this.onAddToCart ) .on( 'click', '.add_to_cart_button', { addToCartHandler: this }, this.onAddToCart )
.on( 'click', '.remove_from_cart_button', { addToCartHandler: this }, this.onRemoveFromCart ) .on( 'click', '.remove_from_cart_button', { addToCartHandler: this }, this.onRemoveFromCart )
.on( 'added_to_cart', this.updateButton ) .on( 'added_to_cart', this.updateButton )
.on( 'ajax_request_not_sent.adding_to_cart', this.updateButton )
.on( 'added_to_cart removed_from_cart', { addToCartHandler: this }, this.updateFragments ); .on( 'added_to_cart removed_from_cart', { addToCartHandler: this }, this.updateFragments );
}; };
@ -69,6 +70,12 @@ jQuery( function( $ ) {
$thisbutton.removeClass( 'added' ); $thisbutton.removeClass( 'added' );
$thisbutton.addClass( 'loading' ); $thisbutton.addClass( 'loading' );
// Allow 3rd parties to validate and quit early.
if ( false === $( document.body ).triggerHandler( 'should_send_ajax_request.adding_to_cart', [ $thisbutton ] ) ) {
$( document.body ).trigger( 'ajax_request_not_sent.adding_to_cart', [ false, false, $thisbutton ] );
return true;
}
var data = {}; var data = {};
// Fetch changes that are directly added by calling $thisbutton.data( key, value ) // Fetch changes that are directly added by calling $thisbutton.data( key, value )
@ -159,10 +166,13 @@ jQuery( function( $ ) {
if ( $button ) { if ( $button ) {
$button.removeClass( 'loading' ); $button.removeClass( 'loading' );
if ( fragments ) {
$button.addClass( 'added' ); $button.addClass( 'added' );
}
// View cart text. // View cart text.
if ( ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) { if ( fragments && ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) {
$button.after( ' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' + $button.after( ' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' +
wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' ); wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' );
} }