Use prototype pattern and trigger wc_cart_button_updated after updating markup
Fixes #15447
This commit is contained in:
parent
d5cb320b7e
commit
f22d30603d
|
@ -1,25 +1,34 @@
|
|||
/* global wc_add_to_cart_params */
|
||||
/*!
|
||||
* WooCommerce Add to Cart JS
|
||||
*/
|
||||
jQuery( function( $ ) {
|
||||
|
||||
if ( typeof wc_add_to_cart_params === 'undefined' ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ajax add to cart.
|
||||
$( document ).on( 'click', '.add_to_cart_button', function() {
|
||||
/**
|
||||
* AddToCartHandler class.
|
||||
*/
|
||||
var AddToCartHandler = function() {
|
||||
$( document )
|
||||
.on( 'click', '.add_to_cart_button', this.onAddToCart )
|
||||
.on( 'added_to_cart', this.updateButton )
|
||||
.on( 'added_to_cart', this.updateCartPage )
|
||||
.on( 'added_to_cart', this.updateFragments );
|
||||
};
|
||||
|
||||
// AJAX add to cart request.
|
||||
/**
|
||||
* Handle the add to cart event.
|
||||
*/
|
||||
AddToCartHandler.prototype.onAddToCart = function( e ) {
|
||||
var $thisbutton = $( this );
|
||||
|
||||
if ( $thisbutton.is( '.ajax_add_to_cart' ) ) {
|
||||
|
||||
if ( ! $thisbutton.attr( 'data-product_id' ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
$thisbutton.removeClass( 'added' );
|
||||
$thisbutton.addClass( 'loading' );
|
||||
|
||||
|
@ -34,7 +43,6 @@ jQuery( function( $ ) {
|
|||
|
||||
// Ajax action.
|
||||
$.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), data, function( response ) {
|
||||
|
||||
if ( ! response ) {
|
||||
return;
|
||||
}
|
||||
|
@ -46,53 +54,24 @@ jQuery( function( $ ) {
|
|||
|
||||
// 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;
|
||||
|
||||
} else {
|
||||
|
||||
// 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 ] );
|
||||
});
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
// On "added_to_cart"
|
||||
$( document.body ).on( 'added_to_cart', function( event, fragments, cart_hash, $button ) {
|
||||
var page = window.location.toString().replace( 'add-to-cart', 'added-to-cart' );
|
||||
/**
|
||||
* Update cart page elements after add to cart events.
|
||||
*/
|
||||
AddToCartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) {
|
||||
$button = typeof $button === 'undefined' ? false : $button;
|
||||
|
||||
if ( $button ) {
|
||||
$button.removeClass( 'loading' );
|
||||
}
|
||||
|
||||
// Block fragments class.
|
||||
if ( fragments ) {
|
||||
$.each( fragments, function( key ) {
|
||||
$( key ).addClass( 'updating' );
|
||||
});
|
||||
}
|
||||
|
||||
// Block widgets and fragments.
|
||||
$( '.shop_table.cart, .updating, .cart_totals' )
|
||||
.fadeTo( '400', '0.6' )
|
||||
.block({
|
||||
message: null,
|
||||
overlayCSS: {
|
||||
opacity: 0.6
|
||||
}
|
||||
});
|
||||
|
||||
if ( $button ) {
|
||||
// Changes button classes.
|
||||
$button.addClass( 'added' );
|
||||
|
||||
// View cart text.
|
||||
|
@ -100,31 +79,56 @@ jQuery( function( $ ) {
|
|||
$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>' );
|
||||
}
|
||||
|
||||
$( document.body ).trigger( 'wc_cart_button_updated', [ $button ] );
|
||||
}
|
||||
};
|
||||
|
||||
// Replace fragments.
|
||||
if ( fragments ) {
|
||||
$.each( fragments, function( key, value ) {
|
||||
$( key ).replaceWith( value );
|
||||
});
|
||||
/**
|
||||
* 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' );
|
||||
|
||||
$( document.body ).trigger( 'wc_fragments_loaded' );
|
||||
}
|
||||
|
||||
// Unblock.
|
||||
$( '.widget_shopping_cart, .updating' ).stop( true ).css( 'opacity', '1' ).unblock();
|
||||
|
||||
// Cart page elements.
|
||||
$( '.shop_table.cart' ).load( page + ' .shop_table.cart:eq(0) > *', function() {
|
||||
$( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
|
||||
|
||||
$( document.body ).trigger( 'cart_page_refreshed' );
|
||||
});
|
||||
|
||||
$( '.cart_totals' ).load( page + ' .cart_totals:eq(0) > *', function() {
|
||||
$( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock();
|
||||
$( document.body ).trigger( 'cart_totals_refreshed' );
|
||||
});
|
||||
};
|
||||
|
||||
});
|
||||
/**
|
||||
* Update fragments after add to cart events.
|
||||
*/
|
||||
AddToCartHandler.prototype.updateFragments = function( e, fragments ) {
|
||||
if ( fragments ) {
|
||||
$.each( fragments, function( key ) {
|
||||
$( key )
|
||||
.addClass( 'updating' )
|
||||
.fadeTo( '400', '0.6' )
|
||||
.block({
|
||||
message: null,
|
||||
overlayCSS: {
|
||||
opacity: 0.6
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$.each( fragments, function( key, value ) {
|
||||
$( key ).replaceWith( value );
|
||||
$( key ).stop( true ).css( 'opacity', '1' ).unblock();
|
||||
});
|
||||
|
||||
$( document.body ).trigger( 'wc_fragments_loaded' );
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Init AddToCartHandler.
|
||||
*/
|
||||
new AddToCartHandler();
|
||||
});
|
||||
|
|
|
@ -1,4 +1 @@
|
|||
/*!
|
||||
* WooCommerce Add to Cart JS
|
||||
*/
|
||||
jQuery(function(a){return"undefined"!=typeof wc_add_to_cart_params&&(a(document).on("click",".add_to_cart_button",function(){var b=a(this);if(b.is(".ajax_add_to_cart")){if(!b.attr("data-product_id"))return!0;b.removeClass("added"),b.addClass("loading");var c={};return a.each(b.data(),function(a,b){c[a]=b}),a(document.body).trigger("adding_to_cart",[b,c]),a.post(wc_add_to_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","add_to_cart"),c,function(c){if(c)return c.error&&c.product_url?void(window.location=c.product_url):"yes"===wc_add_to_cart_params.cart_redirect_after_add?void(window.location=wc_add_to_cart_params.cart_url):void a(document.body).trigger("added_to_cart",[c.fragments,c.cart_hash,b])}),!1}return!0}),void a(document.body).on("added_to_cart",function(b,c,d,e){var f=window.location.toString().replace("add-to-cart","added-to-cart");e="undefined"!=typeof e&&e,e&&e.removeClass("loading"),c&&a.each(c,function(b){a(b).addClass("updating")}),a(".shop_table.cart, .updating, .cart_totals").fadeTo("400","0.6").block({message:null,overlayCSS:{opacity:.6}}),e&&(e.addClass("added"),wc_add_to_cart_params.is_cart||0!==e.parent().find(".added_to_cart").length||e.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>")),c&&(a.each(c,function(b,c){a(b).replaceWith(c)}),a(document.body).trigger("wc_fragments_loaded")),a(".widget_shopping_cart, .updating").stop(!0).css("opacity","1").unblock(),a(".shop_table.cart").load(f+" .shop_table.cart:eq(0) > *",function(){a(".shop_table.cart").stop(!0).css("opacity","1").unblock(),a(document.body).trigger("cart_page_refreshed")}),a(".cart_totals").load(f+" .cart_totals:eq(0) > *",function(){a(".cart_totals").stop(!0).css("opacity","1").unblock()})}))});
|
||||
jQuery(function(a){if("undefined"==typeof wc_add_to_cart_params)return!1;var b=function(){a(document).on("click",".add_to_cart_button",this.onAddToCart).on("added_to_cart",this.updateButton).on("added_to_cart",this.updateCartPage).on("added_to_cart",this.updateFragments)};b.prototype.onAddToCart=function(b){var c=a(this);if(c.is(".ajax_add_to_cart")){if(!c.attr("data-product_id"))return!0;b.preventDefault(),c.removeClass("added"),c.addClass("loading");var d={};a.each(c.data(),function(a,b){d[a]=b}),a(document.body).trigger("adding_to_cart",[c,d]),a.post(wc_add_to_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","add_to_cart"),d,function(b){if(b)return b.error&&b.product_url?void(window.location=b.product_url):"yes"===wc_add_to_cart_params.cart_redirect_after_add?void(window.location=wc_add_to_cart_params.cart_url):void a(document.body).trigger("added_to_cart",[b.fragments,b.cart_hash,c])})}},b.prototype.updateButton=function(b,c,d,e){e="undefined"!=typeof e&&e,e&&(e.removeClass("loading"),e.addClass("added"),wc_add_to_cart_params.is_cart||0!==e.parent().find(".added_to_cart").length||e.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>"),a(document.body).trigger("wc_cart_button_updated",[e]))},b.prototype.updateCartPage=function(){var b=window.location.toString().replace("add-to-cart","added-to-cart");a(".shop_table.cart").load(b+" .shop_table.cart:eq(0) > *",function(){a(".shop_table.cart").stop(!0).css("opacity","1").unblock(),a(document.body).trigger("cart_page_refreshed")}),a(".cart_totals").load(b+" .cart_totals:eq(0) > *",function(){a(".cart_totals").stop(!0).css("opacity","1").unblock(),a(document.body).trigger("cart_totals_refreshed")})},b.prototype.updateFragments=function(b,c){c&&(a.each(c,function(b){a(b).addClass("updating").fadeTo("400","0.6").block({message:null,overlayCSS:{opacity:.6}})}),a.each(c,function(b,c){a(b).replaceWith(c),a(b).stop(!0).css("opacity","1").unblock()}),a(document.body).trigger("wc_fragments_loaded"))},new b});
|
Loading…
Reference in New Issue