diff --git a/assets/js/frontend/checkout.js b/assets/js/frontend/checkout.js index 9bd76b438b2..40e76503b2c 100644 --- a/assets/js/frontend/checkout.js +++ b/assets/js/frontend/checkout.js @@ -401,6 +401,47 @@ jQuery( function( $ ) { return false; }); + $( '.woocommerce-checkout-review-order' ).on( 'click', '.woocommerce-remove-coupon', function( e ) { + e.preventDefault(); + + var container = $( this ).parents( '.woocommerce-checkout-review-order' ), + coupon = $( this ).data( 'coupon' ); + + container.addClass( 'processing' ).block({ + message: null, + overlayCSS: { + background: '#fff', + opacity: 0.6 + } + }); + + var data = { + action: 'woocommerce_remove_coupon', + security: wc_checkout_params.remove_coupon_nonce, + coupon: coupon + }; + + $.ajax({ + type: 'POST', + url: wc_checkout_params.ajax_url, + data: data, + success: function( code ) { + $( '.woocommerce-error, .woocommerce-message' ).remove(); + container.removeClass( 'processing' ).unblock(); + + if ( code ) { + container.before( code ); + + $( 'body' ).trigger( 'update_checkout' ); + + // remove coupon code from coupon field + $( 'form.checkout_coupon' ).find( 'input[name="coupon_code"]' ).val( '' ); + } + }, + dataType: 'html' + }); + }); + $( 'body' ) // Init trigger diff --git a/assets/js/frontend/checkout.min.js b/assets/js/frontend/checkout.min.js index 96d8aed856e..8773a555e69 100644 --- a/assets/js/frontend/checkout.min.js +++ b/assets/js/frontend/checkout.min.js @@ -1 +1 @@ -jQuery(function(a){function b(){e&&e.abort();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()});var c,d,f,g,h,i,j=a("#order_review input[name=payment_method]:checked").val(),k=a("#billing_country").val(),l=a("#billing_state").val(),m=a("input#billing_postcode").val(),n=a("#billing_city").val(),o=a("input#billing_address_1").val(),p=a("input#billing_address_2").val();a("#ship-to-different-address input").is(":checked")?(c=a("#shipping_country").val(),d=a("#shipping_state").val(),f=a("input#shipping_postcode").val(),g=a("#shipping_city").val(),h=a("input#shipping_address_1").val(),i=a("input#shipping_address_2").val()):(c=k,d=l,f=m,g=n,h=o,i=p),a(".woocommerce-checkout-payment, .woocommerce-checkout-review-order-table").block({message:null,overlayCSS:{background:"#fff",opacity:.6}});var q={action:"woocommerce_update_order_review",security:wc_checkout_params.update_order_review_nonce,shipping_method:b,payment_method:j,country:k,state:l,postcode:m,city:n,address:o,address_2:p,s_country:c,s_state:d,s_postcode:f,s_city:g,s_address:h,s_address_2:i,post_data:a("form.checkout").serialize()};e=a.ajax({type:"POST",url:wc_checkout_params.ajax_url,data:q,success:function(b){b&&b.fragments&&(a.each(b.fragments,function(b,c){a(b).replaceWith(c),a(b).unblock()}),a(".woocommerce-checkout").find("input[name=payment_method]:checked").trigger("click"),a("body").trigger("updated_checkout"))}})}function c(){var b=!0;a(f).size()&&($required_inputs=a(f).closest("div").find(".address-field.validate-required"),$required_inputs.size()&&$required_inputs.each(function(){""===a(this).find("input.input-text").val()&&(b=!1)})),b&&(f=!1,a("body").trigger("update_checkout"))}if(a.blockUI.defaults.overlayCSS.cursor="default","undefined"==typeof wc_checkout_params)return!1;var d,e,f=!1;a("body").bind("update_checkout",function(){clearTimeout(d),b()}),a(".checkout_coupon, div.shipping_address").hide(),a("a.showlogin").click(function(){return a("form.login").slideToggle(),!1}),a("a.showcoupon").click(function(){return a(".checkout_coupon").slideToggle(400,function(){a("#coupon_code").focus()}),!1}),a("#ship-to-different-address input").change(function(){a("div.shipping_address").hide(),a(this).is(":checked")&&a("div.shipping_address").slideDown()}).change(),"yes"===wc_checkout_params.option_guest_checkout&&(a("div.create-account").hide(),a("input#createaccount").change(function(){a("div.create-account").hide(),a(this).is(":checked")&&a("div.create-account").slideDown()}).change()),a("#order_review").on("click",".payment_methods input.input-radio",function(){if(a(".payment_methods input.input-radio").length>1){var b=a("div.payment_box."+a(this).attr("ID"));a(this).is(":checked")&&!b.is(":visible")&&(a("div.payment_box").filter(":visible").slideUp(250),a(this).is(":checked")&&a("div.payment_box."+a(this).attr("ID")).slideDown(250))}else a("div.payment_box").show();a("#place_order").val(a(this).data("order_button_text")?a(this).data("order_button_text"):a("#place_order").data("value"))}).find("input[name=payment_method]:checked").click(),a("form.checkout").on("input change","select.shipping_method, input[name^=shipping_method], #ship-to-different-address input, .update_totals_on_change select, .update_totals_on_change input[type=radio]",function(){clearTimeout(d),f=!1,a("body").trigger("update_checkout")}).on("change",".address-field input.input-text, .update_totals_on_change input.input-text",function(){f&&c()}).on("input change",".address-field select",function(){f=this,c()}).on("input keydown",".address-field input.input-text, .update_totals_on_change input.input-text",function(a){var b=a.keyCode||a.which||0;return 9===b?!0:(f=this,clearTimeout(d),void(d=setTimeout(c,"1000")))}).on("blur input change",".input-text, select",function(){var b=a(this),c=b.closest(".form-row"),d=!0;if(c.is(".validate-required")&&""===b.val()&&(c.removeClass("woocommerce-validated").addClass("woocommerce-invalid woocommerce-invalid-required-field"),d=!1),c.is(".validate-email")&&b.val()){var e=new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);e.test(b.val())||(c.removeClass("woocommerce-validated").addClass("woocommerce-invalid woocommerce-invalid-email"),d=!1)}d&&c.removeClass("woocommerce-invalid woocommerce-invalid-required-field").addClass("woocommerce-validated")}).submit(function(){clearTimeout(d);var b=a(this);if(b.is(".processing"))return!1;if(b.triggerHandler("checkout_place_order")!==!1&&b.triggerHandler("checkout_place_order_"+a("#order_review input[name=payment_method]:checked").val())!==!1){b.addClass("processing");var c=b.data();1!=c["blockUI.isBlocked"]&&b.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),a.ajax({type:"POST",url:wc_checkout_params.checkout_url,data:b.serialize(),success:function(c){var d="";try{if(c.indexOf("")>=0&&(c=c.split("")[1]),c.indexOf("")>=0&&(c=c.split("")[0]),d=a.parseJSON(c),"success"!==d.result)throw"failure"===d.result?"Result failure":"Invalid response";window.location=-1!=d.redirect.indexOf("https://")||-1!=d.redirect.indexOf("http://")?d.redirect:decodeURI(d.redirect)}catch(e){if("true"===d.reload)return void window.location.reload();a(".woocommerce-error, .woocommerce-message").remove(),b.prepend(d.messages?d.messages:c),b.removeClass("processing").unblock(),b.find(".input-text, select").blur(),a("html, body").animate({scrollTop:a("form.checkout").offset().top-100},1e3),"true"===d.refresh&&a("body").trigger("update_checkout"),a("body").trigger("checkout_error")}},dataType:"html"})}return!1}),a("form.checkout_coupon").submit(function(){var b=a(this);if(b.is(".processing"))return!1;b.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}});var c={action:"woocommerce_apply_coupon",security:wc_checkout_params.apply_coupon_nonce,coupon_code:b.find("input[name=coupon_code]").val()};return a.ajax({type:"POST",url:wc_checkout_params.ajax_url,data:c,success:function(c){a(".woocommerce-error, .woocommerce-message").remove(),b.removeClass("processing").unblock(),c&&(b.before(c),b.slideUp(),a("body").trigger("update_checkout"))},dataType:"html"}),!1}),a("body").bind("init_checkout",function(){a("#billing_country, #shipping_country, .country_to_state").change(),a("body").trigger("update_checkout")}),"1"===wc_checkout_params.is_checkout&&a("body").trigger("init_checkout")}); \ No newline at end of file +jQuery(function(e){function i(){if(r)r.abort();var t=[];e("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(n,r){t[e(this).data("index")]=e(this).val()});var n=e("#order_review input[name=payment_method]:checked").val(),i=e("#billing_country").val(),s=e("#billing_state").val(),o=e("input#billing_postcode").val(),u=e("#billing_city").val(),a=e("input#billing_address_1").val(),f=e("input#billing_address_2").val(),l,c,h,p,d,v;if(e("#ship-to-different-address input").is(":checked")){l=e("#shipping_country").val();c=e("#shipping_state").val();h=e("input#shipping_postcode").val();p=e("#shipping_city").val();d=e("input#shipping_address_1").val();v=e("input#shipping_address_2").val()}else{l=i;c=s;h=o;p=u;d=a;v=f}e(".woocommerce-checkout-payment, .woocommerce-checkout-review-order-table").block({message:null,overlayCSS:{background:"#fff",opacity:.6}});var m={action:"woocommerce_update_order_review",security:wc_checkout_params.update_order_review_nonce,shipping_method:t,payment_method:n,country:i,state:s,postcode:o,city:u,address:a,address_2:f,s_country:l,s_state:c,s_postcode:h,s_city:p,s_address:d,s_address_2:v,post_data:e("form.checkout").serialize()};r=e.ajax({type:"POST",url:wc_checkout_params.ajax_url,data:m,success:function(t){if(t&&t.fragments){e.each(t.fragments,function(t,n){e(t).replaceWith(n);e(t).unblock()});e(".woocommerce-checkout").find("input[name=payment_method]:checked").trigger("click");e("body").trigger("updated_checkout")}}})}function s(){var t=true;if(e(n).size()){$required_inputs=e(n).closest("div").find(".address-field.validate-required");if($required_inputs.size()){$required_inputs.each(function(){if(e(this).find("input.input-text").val()===""){t=false}})}}if(t){n=false;e("body").trigger("update_checkout")}}e.blockUI.defaults.overlayCSS.cursor="default";if(typeof wc_checkout_params==="undefined")return false;var t,n=false,r;e("body").bind("update_checkout",function(){clearTimeout(t);i()});e(".checkout_coupon, div.shipping_address").hide();e("a.showlogin").click(function(){e("form.login").slideToggle();return false});e("a.showcoupon").click(function(){e(".checkout_coupon").slideToggle(400,function(){e("#coupon_code").focus()});return false});e("#ship-to-different-address input").change(function(){e("div.shipping_address").hide();if(e(this).is(":checked")){e("div.shipping_address").slideDown()}}).change();if(wc_checkout_params.option_guest_checkout==="yes"){e("div.create-account").hide();e("input#createaccount").change(function(){e("div.create-account").hide();if(e(this).is(":checked")){e("div.create-account").slideDown()}}).change()}e("#order_review").on("click",".payment_methods input.input-radio",function(){if(e(".payment_methods input.input-radio").length>1){var t=e("div.payment_box."+e(this).attr("ID"));if(e(this).is(":checked")&&!t.is(":visible")){e("div.payment_box").filter(":visible").slideUp(250);if(e(this).is(":checked")){e("div.payment_box."+e(this).attr("ID")).slideDown(250)}}}else{e("div.payment_box").show()}if(e(this).data("order_button_text")){e("#place_order").val(e(this).data("order_button_text"))}else{e("#place_order").val(e("#place_order").data("value"))}}).find("input[name=payment_method]:checked").click();e("form.checkout").on("input change","select.shipping_method, input[name^=shipping_method], #ship-to-different-address input, .update_totals_on_change select, .update_totals_on_change input[type=radio]",function(){clearTimeout(t);n=false;e("body").trigger("update_checkout")}).on("change",".address-field input.input-text, .update_totals_on_change input.input-text",function(){if(n){s()}}).on("input change",".address-field select",function(){n=this;s()}).on("input keydown",".address-field input.input-text, .update_totals_on_change input.input-text",function(e){var r=e.keyCode||e.which||0;if(r===9){return true}n=this;clearTimeout(t);t=setTimeout(s,"1000")}).on("blur input change",".input-text, select",function(){var t=e(this),n=t.closest(".form-row"),r=true;if(n.is(".validate-required")){if(t.val()===""){n.removeClass("woocommerce-validated").addClass("woocommerce-invalid woocommerce-invalid-required-field");r=false}}if(n.is(".validate-email")){if(t.val()){var i=new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);if(!i.test(t.val())){n.removeClass("woocommerce-validated").addClass("woocommerce-invalid woocommerce-invalid-email");r=false}}}if(r){n.removeClass("woocommerce-invalid woocommerce-invalid-required-field").addClass("woocommerce-validated")}}).submit(function(){clearTimeout(t);var n=e(this);if(n.is(".processing")){return false}if(n.triggerHandler("checkout_place_order")!==false&&n.triggerHandler("checkout_place_order_"+e("#order_review input[name=payment_method]:checked").val())!==false){n.addClass("processing");var r=n.data();if(r["blockUI.isBlocked"]!=1){n.block({message:null,overlayCSS:{background:"#fff",opacity:.6}})}e.ajax({type:"POST",url:wc_checkout_params.checkout_url,data:n.serialize(),success:function(t){var r="";try{if(t.indexOf("")>=0)t=t.split("")[1];if(t.indexOf("")>=0)t=t.split("")[0];r=e.parseJSON(t);if(r.result==="success"){if(r.redirect.indexOf("https://")!=-1||r.redirect.indexOf("http://")!=-1){window.location=r.redirect}else{window.location=decodeURI(r.redirect)}}else if(r.result==="failure"){throw"Result failure"}else{throw"Invalid response"}}catch(i){if(r.reload==="true"){window.location.reload();return}e(".woocommerce-error, .woocommerce-message").remove();if(r.messages){n.prepend(r.messages)}else{n.prepend(t)}n.removeClass("processing").unblock();n.find(".input-text, select").blur();e("html, body").animate({scrollTop:e("form.checkout").offset().top-100},1e3);if(r.refresh==="true")e("body").trigger("update_checkout");e("body").trigger("checkout_error")}},dataType:"html"})}return false});e("form.checkout_coupon").submit(function(){var t=e(this);if(t.is(".processing"))return false;t.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}});var n={action:"woocommerce_apply_coupon",security:wc_checkout_params.apply_coupon_nonce,coupon_code:t.find("input[name=coupon_code]").val()};e.ajax({type:"POST",url:wc_checkout_params.ajax_url,data:n,success:function(n){e(".woocommerce-error, .woocommerce-message").remove();t.removeClass("processing").unblock();if(n){t.before(n);t.slideUp();e("body").trigger("update_checkout")}},dataType:"html"});return false});e(".woocommerce-checkout-review-order").on("click",".woocommerce-remove-coupon",function(t){t.preventDefault();var n=e(this).parents(".woocommerce-checkout-review-order"),r=e(this).data("coupon");n.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}});var i={action:"woocommerce_remove_coupon",security:wc_checkout_params.remove_coupon_nonce,coupon:r};e.ajax({type:"POST",url:wc_checkout_params.ajax_url,data:i,success:function(t){e(".woocommerce-error, .woocommerce-message").remove();n.removeClass("processing").unblock();if(t){n.before(t);e("body").trigger("update_checkout");e("form.checkout_coupon").find('input[name="coupon_code"]').val("")}},dataType:"html"})});e("body").bind("init_checkout",function(){e("#billing_country, #shipping_country, .country_to_state").change();e("body").trigger("update_checkout")});if(wc_checkout_params.is_checkout==="1"){e("body").trigger("init_checkout")}}) \ No newline at end of file diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index e48e0928eb5..238d79b0cd7 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -26,6 +26,7 @@ class WC_AJAX { $ajax_events = array( 'get_refreshed_fragments' => true, 'apply_coupon' => true, + 'remove_coupon' => true, 'update_shipping_method' => true, 'update_order_review' => true, 'add_to_cart' => true, @@ -118,6 +119,30 @@ class WC_AJAX { die(); } + /** + * AJAX remove coupon on cart and checkout page + */ + public static function remove_coupon() { + + check_ajax_referer( 'remove-coupon', 'security' ); + + $coupon = wc_clean( $_POST['coupon'] ); + + if ( ! isset( $coupon ) || empty( $coupon ) ) { + wc_add_notice( __( 'Sorry there was a problem removing this coupon.', 'woocommerce' ) ); + + } else { + + WC()->cart->remove_coupon( $coupon ); + + wc_add_notice( __( 'Coupon has been removed.', 'woocommerce' ) ); + } + + wc_print_notices(); + + die(); + } + /** * AJAX update shipping method on cart page */ diff --git a/includes/class-wc-frontend-scripts.php b/includes/class-wc-frontend-scripts.php index fc0cb3883df..f4dd948616e 100644 --- a/includes/class-wc-frontend-scripts.php +++ b/includes/class-wc-frontend-scripts.php @@ -202,8 +202,9 @@ class WC_Frontend_Scripts { case 'wc-checkout' : return array( 'ajax_url' => WC()->ajax_url(), - 'update_order_review_nonce' => wp_create_nonce( "update-order-review" ), - 'apply_coupon_nonce' => wp_create_nonce( "apply-coupon" ), + 'update_order_review_nonce' => wp_create_nonce( 'update-order-review' ), + 'apply_coupon_nonce' => wp_create_nonce( 'apply-coupon' ), + 'remove_coupon_nonce' => wp_create_nonce( 'remove-coupon' ), 'option_guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ), 'checkout_url' => add_query_arg( 'action', 'woocommerce_checkout', WC()->ajax_url() ), 'is_checkout' => is_page( wc_get_page_id( 'checkout' ) ) && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0 diff --git a/includes/wc-cart-functions.php b/includes/wc-cart-functions.php index bdf3a055dfb..5fcd10585b6 100644 --- a/includes/wc-cart-functions.php +++ b/includes/wc-cart-functions.php @@ -219,7 +219,7 @@ function wc_cart_totals_coupon_html( $coupon ) { // get rid of empty array elements $value = array_filter( $value ); - $value = implode( ', ', $value ) . ' ' . __( '[Remove]', 'woocommerce' ) . ''; + $value = implode( ', ', $value ) . ' ' . __( '[Remove]', 'woocommerce' ) . ''; echo apply_filters( 'woocommerce_cart_totals_coupon_html', $value, $coupon ); }