From 30a272e58aa8a08365e20ea27974b6f34417c51f Mon Sep 17 00:00:00 2001 From: Kevin Killingsworth Date: Thu, 11 Feb 2016 13:55:34 -0600 Subject: [PATCH] Add remove coupon ajax handling in cart. Add code to handle ajax call for removing a coupon from the cart. --- assets/js/frontend/cart.js | 29 ++++++++++++++++++++++++++ assets/js/frontend/cart.min.js | 2 +- includes/class-wc-frontend-scripts.php | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/assets/js/frontend/cart.js b/assets/js/frontend/cart.js index 6ee7ebc1fd8..76b8a9d22ad 100644 --- a/assets/js/frontend/cart.js +++ b/assets/js/frontend/cart.js @@ -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(); + } + }); + }); }); diff --git a/assets/js/frontend/cart.min.js b/assets/js/frontend/cart.min.js index c464d8789a3..124f944134c 100644 --- a/assets/js/frontend/cart.min.js +++ b/assets/js/frontend/cart.min.js @@ -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()}})})}); \ No newline at end of file +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()}})})}); \ No newline at end of file diff --git a/includes/class-wc-frontend-scripts.php b/includes/class-wc-frontend-scripts.php index 01fe26abe8e..82a23703a47 100644 --- a/includes/class-wc-frontend-scripts.php +++ b/includes/class-wc-frontend-scripts.php @@ -290,6 +290,7 @@ class WC_Frontend_Scripts { 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ), 'update_shipping_method_nonce' => wp_create_nonce( "update-shipping-method" ), 'apply_coupon_nonce' => wp_create_nonce( "apply-coupon" ), + 'remove_coupon_nonce' => wp_create_nonce( "remove-coupon" ), ); break; case 'wc-cart-fragments' :