Block cart form while applying coupon.

Add code to block the cart form during the apply operation
until completion. Refactor block/unblock into local methods
for reuse.
This commit is contained in:
Kevin Killingsworth 2016-02-11 13:01:20 -06:00
parent 4cc7e99ab7
commit 191b58eba0
2 changed files with 34 additions and 16 deletions

View File

@ -1,6 +1,27 @@
/* global wc_cart_params */
jQuery( function( $ ) {
// Check if a node is blocked for processing.
var is_blocked = function( $node ) {
return $node.is( '.processing' );
};
// Block a node for processing.
var block = function( $node ) {
$node.addClass( 'processing' ).block( {
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
};
// Unblock a node after processing is complete.
var unblock = function( $node ) {
$node.removeClass( 'processing' ).unblock();
};
// wc_cart_params is required to continue, ensure the object exists
if ( typeof wc_cart_params === 'undefined' ) {
return false;
@ -17,13 +38,7 @@ jQuery( function( $ ) {
shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val();
});
$( 'div.cart_totals' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
block( $( 'div.cart_totals' ) );
var data = {
security: wc_cart_params.update_shipping_method_nonce,
@ -40,13 +55,7 @@ jQuery( function( $ ) {
// Update the cart after something has changed.
var update_cart_totals = function() {
$( 'div.cart_totals' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
block( $( 'div.cart_totals' ) );
var url = wc_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_cart_totals' );
@ -70,9 +79,16 @@ jQuery( function( $ ) {
$( '[name="apply_coupon"]' ).on( 'click', function( evt ) {
evt.preventDefault();
var $form = $( 'div.woocommerce > form' );
if ( is_blocked( $form ) ) {
return false;
}
block( $form );
var $text_field = $( '#coupon_code' );
var coupon_code = $text_field.val();
$text_field.val( '' );
var url = wc_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'apply_coupon' );
var data = {
@ -89,6 +105,8 @@ jQuery( function( $ ) {
show_notice( response );
},
complete: function() {
unblock( $form );
$text_field.val( '' );
update_cart_totals();
}
});

View File

@ -1 +1 @@
jQuery(function(a){if("undefined"==typeof wc_cart_params)return!1;a(document).on("click",".shipping-calculator-button",function(){return a(".shipping-calculator-form").slideToggle("slow"),!1}).on("change","select.shipping_method, input[name^=shipping_method]",function(){var b=[];a("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(){b[a(this).data("index")]=a(this).val()}),a("div.cart_totals").block({message:null,overlayCSS:{background:"#fff",opacity:.6}});var c={security:wc_cart_params.update_shipping_method_nonce,shipping_method:b};a.post(wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","update_shipping_method"),c,function(b){a("div.cart_totals").replaceWith(b),a(document.body).trigger("updated_shipping_method")})}),a(".shipping-calculator-form").hide();var b=function(){a("div.cart_totals").block({message:null,overlayCSS:{background:"#fff",opacity:.6}});var b=wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","get_cart_totals");a.ajax({url:b,success:function(b){a("div.cart_totals").replaceWith(b)}})},c=function(b){var c=a("div.woocommerce > form");a(".woocommerce-error, .woocommerce-message").remove(),c.before(b)};a('[name="apply_coupon"]').on("click",function(d){d.preventDefault();var e=a("#coupon_code"),f=e.val();e.val("");var g=wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","apply_coupon"),h={security:wc_cart_params.apply_coupon_nonce,coupon_code:f};a.ajax({type:"POST",url:g,data:h,dataType:"html",success:function(a){c(a)},complete:function(){b()}})})});
jQuery(function(a){var b=function(a){return a.is(".processing")},c=function(a){a.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},d=function(a){a.removeClass("processing").unblock()};if("undefined"==typeof wc_cart_params)return!1;a(document).on("click",".shipping-calculator-button",function(){return a(".shipping-calculator-form").slideToggle("slow"),!1}).on("change","select.shipping_method, input[name^=shipping_method]",function(){var b=[];a("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(){b[a(this).data("index")]=a(this).val()}),c(a("div.cart_totals"));var d={security:wc_cart_params.update_shipping_method_nonce,shipping_method:b};a.post(wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","update_shipping_method"),d,function(b){a("div.cart_totals").replaceWith(b),a(document.body).trigger("updated_shipping_method")})}),a(".shipping-calculator-form").hide();var e=function(){c(a("div.cart_totals"));var b=wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","get_cart_totals");a.ajax({url:b,success:function(b){a("div.cart_totals").replaceWith(b)}})},f=function(b){var c=a("div.woocommerce > form");a(".woocommerce-error, .woocommerce-message").remove(),c.before(b)};a('[name="apply_coupon"]').on("click",function(g){g.preventDefault();var h=a("div.woocommerce > form");if(b(h))return!1;c(h);var i=a("#coupon_code"),j=i.val(),k=wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","apply_coupon"),l={security:wc_cart_params.apply_coupon_nonce,coupon_code:j};a.ajax({type:"POST",url:k,data:l,dataType:"html",success:function(a){f(a)},complete:function(){d(h),i.val(""),e()}})})});