From 81d186000d952f001e750273440ff7e5dbe4cd6d Mon Sep 17 00:00:00 2001 From: Kevin Killingsworth Date: Thu, 22 Sep 2016 16:52:23 -0400 Subject: [PATCH] Fix cart update field enter on submit for IE Fix for issue #11811 https://github.com/woothemes/woocommerce/issues/11811 IE handles hitting on an input field differently than Chrome or Firefox in that it finds the nearest submit button and fires the event from it instead of the actual text field from which the user hit . This commit catches the key event before the submit happens and forces the cart_submit handling on that event instead. --- assets/js/frontend/cart.js | 32 +++++++++++++++++++++++++++++++- assets/js/frontend/cart.min.js | 2 +- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/assets/js/frontend/cart.js b/assets/js/frontend/cart.js index 84d61c8ff3f..f201fcb5e57 100644 --- a/assets/js/frontend/cart.js +++ b/assets/js/frontend/cart.js @@ -267,6 +267,7 @@ jQuery( function( $ ) { */ init: function() { this.update_cart_totals = this.update_cart_totals.bind( this ); + this.input_keypress = this.input_keypress.bind( this ); this.cart_submit = this.cart_submit.bind( this ); this.submit_click = this.submit_click.bind( this ); this.apply_coupon = this.apply_coupon.bind( this ); @@ -282,6 +283,10 @@ jQuery( function( $ ) { 'click', 'div.woocommerce > form input[type=submit]', this.submit_click ); + $( document ).on( + 'keypress', + 'div.woocommerce > form input[type=number]', + this.input_keypress ); $( document ).on( 'submit', 'div.woocommerce:not(.widget_product_search) > form', @@ -352,15 +357,40 @@ jQuery( function( $ ) { } ); }, + /** + * Handle the key for quantity fields. + * + * @param {Object} evt The JQuery event + * + * For IE, if you hit enter on a quantity field, it makes the + * document.activeElement the first submit button it finds. + * For us, that is the Apply Coupon button. This is required + * to catch the event before that happens. + */ + input_keypress: function( evt ) { + + // Catch the enter key and don't let it submit the form. + if ( 13 === evt.keyCode ) { + evt.preventDefault(); + this.cart_submit( evt ); + } + }, + /** * Handle cart form submit and route to correct logic. * * @param {Object} evt The JQuery event */ cart_submit: function( evt ) { - var $form = $( evt.currentTarget ); var $submit = $( document.activeElement ); var $clicked = $( 'input[type=submit][clicked=true]' ); + var $form = $( evt.currentTarget ); + + // For submit events, currentTarget is form. + // For keypress events, currentTarget is input. + if ( ! $form.is( 'form' ) ) { + $form = $( evt.currentTarget ).parents( 'form' ); + } if ( 0 === $form.find( '.shop_table.cart' ).length ) { return; diff --git a/assets/js/frontend/cart.min.js b/assets/js/frontend/cart.min.js index ea677a71dfe..c58fb62c973 100644 --- a/assets/js/frontend/cart.min.js +++ b/assets/js/frontend/cart.min.js @@ -1 +1 @@ -jQuery(function(a){if("undefined"==typeof wc_cart_params)return!1;var b=function(a){return wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%",a)},c=function(a){return a.is(".processing")||a.parents(".processing").length},d=function(a){c(a)||a.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},e=function(a){a.removeClass("processing").unblock()},f=function(b){var c=a.parseHTML(b),d=a(".shop_table.cart",c).closest("form"),e=a(".cart_totals",c),f=a(".woocommerce-error",c),i=a(".woocommerce-message",c),j=a(".woocommerce-info",c);if(a(".woocommerce-error, .woocommerce-message, .woocommerce-info").remove(),0===d.length){if(a(".woocommerce-checkout").length)return void window.location.reload();var k=a(".cart-empty",c).closest(".woocommerce");a(".shop_table.cart").closest(".woocommerce").replaceWith(k),f.length>0&&h(f,a(".cart-empty").closest(".woocommerce")),i.length>0&&h(i,a(".cart-empty").closest(".woocommerce")),j.length>0&&h(j,a(".cart-empty").closest(".woocommerce"))}else a(".woocommerce-checkout").length&&a(document.body).trigger("update_checkout"),a(".shop_table.cart").closest("form").replaceWith(d),a(".shop_table.cart").closest("form").find('input[name="update_cart"]').prop("disabled",!0),f.length>0&&h(f),i.length>0&&h(i),j.length>0&&h(j),g(e);a(document.body).trigger("updated_wc_div")},g=function(b){a(".cart_totals").replaceWith(b),a(document.body).trigger("updated_cart_totals")},h=function(b,c){c||(c=a(".shop_table.cart").closest("form")),a(".woocommerce-error, .woocommerce-message, .woocommerce-info").remove(),c.before(b)},i={init:function(b){this.cart=b,this.toggle_shipping=this.toggle_shipping.bind(this),this.shipping_method_selected=this.shipping_method_selected.bind(this),this.shipping_calculator_submit=this.shipping_calculator_submit.bind(this),a(document).on("click",".shipping-calculator-button",this.toggle_shipping),a(document).on("change","select.shipping_method, input[name^=shipping_method]",this.shipping_method_selected),a(document).on("submit","form.woocommerce-shipping-calculator",this.shipping_calculator_submit),a(".shipping-calculator-form").hide()},toggle_shipping:function(){return a(".shipping-calculator-form").slideToggle("slow"),!1},shipping_method_selected:function(c){var f=c.currentTarget,h={};a("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(){h[a(f).data("index")]=a(f).val()}),d(a("div.cart_totals"));var i={security:wc_cart_params.update_shipping_method_nonce,shipping_method:h};a.ajax({type:"post",url:b("update_shipping_method"),data:i,dataType:"html",success:function(a){g(a)},complete:function(){e(a("div.cart_totals")),a(document.body).trigger("updated_shipping_method")}})},shipping_calculator_submit:function(b){b.preventDefault();var c=a(b.currentTarget);d(a("div.cart_totals")),d(c),a("").attr("type","hidden").attr("name","calc_shipping").attr("value","x").appendTo(c),a.ajax({type:c.attr("method"),url:c.attr("action"),data:c.serialize(),dataType:"html",success:function(a){f(a)},complete:function(){e(c),e(a("div.cart_totals"))}})}},j={init:function(){this.update_cart_totals=this.update_cart_totals.bind(this),this.cart_submit=this.cart_submit.bind(this),this.submit_click=this.submit_click.bind(this),this.apply_coupon=this.apply_coupon.bind(this),this.remove_coupon_clicked=this.remove_coupon_clicked.bind(this),this.quantity_update=this.quantity_update.bind(this),this.item_remove_clicked=this.item_remove_clicked.bind(this),this.update_cart=this.update_cart.bind(this),a(document).on("wc_update_cart",this.update_cart),a(document).on("click","div.woocommerce > form input[type=submit]",this.submit_click),a(document).on("submit","div.woocommerce:not(.widget_product_search) > form",this.cart_submit),a(document).on("click","a.woocommerce-remove-coupon",this.remove_coupon_clicked),a(document).on("click","td.product-remove > a",this.item_remove_clicked),a(document).on("change input","div.woocommerce > form .cart_item :input",this.input_changed),a('div.woocommerce > form input[name="update_cart"]').prop("disabled",!0)},input_changed:function(){a('div.woocommerce > form input[name="update_cart"]').prop("disabled",!1)},update_cart:function(){var b=a(".shop_table.cart").closest("form");d(b),d(a("div.cart_totals")),a.ajax({type:b.attr("method"),url:b.attr("action"),data:b.serialize(),dataType:"html",success:function(a){f(a)},complete:function(){e(b),e(a("div.cart_totals"))}})},update_cart_totals:function(){d(a("div.cart_totals")),a.ajax({url:b("get_cart_totals"),dataType:"html",success:function(a){g(a)},complete:function(){e(a("div.cart_totals"))}})},cart_submit:function(b){var d=a(b.currentTarget),e=a(document.activeElement),f=a("input[type=submit][clicked=true]");if(0!==d.find(".shop_table.cart").length)return!c(d)&&void(f.is('[name="update_cart"]')||e.is("input.qty")?(b.preventDefault(),this.quantity_update(d)):(f.is('[name="apply_coupon"]')||e.is("#coupon_code"))&&(b.preventDefault(),this.apply_coupon(d)))},submit_click:function(b){a("input[type=submit]",a(b.target).parents("form")).removeAttr("clicked"),a(b.target).attr("clicked","true")},apply_coupon:function(c){d(c);var f=this,g=a("#coupon_code"),i=g.val(),j={security:wc_cart_params.apply_coupon_nonce,coupon_code:i};a.ajax({type:"POST",url:b("apply_coupon"),data:j,dataType:"html",success:function(b){h(b),a(document.body).trigger("applied_coupon")},complete:function(){e(c),g.val(""),f.update_cart_totals()}})},remove_coupon_clicked:function(c){c.preventDefault();var f=this,g=a(c.currentTarget).parents("tr"),i=a(c.currentTarget).attr("data-coupon");d(g.parents("table"));var j={security:wc_cart_params.remove_coupon_nonce,coupon:i};a.ajax({type:"POST",url:b("remove_coupon"),data:j,dataType:"html",success:function(b){h(b),a(document.body).trigger("removed_coupon"),e(g.parents("table"))},complete:function(){f.update_cart_totals()}})},quantity_update:function(b){a("").attr("type","hidden").attr("name","update_cart").attr("value","Update Cart").appendTo(b),d(b),d(a("div.cart_totals")),a.ajax({type:b.attr("method"),url:b.attr("action"),data:b.serialize(),dataType:"html",success:f,complete:function(){e(b),e(a("div.cart_totals"))}})},item_remove_clicked:function(b){b.preventDefault();var c=a(b.currentTarget),g=c.parents("form");d(g),d(a("div.cart_totals")),a.ajax({type:"GET",url:c.attr("href"),dataType:"html",success:f,complete:function(){e(g),e(a("div.cart_totals"))}})}};i.init(j),j.init()}); \ No newline at end of file +jQuery(function(a){if("undefined"==typeof wc_cart_params)return!1;var b=function(a){return wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%",a)},c=function(a){return a.is(".processing")||a.parents(".processing").length},d=function(a){c(a)||a.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},e=function(a){a.removeClass("processing").unblock()},f=function(b){var c=a.parseHTML(b),d=a(".shop_table.cart",c).closest("form"),e=a(".cart_totals",c),f=a(".woocommerce-error",c),i=a(".woocommerce-message",c),j=a(".woocommerce-info",c);if(a(".woocommerce-error, .woocommerce-message, .woocommerce-info").remove(),0===d.length){if(a(".woocommerce-checkout").length)return void window.location.reload();var k=a(".cart-empty",c).closest(".woocommerce");a(".shop_table.cart").closest(".woocommerce").replaceWith(k),f.length>0&&h(f,a(".cart-empty").closest(".woocommerce")),i.length>0&&h(i,a(".cart-empty").closest(".woocommerce")),j.length>0&&h(j,a(".cart-empty").closest(".woocommerce"))}else a(".woocommerce-checkout").length&&a(document.body).trigger("update_checkout"),a(".shop_table.cart").closest("form").replaceWith(d),a(".shop_table.cart").closest("form").find('input[name="update_cart"]').prop("disabled",!0),f.length>0&&h(f),i.length>0&&h(i),j.length>0&&h(j),g(e);a(document.body).trigger("updated_wc_div")},g=function(b){a(".cart_totals").replaceWith(b),a(document.body).trigger("updated_cart_totals")},h=function(b,c){c||(c=a(".shop_table.cart").closest("form")),a(".woocommerce-error, .woocommerce-message, .woocommerce-info").remove(),c.before(b)},i={init:function(b){this.cart=b,this.toggle_shipping=this.toggle_shipping.bind(this),this.shipping_method_selected=this.shipping_method_selected.bind(this),this.shipping_calculator_submit=this.shipping_calculator_submit.bind(this),a(document).on("click",".shipping-calculator-button",this.toggle_shipping),a(document).on("change","select.shipping_method, input[name^=shipping_method]",this.shipping_method_selected),a(document).on("submit","form.woocommerce-shipping-calculator",this.shipping_calculator_submit),a(".shipping-calculator-form").hide()},toggle_shipping:function(){return a(".shipping-calculator-form").slideToggle("slow"),!1},shipping_method_selected:function(c){var f=c.currentTarget,h={};a("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(){h[a(f).data("index")]=a(f).val()}),d(a("div.cart_totals"));var i={security:wc_cart_params.update_shipping_method_nonce,shipping_method:h};a.ajax({type:"post",url:b("update_shipping_method"),data:i,dataType:"html",success:function(a){g(a)},complete:function(){e(a("div.cart_totals")),a(document.body).trigger("updated_shipping_method")}})},shipping_calculator_submit:function(b){b.preventDefault();var c=a(b.currentTarget);d(a("div.cart_totals")),d(c),a("").attr("type","hidden").attr("name","calc_shipping").attr("value","x").appendTo(c),a.ajax({type:c.attr("method"),url:c.attr("action"),data:c.serialize(),dataType:"html",success:function(a){f(a)},complete:function(){e(c),e(a("div.cart_totals"))}})}},j={init:function(){this.update_cart_totals=this.update_cart_totals.bind(this),this.input_keypress=this.input_keypress.bind(this),this.cart_submit=this.cart_submit.bind(this),this.submit_click=this.submit_click.bind(this),this.apply_coupon=this.apply_coupon.bind(this),this.remove_coupon_clicked=this.remove_coupon_clicked.bind(this),this.quantity_update=this.quantity_update.bind(this),this.item_remove_clicked=this.item_remove_clicked.bind(this),this.update_cart=this.update_cart.bind(this),a(document).on("wc_update_cart",this.update_cart),a(document).on("click","div.woocommerce > form input[type=submit]",this.submit_click),a(document).on("keypress","div.woocommerce > form input[type=number]",this.input_keypress),a(document).on("submit","div.woocommerce:not(.widget_product_search) > form",this.cart_submit),a(document).on("click","a.woocommerce-remove-coupon",this.remove_coupon_clicked),a(document).on("click","td.product-remove > a",this.item_remove_clicked),a(document).on("change input","div.woocommerce > form .cart_item :input",this.input_changed),a('div.woocommerce > form input[name="update_cart"]').prop("disabled",!0)},input_changed:function(){a('div.woocommerce > form input[name="update_cart"]').prop("disabled",!1)},update_cart:function(){var b=a(".shop_table.cart").closest("form");d(b),d(a("div.cart_totals")),a.ajax({type:b.attr("method"),url:b.attr("action"),data:b.serialize(),dataType:"html",success:function(a){f(a)},complete:function(){e(b),e(a("div.cart_totals"))}})},update_cart_totals:function(){d(a("div.cart_totals")),a.ajax({url:b("get_cart_totals"),dataType:"html",success:function(a){g(a)},complete:function(){e(a("div.cart_totals"))}})},input_keypress:function(a){13===a.keyCode&&(a.preventDefault(),this.cart_submit(a))},cart_submit:function(b){var d=a(document.activeElement),e=a("input[type=submit][clicked=true]"),f=a(b.currentTarget);if(f.is("form")||(f=a(b.currentTarget).parents("form")),0!==f.find(".shop_table.cart").length)return!c(f)&&void(e.is('[name="update_cart"]')||d.is("input.qty")?(b.preventDefault(),this.quantity_update(f)):(e.is('[name="apply_coupon"]')||d.is("#coupon_code"))&&(b.preventDefault(),this.apply_coupon(f)))},submit_click:function(b){a("input[type=submit]",a(b.target).parents("form")).removeAttr("clicked"),a(b.target).attr("clicked","true")},apply_coupon:function(c){d(c);var f=this,g=a("#coupon_code"),i=g.val(),j={security:wc_cart_params.apply_coupon_nonce,coupon_code:i};a.ajax({type:"POST",url:b("apply_coupon"),data:j,dataType:"html",success:function(b){h(b),a(document.body).trigger("applied_coupon")},complete:function(){e(c),g.val(""),f.update_cart_totals()}})},remove_coupon_clicked:function(c){c.preventDefault();var f=this,g=a(c.currentTarget).parents("tr"),i=a(c.currentTarget).attr("data-coupon");d(g.parents("table"));var j={security:wc_cart_params.remove_coupon_nonce,coupon:i};a.ajax({type:"POST",url:b("remove_coupon"),data:j,dataType:"html",success:function(b){h(b),a(document.body).trigger("removed_coupon"),e(g.parents("table"))},complete:function(){f.update_cart_totals()}})},quantity_update:function(b){a("").attr("type","hidden").attr("name","update_cart").attr("value","Update Cart").appendTo(b),d(b),d(a("div.cart_totals")),a.ajax({type:b.attr("method"),url:b.attr("action"),data:b.serialize(),dataType:"html",success:f,complete:function(){e(b),e(a("div.cart_totals"))}})},item_remove_clicked:function(b){b.preventDefault();var c=a(b.currentTarget),g=c.parents("form");d(g),d(a("div.cart_totals")),a.ajax({type:"GET",url:c.attr("href"),dataType:"html",success:f,complete:function(){e(g),e(a("div.cart_totals"))}})}};i.init(j),j.init()}); \ No newline at end of file