diff --git a/assets/js/frontend/add-to-cart.js b/assets/js/frontend/add-to-cart.js index 8d6666c96de..6fcee7b5a74 100644 --- a/assets/js/frontend/add-to-cart.js +++ b/assets/js/frontend/add-to-cart.js @@ -17,6 +17,7 @@ jQuery( function( $ ) { .on( 'click', '.add_to_cart_button', { addToCartHandler: this }, this.onAddToCart ) .on( 'click', '.remove_from_cart_button', { addToCartHandler: this }, this.onRemoveFromCart ) .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 ); }; @@ -69,6 +70,12 @@ jQuery( function( $ ) { $thisbutton.removeClass( 'added' ); $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 = {}; // Fetch changes that are directly added by calling $thisbutton.data( key, value ) @@ -159,10 +166,13 @@ jQuery( function( $ ) { if ( $button ) { $button.removeClass( 'loading' ); - $button.addClass( 'added' ); + + if ( fragments ) { + $button.addClass( 'added' ); + } // 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( ' ' + wc_add_to_cart_params.i18n_view_cart + '' ); }