diff --git a/assets/js/frontend/add-to-cart.js b/assets/js/frontend/add-to-cart.js index 5b5a332c699..05de7d41696 100644 --- a/assets/js/frontend/add-to-cart.js +++ b/assets/js/frontend/add-to-cart.js @@ -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,25 +78,30 @@ 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 ) { - if ( ! response ) { - return; - } + 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; + } - if ( response.error && response.product_url ) { - window.location = response.product_url; - return; - } + if ( response.error && response.product_url ) { + window.location = response.product_url; + return; + } - // Redirect to cart option - if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) { - window.location = wc_add_to_cart_params.cart_url; - return; - } + // Redirect to cart option + if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) { + window.location = wc_add_to_cart_params.cart_url; + return; + } - // Trigger event so themes can refresh other areas. - $( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] ); + // 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 ) { - if ( ! response || ! response.fragments ) { + 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 ] ); + }, + error: function() { window.location = $thisbutton.attr( 'href' ); return; - } - $( document.body ).trigger( 'removed_from_cart', [ response.fragments, response.cart_hash, $thisbutton ] ); - }).fail( 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. */ diff --git a/assets/js/frontend/cart.js b/assets/js/frontend/cart.js index 70de56b9827..816863ed454 100644 --- a/assets/js/frontend/cart.js +++ b/assets/js/frontend/cart.js @@ -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',