Add remove coupon ajax handling in cart.

Add code to handle ajax call for removing a coupon from the cart.
This commit is contained in:
Kevin Killingsworth 2016-02-11 13:55:34 -06:00
parent 191b58eba0
commit 30a272e58a
3 changed files with 31 additions and 1 deletions

View File

@ -111,4 +111,33 @@ jQuery( function( $ ) {
} }
}); });
}); });
$( document ).on( 'click', 'a.woocommerce-remove-coupon', function( evt ) {
evt.preventDefault();
var $tr = $( this ).parents( 'tr' );
var coupon = $( this ).attr( 'data-coupon' );
block( $tr.parents( 'table' ) );
var url = wc_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_coupon' );
var data = {
security: wc_cart_params.remove_coupon_nonce,
coupon: coupon
};
$.ajax( {
type: 'POST',
url: url,
data: data,
dataType: 'html',
success: function( response ) {
show_notice( response );
unblock( $tr.parents( 'table' ) );
},
complete: function() {
update_cart_totals();
}
});
});
}); });

View File

@ -1 +1 @@
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()}})})}); 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()}})}),a(document).on("click","a.woocommerce-remove-coupon",function(b){b.preventDefault();var g=a(this).parents("tr"),h=a(this).attr("data-coupon");c(g.parents("table"));var i=wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","remove_coupon"),j={security:wc_cart_params.remove_coupon_nonce,coupon:h};a.ajax({type:"POST",url:i,data:j,dataType:"html",success:function(a){f(a),d(g.parents("table"))},complete:function(){e()}})})});

View File

@ -290,6 +290,7 @@ class WC_Frontend_Scripts {
'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ), 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ),
'update_shipping_method_nonce' => wp_create_nonce( "update-shipping-method" ), 'update_shipping_method_nonce' => wp_create_nonce( "update-shipping-method" ),
'apply_coupon_nonce' => wp_create_nonce( "apply-coupon" ), 'apply_coupon_nonce' => wp_create_nonce( "apply-coupon" ),
'remove_coupon_nonce' => wp_create_nonce( "remove-coupon" ),
); );
break; break;
case 'wc-cart-fragments' : case 'wc-cart-fragments' :