Merge pull request #23293 from woocommerce/fix/23283

Queue AJAX add to cart events to avoid overwriting session data
This commit is contained in:
Rodrigo Primo 2019-07-12 11:16:52 -03:00 committed by GitHub
commit 49286ba1ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 43 deletions

View File

@ -9,14 +9,50 @@ jQuery( function( $ ) {
* AddToCartHandler class.
*/
var AddToCartHandler = function() {
this.requests = [];
this.addRequest = this.addRequest.bind( this );
this.run = this.run.bind( this );
$( document.body )
.on( 'click', '.add_to_cart_button', this.onAddToCart )
.on( 'click', '.remove_from_cart_button', this.onRemoveFromCart )
.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( 'added_to_cart', this.updateCartPage )
.on( 'added_to_cart removed_from_cart', this.updateFragments );
.on( 'added_to_cart removed_from_cart', { addToCartHandler: this }, this.updateFragments );
};
/**
* Add add to cart event.
*/
AddToCartHandler.prototype.addRequest = function( request ) {
this.requests.push( request );
if ( 1 === this.requests.length ) {
this.run();
}
}
/**
* Run add to cart events.
*/
AddToCartHandler.prototype.run = function( e ) {
var requestManager = this,
originalCallback = requestManager.requests[0].complete;
requestManager.requests[0].complete = function() {
if ( typeof originalCallback === 'function' ) {
originalCallback();
}
requestManager.requests.shift();
if ( requestManager.requests.length > 0 ) {
requestManager.run();
}
};
$.ajax( this.requests[0] );
}
/**
* Handle the add to cart event.
*/
@ -42,8 +78,11 @@ jQuery( function( $ ) {
// Trigger event.
$( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
// Ajax action.
$.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), data, function( response ) {
e.data.addToCartHandler.addRequest({
type: 'POST',
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ),
data: data,
success: function( response ) {
if ( ! response ) {
return;
}
@ -61,6 +100,8 @@ jQuery( function( $ ) {
// Trigger event so themes can refresh other areas.
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
},
dataType: 'json'
});
}
};
@ -81,15 +122,25 @@ jQuery( function( $ ) {
}
});
$.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_from_cart' ), { cart_item_key : $thisbutton.data( 'cart_item_key' ) }, function( response ) {
e.data.addToCartHandler.addRequest({
type: 'POST',
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_from_cart' ),
data: {
cart_item_key : $thisbutton.data( 'cart_item_key' )
},
success: function( response ) {
if ( ! response || ! response.fragments ) {
window.location = $thisbutton.attr( 'href' );
return;
}
$( document.body ).trigger( 'removed_from_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
}).fail( function() {
},
error: function() {
window.location = $thisbutton.attr( 'href' );
return;
},
dataType: 'json'
});
};
@ -113,21 +164,6 @@ jQuery( function( $ ) {
}
};
/**
* Update cart page elements after add to cart events.
*/
AddToCartHandler.prototype.updateCartPage = function() {
var page = window.location.toString().replace( 'add-to-cart', 'added-to-cart' );
$.get( page, function( data ) {
$( '.shop_table.cart:eq(0)' ).replaceWith( $( data ).find( '.shop_table.cart:eq(0)' ) );
$( '.cart_totals:eq(0)' ).replaceWith( $( data ).find( '.cart_totals:eq(0)' ) );
$( '.cart_totals, .shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
$( document.body ).trigger( 'cart_page_refreshed' );
$( document.body ).trigger( 'cart_totals_refreshed' );
} );
};
/**
* Update fragments after add to cart events.
*/

View File

@ -270,7 +270,7 @@ jQuery( function( $ ) {
this.update_cart = this.update_cart.bind( this );
$( document ).on(
'wc_update_cart',
'wc_update_cart added_to_cart',
function() { cart.update_cart.apply( cart, [].slice.call( arguments, 1 ) ); } );
$( document ).on(
'click',