diff --git a/assets/js/frontend/checkout.js b/assets/js/frontend/checkout.js index 66e671bbc67..502655b6fd8 100644 --- a/assets/js/frontend/checkout.js +++ b/assets/js/frontend/checkout.js @@ -25,11 +25,14 @@ jQuery( function( $ ) { this.$order_review.on( 'click', 'input[name="payment_method"]', this.payment_method_selected ); } + // Prevent HTML5 validation which can conflict. + this.$checkout_form.attr( 'novalidate', 'novalidate' ); + // Form submission this.$checkout_form.on( 'submit', this.submit ); // Inline validation - this.$checkout_form.on( 'blur change', '.input-text, select, input:checkbox', this.validate_field ); + this.$checkout_form.on( 'input blur change', '.input-text, select, input:checkbox', this.validate_field ); // Manual trigger this.$checkout_form.on( 'update', this.trigger_update_checkout ); @@ -104,6 +107,8 @@ jQuery( function( $ ) { $( 'div.create-account' ).hide(); if ( $( this ).is( ':checked' ) ) { + // Ensure password is not pre-populated. + $( '#account_password' ).val( '' ).change(); $( 'div.create-account' ).slideDown(); } }, @@ -163,36 +168,45 @@ jQuery( function( $ ) { reset_update_checkout_timer: function() { clearTimeout( wc_checkout_form.updateTimer ); }, - validate_field: function() { - var $this = $( this ), - $parent = $this.closest( '.form-row' ), - validated = true; + validate_field: function( e ) { + var $this = $( this ), + $parent = $this.closest( '.form-row' ), + validated = true, + validate_required = $parent.is( '.validate-required' ), + validate_email = $parent.is( '.validate-email' ), + event_type = e.type; - if ( $parent.is( '.validate-required' ) ) { - if ( 'checkbox' === $this.attr( 'type' ) && ! $this.is( ':checked' ) ) { - $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' ); - validated = false; - } else if ( $this.val() === '' ) { - $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' ); - validated = false; - } + if ( 'input' === event_type ) { + $parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email woocommerce-validated' ); } - if ( $parent.is( '.validate-email' ) ) { - if ( $this.val() ) { + if ( 'focusout' === event_type || 'change' === event_type ) { - /* https://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex */ - var pattern = 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 ( ! pattern.test( $this.val() ) ) { - $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-email' ); + if ( validate_required ) { + if ( 'checkbox' === $this.attr( 'type' ) && ! $this.is( ':checked' ) ) { + $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' ); + validated = false; + } else if ( $this.val() === '' ) { + $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' ); validated = false; } } - } - if ( validated ) { - $parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field' ).addClass( 'woocommerce-validated' ); + if ( validate_email ) { + if ( $this.val() ) { + /* https://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex */ + var pattern = 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 ( ! pattern.test( $this.val() ) ) { + $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-email' ); + validated = false; + } + } + } + + if ( validated ) { + $parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email' ).addClass( 'woocommerce-validated' ); + } } }, update_checkout: function( event, args ) { diff --git a/assets/js/frontend/checkout.min.js b/assets/js/frontend/checkout.min.js index ad762a2d43a..80741288692 100644 --- a/assets/js/frontend/checkout.min.js +++ b/assets/js/frontend/checkout.min.js @@ -1 +1 @@ -jQuery(function(a){if("undefined"==typeof wc_checkout_params)return!1;a.blockUI.defaults.overlayCSS.cursor="default";var b={updateTimer:!1,dirtyInput:!1,xhr:!1,$order_review:a("#order_review"),$checkout_form:a("form.checkout"),init:function(){a(document.body).bind("update_checkout",this.update_checkout),a(document.body).bind("init_checkout",this.init_checkout),this.$checkout_form.on("click",'input[name="payment_method"]',this.payment_method_selected),a(document.body).hasClass("woocommerce-order-pay")&&this.$order_review.on("click",'input[name="payment_method"]',this.payment_method_selected),this.$checkout_form.on("submit",this.submit),this.$checkout_form.on("blur change",".input-text, select, input:checkbox",this.validate_field),this.$checkout_form.on("update",this.trigger_update_checkout),this.$checkout_form.on("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"], .update_totals_on_change input[type="checkbox"]',this.trigger_update_checkout),this.$checkout_form.on("change",".address-field select",this.input_changed),this.$checkout_form.on("change",".address-field input.input-text, .update_totals_on_change input.input-text",this.maybe_input_changed),this.$checkout_form.on("change keydown",".address-field input.input-text, .update_totals_on_change input.input-text",this.queue_update_checkout),this.$checkout_form.on("change","#ship-to-different-address input",this.ship_to_different_address),this.$checkout_form.find("#ship-to-different-address input").change(),this.init_payment_methods(),"1"===wc_checkout_params.is_checkout&&a(document.body).trigger("init_checkout"),"yes"===wc_checkout_params.option_guest_checkout&&a("input#createaccount").change(this.toggle_create_account).change()},init_payment_methods:function(b){var c=a(".woocommerce-checkout").find('input[name="payment_method"]');1===c.length&&c.eq(0).hide(),b&&a("#"+b).prop("checked",!0),0===c.filter(":checked").length&&c.eq(0).prop("checked",!0),c.filter(":checked").eq(0).trigger("click")},get_payment_method:function(){return b.$checkout_form.find('input[name="payment_method"]:checked').val()},payment_method_selected: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(this).data("order_button_text")?a("#place_order").val(a(this).data("order_button_text")):a("#place_order").val(a("#place_order").data("value"))},toggle_create_account:function(){a("div.create-account").hide(),a(this).is(":checked")&&a("div.create-account").slideDown()},init_checkout:function(){a("#billing_country, #shipping_country, .country_to_state").change(),a(document.body).trigger("update_checkout")},maybe_input_changed:function(a){b.dirtyInput&&b.input_changed(a)},input_changed:function(a){b.dirtyInput=a.target,b.maybe_update_checkout()},queue_update_checkout:function(a){var c=a.keyCode||a.which||0;return 9===c||(b.dirtyInput=this,b.reset_update_checkout_timer(),void(b.updateTimer=setTimeout(b.maybe_update_checkout,"1000")))},trigger_update_checkout:function(){b.reset_update_checkout_timer(),b.dirtyInput=!1,a(document.body).trigger("update_checkout")},maybe_update_checkout:function(){var c=!0;if(a(b.dirtyInput).length){var d=a(b.dirtyInput).closest("div").find(".address-field.validate-required");d.length&&d.each(function(){""===a(this).find("input.input-text").val()&&(c=!1)})}c&&b.trigger_update_checkout()},ship_to_different_address:function(){a("div.shipping_address").hide(),a(this).is(":checked")&&a("div.shipping_address").slideDown()},reset_update_checkout_timer:function(){clearTimeout(b.updateTimer)},validate_field:function(){var b=a(this),c=b.closest(".form-row"),d=!0;if(c.is(".validate-required")&&("checkbox"!==b.attr("type")||b.is(":checked")?""===b.val()&&(c.removeClass("woocommerce-validated").addClass("woocommerce-invalid woocommerce-invalid-required-field"),d=!1):(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")},update_checkout:function(a,c){b.reset_update_checkout_timer(),b.updateTimer=setTimeout(b.update_checkout_action,"5",c)},update_checkout_action:function(c){if(b.xhr&&b.xhr.abort(),0!==a("form.checkout").length){c="undefined"!=typeof c?c:{update_shipping_method:!0};var d=a("#billing_country").val(),e=a("#billing_state").val(),f=a("input#billing_postcode").val(),g=a("#billing_city").val(),h=a("input#billing_address_1").val(),i=a("input#billing_address_2").val(),j=d,k=e,l=f,m=g,n=h,o=i;a("#ship-to-different-address").find("input").is(":checked")&&(j=a("#shipping_country").val(),k=a("#shipping_state").val(),l=a("input#shipping_postcode").val(),m=a("#shipping_city").val(),n=a("input#shipping_address_1").val(),o=a("input#shipping_address_2").val());var p={security:wc_checkout_params.update_order_review_nonce,payment_method:b.get_payment_method(),country:d,state:e,postcode:f,city:g,address:h,address_2:i,s_country:j,s_state:k,s_postcode:l,s_city:m,s_address:n,s_address_2:o,post_data:a("form.checkout").serialize()};if(!1!==c.update_shipping_method){var q={};a('select.shipping_method, input[name^="shipping_method"][type="radio"]:checked, input[name^="shipping_method"][type="hidden"]').each(function(){q[a(this).data("index")]=a(this).val()}),p.shipping_method=q}a(".woocommerce-checkout-payment, .woocommerce-checkout-review-order-table").block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),b.xhr=a.ajax({type:"POST",url:wc_checkout_params.wc_ajax_url.toString().replace("%%endpoint%%","update_order_review"),data:p,success:function(c){var d=a('.woocommerce-checkout input[name="payment_method"]:checked').attr("id");if("true"===c.reload)return void window.location.reload();a(".woocommerce-NoticeGroup-updateOrderReview").remove();var e=a("#terms").prop("checked"),f={};if(a(".payment_box input").each(function(){var b=a(this).attr("id");b&&(a.inArray(a(this).attr("type"),["checkbox","radio"])!==-1?f[b]=a(this).prop("checked"):f[b]=a(this).val())}),c&&c.fragments&&a.each(c.fragments,function(b,c){a(b).replaceWith(c),a(b).unblock()}),e&&a("#terms").prop("checked",!0),a.isEmptyObject(f)||a(".payment_box input").each(function(){var b=a(this).attr("id");b&&(a.inArray(a(this).attr("type"),["checkbox","radio"])!==-1?a(this).prop("checked",f[b]).change():a(this).val(f[b]).change())}),"failure"===c.result){var g=a("form.checkout");a(".woocommerce-error, .woocommerce-message").remove(),c.messages?g.prepend('
'+c.messages+"
"):g.prepend(c),g.find(".input-text, select, input:checkbox").blur(),a("html, body").animate({scrollTop:a("form.checkout").offset().top-100},1e3)}b.init_payment_methods(d),a(document.body).trigger("updated_checkout",[c])}})}},submit:function(){b.reset_update_checkout_timer();var c=a(this);if(c.is(".processing"))return!1;if(c.triggerHandler("checkout_place_order")!==!1&&c.triggerHandler("checkout_place_order_"+b.get_payment_method())!==!1){c.addClass("processing");var d=c.data();1!==d["blockUI.isBlocked"]&&c.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),a.ajaxSetup({dataFilter:function(b,c){if("json"!==c)return b;try{var d=a.parseJSON(b);if(d&&"object"==typeof d)return b}catch(a){var e=b.match(/{"result.*"}/);null===e?console.log("Unable to fix malformed JSON"):(console.log("Fixed malformed JSON. Original:"),console.log(b),b=e[0])}return b}}),a.ajax({type:"POST",url:wc_checkout_params.checkout_url,data:c.serialize(),dataType:"json",success:function(c){try{if("success"!==c.result)throw"failure"===c.result?"Result failure":"Invalid response";-1===c.redirect.indexOf("https://")||-1===c.redirect.indexOf("http://")?window.location=c.redirect:window.location=decodeURI(c.redirect)}catch(d){if("true"===c.reload)return void window.location.reload();"true"===c.refresh&&a(document.body).trigger("update_checkout"),c.messages?b.submit_error(c.messages):b.submit_error('
'+wc_checkout_params.i18n_checkout_error+"
")}},error:function(a,c,d){b.submit_error('
'+d+"
")}})}return!1},submit_error:function(c){a(".woocommerce-error, .woocommerce-message").remove(),b.$checkout_form.prepend('
'+c+"
"),b.$checkout_form.removeClass("processing").unblock(),b.$checkout_form.find(".input-text, select, input:checkbox").blur(),a("html, body").animate({scrollTop:a("form.checkout").offset().top-100},1e3),a(document.body).trigger("checkout_error")}},c={init:function(){a(document.body).on("click","a.showcoupon",this.show_coupon_form),a(document.body).on("click",".woocommerce-remove-coupon",this.remove_coupon),a("form.checkout_coupon").hide().submit(this.submit)},show_coupon_form:function(){return a(".checkout_coupon").slideToggle(400,function(){a(".checkout_coupon").find(":input:eq(0)").focus()}),!1},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={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.wc_ajax_url.toString().replace("%%endpoint%%","apply_coupon"),data:c,success:function(c){a(".woocommerce-error, .woocommerce-message").remove(),b.removeClass("processing").unblock(),c&&(b.before(c),b.slideUp(),a(document.body).trigger("update_checkout",{update_shipping_method:!1}))},dataType:"html"}),!1},remove_coupon:function(b){b.preventDefault();var c=a(this).parents(".woocommerce-checkout-review-order"),d=a(this).data("coupon");c.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}});var e={security:wc_checkout_params.remove_coupon_nonce,coupon:d};a.ajax({type:"POST",url:wc_checkout_params.wc_ajax_url.toString().replace("%%endpoint%%","remove_coupon"),data:e,success:function(b){a(".woocommerce-error, .woocommerce-message").remove(),c.removeClass("processing").unblock(),b&&(a("form.woocommerce-checkout").before(b),a(document.body).trigger("update_checkout",{update_shipping_method:!1}),a("form.checkout_coupon").find('input[name="coupon_code"]').val(""))},error:function(a){wc_checkout_params.debug_mode&&console.log(a.responseText)},dataType:"html"})}},d={init:function(){a(document.body).on("click","a.showlogin",this.show_login_form)},show_login_form:function(){return a("form.login, form.woocommerce-form--login").slideToggle(),!1}};b.init(),c.init(),d.init()}); \ No newline at end of file +jQuery(function(a){if("undefined"==typeof wc_checkout_params)return!1;a.blockUI.defaults.overlayCSS.cursor="default";var b={updateTimer:!1,dirtyInput:!1,xhr:!1,$order_review:a("#order_review"),$checkout_form:a("form.checkout"),init:function(){a(document.body).bind("update_checkout",this.update_checkout),a(document.body).bind("init_checkout",this.init_checkout),this.$checkout_form.on("click",'input[name="payment_method"]',this.payment_method_selected),a(document.body).hasClass("woocommerce-order-pay")&&this.$order_review.on("click",'input[name="payment_method"]',this.payment_method_selected),this.$checkout_form.attr("novalidate","novalidate"),this.$checkout_form.on("submit",this.submit),this.$checkout_form.on("input blur change",".input-text, select, input:checkbox",this.validate_field),this.$checkout_form.on("update",this.trigger_update_checkout),this.$checkout_form.on("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"], .update_totals_on_change input[type="checkbox"]',this.trigger_update_checkout),this.$checkout_form.on("change",".address-field select",this.input_changed),this.$checkout_form.on("change",".address-field input.input-text, .update_totals_on_change input.input-text",this.maybe_input_changed),this.$checkout_form.on("change keydown",".address-field input.input-text, .update_totals_on_change input.input-text",this.queue_update_checkout),this.$checkout_form.on("change","#ship-to-different-address input",this.ship_to_different_address),this.$checkout_form.find("#ship-to-different-address input").change(),this.init_payment_methods(),"1"===wc_checkout_params.is_checkout&&a(document.body).trigger("init_checkout"),"yes"===wc_checkout_params.option_guest_checkout&&a("input#createaccount").change(this.toggle_create_account).change()},init_payment_methods:function(b){var c=a(".woocommerce-checkout").find('input[name="payment_method"]');1===c.length&&c.eq(0).hide(),b&&a("#"+b).prop("checked",!0),0===c.filter(":checked").length&&c.eq(0).prop("checked",!0),c.filter(":checked").eq(0).trigger("click")},get_payment_method:function(){return b.$checkout_form.find('input[name="payment_method"]:checked').val()},payment_method_selected: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(this).data("order_button_text")?a("#place_order").val(a(this).data("order_button_text")):a("#place_order").val(a("#place_order").data("value"))},toggle_create_account:function(){a("div.create-account").hide(),a(this).is(":checked")&&(a("#account_password").val("").change(),a("div.create-account").slideDown())},init_checkout:function(){a("#billing_country, #shipping_country, .country_to_state").change(),a(document.body).trigger("update_checkout")},maybe_input_changed:function(a){b.dirtyInput&&b.input_changed(a)},input_changed:function(a){b.dirtyInput=a.target,b.maybe_update_checkout()},queue_update_checkout:function(a){var c=a.keyCode||a.which||0;return 9===c||(b.dirtyInput=this,b.reset_update_checkout_timer(),void(b.updateTimer=setTimeout(b.maybe_update_checkout,"1000")))},trigger_update_checkout:function(){b.reset_update_checkout_timer(),b.dirtyInput=!1,a(document.body).trigger("update_checkout")},maybe_update_checkout:function(){var c=!0;if(a(b.dirtyInput).length){var d=a(b.dirtyInput).closest("div").find(".address-field.validate-required");d.length&&d.each(function(){""===a(this).find("input.input-text").val()&&(c=!1)})}c&&b.trigger_update_checkout()},ship_to_different_address:function(){a("div.shipping_address").hide(),a(this).is(":checked")&&a("div.shipping_address").slideDown()},reset_update_checkout_timer:function(){clearTimeout(b.updateTimer)},validate_field:function(b){var c=a(this),d=c.closest(".form-row"),e=!0,f=d.is(".validate-required"),g=d.is(".validate-email"),h=b.type;if("input"===h&&d.removeClass("woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email woocommerce-validated"),"focusout"===h||"change"===h){if(f&&("checkbox"!==c.attr("type")||c.is(":checked")?""===c.val()&&(d.removeClass("woocommerce-validated").addClass("woocommerce-invalid woocommerce-invalid-required-field"),e=!1):(d.removeClass("woocommerce-validated").addClass("woocommerce-invalid woocommerce-invalid-required-field"),e=!1)),g&&c.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);i.test(c.val())||(d.removeClass("woocommerce-validated").addClass("woocommerce-invalid woocommerce-invalid-email"),e=!1)}e&&d.removeClass("woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email").addClass("woocommerce-validated")}},update_checkout:function(a,c){b.reset_update_checkout_timer(),b.updateTimer=setTimeout(b.update_checkout_action,"5",c)},update_checkout_action:function(c){if(b.xhr&&b.xhr.abort(),0!==a("form.checkout").length){c="undefined"!=typeof c?c:{update_shipping_method:!0};var d=a("#billing_country").val(),e=a("#billing_state").val(),f=a("input#billing_postcode").val(),g=a("#billing_city").val(),h=a("input#billing_address_1").val(),i=a("input#billing_address_2").val(),j=d,k=e,l=f,m=g,n=h,o=i;a("#ship-to-different-address").find("input").is(":checked")&&(j=a("#shipping_country").val(),k=a("#shipping_state").val(),l=a("input#shipping_postcode").val(),m=a("#shipping_city").val(),n=a("input#shipping_address_1").val(),o=a("input#shipping_address_2").val());var p={security:wc_checkout_params.update_order_review_nonce,payment_method:b.get_payment_method(),country:d,state:e,postcode:f,city:g,address:h,address_2:i,s_country:j,s_state:k,s_postcode:l,s_city:m,s_address:n,s_address_2:o,post_data:a("form.checkout").serialize()};if(!1!==c.update_shipping_method){var q={};a('select.shipping_method, input[name^="shipping_method"][type="radio"]:checked, input[name^="shipping_method"][type="hidden"]').each(function(){q[a(this).data("index")]=a(this).val()}),p.shipping_method=q}a(".woocommerce-checkout-payment, .woocommerce-checkout-review-order-table").block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),b.xhr=a.ajax({type:"POST",url:wc_checkout_params.wc_ajax_url.toString().replace("%%endpoint%%","update_order_review"),data:p,success:function(c){var d=a('.woocommerce-checkout input[name="payment_method"]:checked').attr("id");if("true"===c.reload)return void window.location.reload();a(".woocommerce-NoticeGroup-updateOrderReview").remove();var e=a("#terms").prop("checked"),f={};if(a(".payment_box input").each(function(){var b=a(this).attr("id");b&&(a.inArray(a(this).attr("type"),["checkbox","radio"])!==-1?f[b]=a(this).prop("checked"):f[b]=a(this).val())}),c&&c.fragments&&a.each(c.fragments,function(b,c){a(b).replaceWith(c),a(b).unblock()}),e&&a("#terms").prop("checked",!0),a.isEmptyObject(f)||a(".payment_box input").each(function(){var b=a(this).attr("id");b&&(a.inArray(a(this).attr("type"),["checkbox","radio"])!==-1?a(this).prop("checked",f[b]).change():a(this).val(f[b]).change())}),"failure"===c.result){var g=a("form.checkout");a(".woocommerce-error, .woocommerce-message").remove(),c.messages?g.prepend('
'+c.messages+"
"):g.prepend(c),g.find(".input-text, select, input:checkbox").blur(),a("html, body").animate({scrollTop:a("form.checkout").offset().top-100},1e3)}b.init_payment_methods(d),a(document.body).trigger("updated_checkout",[c])}})}},submit:function(){b.reset_update_checkout_timer();var c=a(this);if(c.is(".processing"))return!1;if(c.triggerHandler("checkout_place_order")!==!1&&c.triggerHandler("checkout_place_order_"+b.get_payment_method())!==!1){c.addClass("processing");var d=c.data();1!==d["blockUI.isBlocked"]&&c.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),a.ajaxSetup({dataFilter:function(b,c){if("json"!==c)return b;try{var d=a.parseJSON(b);if(d&&"object"==typeof d)return b}catch(a){var e=b.match(/{"result.*"}/);null===e?console.log("Unable to fix malformed JSON"):(console.log("Fixed malformed JSON. Original:"),console.log(b),b=e[0])}return b}}),a.ajax({type:"POST",url:wc_checkout_params.checkout_url,data:c.serialize(),dataType:"json",success:function(c){try{if("success"!==c.result)throw"failure"===c.result?"Result failure":"Invalid response";-1===c.redirect.indexOf("https://")||-1===c.redirect.indexOf("http://")?window.location=c.redirect:window.location=decodeURI(c.redirect)}catch(d){if("true"===c.reload)return void window.location.reload();"true"===c.refresh&&a(document.body).trigger("update_checkout"),c.messages?b.submit_error(c.messages):b.submit_error('
'+wc_checkout_params.i18n_checkout_error+"
")}},error:function(a,c,d){b.submit_error('
'+d+"
")}})}return!1},submit_error:function(c){a(".woocommerce-error, .woocommerce-message").remove(),b.$checkout_form.prepend('
'+c+"
"),b.$checkout_form.removeClass("processing").unblock(),b.$checkout_form.find(".input-text, select, input:checkbox").blur(),a("html, body").animate({scrollTop:a("form.checkout").offset().top-100},1e3),a(document.body).trigger("checkout_error")}},c={init:function(){a(document.body).on("click","a.showcoupon",this.show_coupon_form),a(document.body).on("click",".woocommerce-remove-coupon",this.remove_coupon),a("form.checkout_coupon").hide().submit(this.submit)},show_coupon_form:function(){return a(".checkout_coupon").slideToggle(400,function(){a(".checkout_coupon").find(":input:eq(0)").focus()}),!1},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={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.wc_ajax_url.toString().replace("%%endpoint%%","apply_coupon"),data:c,success:function(c){a(".woocommerce-error, .woocommerce-message").remove(),b.removeClass("processing").unblock(),c&&(b.before(c),b.slideUp(),a(document.body).trigger("update_checkout",{update_shipping_method:!1}))},dataType:"html"}),!1},remove_coupon:function(b){b.preventDefault();var c=a(this).parents(".woocommerce-checkout-review-order"),d=a(this).data("coupon");c.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}});var e={security:wc_checkout_params.remove_coupon_nonce,coupon:d};a.ajax({type:"POST",url:wc_checkout_params.wc_ajax_url.toString().replace("%%endpoint%%","remove_coupon"),data:e,success:function(b){a(".woocommerce-error, .woocommerce-message").remove(),c.removeClass("processing").unblock(),b&&(a("form.woocommerce-checkout").before(b),a(document.body).trigger("update_checkout",{update_shipping_method:!1}),a("form.checkout_coupon").find('input[name="coupon_code"]').val(""))},error:function(a){wc_checkout_params.debug_mode&&console.log(a.responseText)},dataType:"html"})}},d={init:function(){a(document.body).on("click","a.showlogin",this.show_login_form)},show_login_form:function(){return a("form.login, form.woocommerce-form--login").slideToggle(),!1}};b.init(),c.init(),d.init()}); \ No newline at end of file diff --git a/assets/js/frontend/country-select.js b/assets/js/frontend/country-select.js index cea40b35d07..20e8af669e4 100644 --- a/assets/js/frontend/country-select.js +++ b/assets/js/frontend/country-select.js @@ -46,9 +46,8 @@ jQuery( function( $ ) { return wc_country_select_params.i18n_searching; } }; - - var language = { 'language' : formatString }; - + + var language = { 'language' : formatString }; return language; } @@ -62,6 +61,10 @@ jQuery( function( $ ) { }, getEnhancedSelectFormatString() ); $( this ).select2( select2_args ); + // Maintain focus after select https://github.com/select2/select2/issues/4384 + $( this ).on( 'select2:select', function() { + $( this ).focus(); + } ); }); }; diff --git a/assets/js/frontend/country-select.min.js b/assets/js/frontend/country-select.min.js index ca75d4a2536..69f287e52b1 100644 --- a/assets/js/frontend/country-select.min.js +++ b/assets/js/frontend/country-select.min.js @@ -1 +1 @@ -jQuery(function(a){function b(){var a={noResults:function(){return wc_country_select_params.i18n_no_matches},errorLoading:function(){return wc_country_select_params.i18n_ajax_error},inputTooShort:function(a){var b=a.minimum-a.input.length;return 1===b?wc_country_select_params.i18n_input_too_short_1:wc_country_select_params.i18n_input_too_short_n.replace("%qty%",b)},inputTooLong:function(a){var b=a.input.length-a.maximum;return 1===b?wc_country_select_params.i18n_input_too_long_1:wc_country_select_params.i18n_input_too_long_n.replace("%qty%",b)},maximumSelected:function(a){return 1===a.maximum?wc_country_select_params.i18n_selection_too_long_1:wc_country_select_params.i18n_selection_too_long_n.replace("%qty%",a.maximum)},loadingMore:function(){return wc_country_select_params.i18n_load_more},searching:function(){return wc_country_select_params.i18n_searching}},b={language:a};return b}if("undefined"==typeof wc_country_select_params)return!1;if(a().select2){var c=function(){a("select.country_select:visible, select.state_select:visible").each(function(){var c=a.extend({placeholderOption:"first",width:"100%"},b());a(this).select2(c)})};c(),a(document.body).bind("country_to_state_changed",function(){c()})}var d=wc_country_select_params.countries.replace(/"/g,'"'),e=a.parseJSON(d);a(document.body).on("change","select.country_to_state, input.country_to_state",function(){var b=a(this).closest(".woocommerce-billing-fields, .woocommerce-shipping-fields, .woocommerce-shipping-calculator");b.length||(b=a(this).closest(".form-row").parent());var c=a(this).val(),d=b.find("#billing_state, #shipping_state, #calc_shipping_state"),f=d.parent(),g=d.attr("name"),h=d.attr("id"),i=d.val(),j=d.attr("placeholder")||d.attr("data-placeholder")||"";if(e[c])if(a.isEmptyObject(e[c]))d.parent().hide().find(".select2-container").remove(),d.replaceWith(''),a(document.body).trigger("country_to_state_changed",[c,b]);else{var k="",l=e[c];for(var m in l)l.hasOwnProperty(m)&&(k=k+'");d.parent().show(),d.is("input")&&(d.replaceWith(''),d=b.find("#billing_state, #shipping_state, #calc_shipping_state")),d.html('"+k),d.val(i).change(),a(document.body).trigger("country_to_state_changed",[c,b])}else d.is("select")?(f.show().find(".select2-container").remove(),d.replaceWith(''),a(document.body).trigger("country_to_state_changed",[c,b])):d.is('input[type="hidden"]')&&(f.show().find(".select2-container").remove(),d.replaceWith(''),a(document.body).trigger("country_to_state_changed",[c,b]));a(document.body).trigger("country_to_state_changing",[c,b])}),a(function(){a(":input.country_to_state").change()})}); \ No newline at end of file +jQuery(function(a){function b(){var a={noResults:function(){return wc_country_select_params.i18n_no_matches},errorLoading:function(){return wc_country_select_params.i18n_ajax_error},inputTooShort:function(a){var b=a.minimum-a.input.length;return 1===b?wc_country_select_params.i18n_input_too_short_1:wc_country_select_params.i18n_input_too_short_n.replace("%qty%",b)},inputTooLong:function(a){var b=a.input.length-a.maximum;return 1===b?wc_country_select_params.i18n_input_too_long_1:wc_country_select_params.i18n_input_too_long_n.replace("%qty%",b)},maximumSelected:function(a){return 1===a.maximum?wc_country_select_params.i18n_selection_too_long_1:wc_country_select_params.i18n_selection_too_long_n.replace("%qty%",a.maximum)},loadingMore:function(){return wc_country_select_params.i18n_load_more},searching:function(){return wc_country_select_params.i18n_searching}},b={language:a};return b}if("undefined"==typeof wc_country_select_params)return!1;if(a().select2){var c=function(){a("select.country_select:visible, select.state_select:visible").each(function(){var c=a.extend({placeholderOption:"first",width:"100%"},b());a(this).select2(c),a(this).on("select2:select",function(){a(this).focus()})})};c(),a(document.body).bind("country_to_state_changed",function(){c()})}var d=wc_country_select_params.countries.replace(/"/g,'"'),e=a.parseJSON(d);a(document.body).on("change","select.country_to_state, input.country_to_state",function(){var b=a(this).closest(".woocommerce-billing-fields, .woocommerce-shipping-fields, .woocommerce-shipping-calculator");b.length||(b=a(this).closest(".form-row").parent());var c=a(this).val(),d=b.find("#billing_state, #shipping_state, #calc_shipping_state"),f=d.parent(),g=d.attr("name"),h=d.attr("id"),i=d.val(),j=d.attr("placeholder")||d.attr("data-placeholder")||"";if(e[c])if(a.isEmptyObject(e[c]))d.parent().hide().find(".select2-container").remove(),d.replaceWith(''),a(document.body).trigger("country_to_state_changed",[c,b]);else{var k="",l=e[c];for(var m in l)l.hasOwnProperty(m)&&(k=k+'");d.parent().show(),d.is("input")&&(d.replaceWith(''),d=b.find("#billing_state, #shipping_state, #calc_shipping_state")),d.html('"+k),d.val(i).change(),a(document.body).trigger("country_to_state_changed",[c,b])}else d.is("select")?(f.show().find(".select2-container").remove(),d.replaceWith(''),a(document.body).trigger("country_to_state_changed",[c,b])):d.is('input[type="hidden"]')&&(f.show().find(".select2-container").remove(),d.replaceWith(''),a(document.body).trigger("country_to_state_changed",[c,b]));a(document.body).trigger("country_to_state_changing",[c,b])}),a(function(){a(":input.country_to_state").change()})}); \ No newline at end of file diff --git a/includes/class-wc-checkout.php b/includes/class-wc-checkout.php index 768643666a3..57fef2da486 100644 --- a/includes/class-wc-checkout.php +++ b/includes/class-wc-checkout.php @@ -202,7 +202,6 @@ class WC_Checkout { 'label' => __( 'Account username', 'woocommerce' ), 'required' => true, 'placeholder' => esc_attr__( 'Username', 'woocommerce' ), - 'autocomplete' => 'username', ); } @@ -212,7 +211,6 @@ class WC_Checkout { 'label' => __( 'Account password', 'woocommerce' ), 'required' => true, 'placeholder' => esc_attr__( 'Password', 'woocommerce' ), - 'autocomplete' => 'password', ); } @@ -618,10 +616,10 @@ class WC_Checkout { } } - if ( in_array( 'email', $format ) ) { + if ( in_array( 'email', $format ) && '' !== $data[ $key ] ) { $data[ $key ] = sanitize_email( $data[ $key ] ); - if ( '' !== $data[ $key ] && ! is_email( $data[ $key ] ) ) { + if ( ! is_email( $data[ $key ] ) ) { /* translators: %s: email address */ $errors->add( 'validation', sprintf( __( '%s is not a valid email address.', 'woocommerce' ), '' . $field_label . '' ) ); } diff --git a/templates/checkout/form-billing.php b/templates/checkout/form-billing.php index afb9662d35c..2abed5dcdac 100644 --- a/templates/checkout/form-billing.php +++ b/templates/checkout/form-billing.php @@ -43,7 +43,8 @@ if ( ! defined( 'ABSPATH' ) ) { - + +
is_registration_enabled() ) : ?> is_registration_required() ) : ?> @@ -56,32 +57,23 @@ if ( ! defined( 'ABSPATH' ) ) { - + - get_checkout_fields( 'account' ) ) : ?> + -
+ get_checkout_fields( 'account' ) ) : ?> -

+ - - - - +
+ +