From 054d499636f34885664cc00869db1ddfec0d6674 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 6 Jan 2015 17:39:09 +0000 Subject: [PATCH] Made checkout JS more modular --- assets/js/frontend/checkout.js | 840 +++++++++--------- assets/js/frontend/checkout.min.js | 2 +- .../settings/class-wc-settings-shipping.php | 2 +- 3 files changed, 419 insertions(+), 425 deletions(-) diff --git a/assets/js/frontend/checkout.js b/assets/js/frontend/checkout.js index 190603f1e7d..be188ade058 100644 --- a/assets/js/frontend/checkout.js +++ b/assets/js/frontend/checkout.js @@ -1,498 +1,492 @@ jQuery( function( $ ) { + // wc_checkout_params is required to continue, ensure the object exists + if ( typeof wc_checkout_params === 'undefined' ) { + return false; + } + $.blockUI.defaults.overlayCSS.cursor = 'default'; - // wc_checkout_params is required to continue, ensure the object exists - if ( typeof wc_checkout_params === 'undefined' ) - return false; + var wc_checkout_form = { + updateTimer: false, + dirtyInput: false, + xhr: false, + $order_review: $( '#order_review' ), + $checkout_form: $( 'form.checkout' ), + init: function() { + $( 'body' ).bind( 'update_checkout', this.reset_update_checkout_timer ); + $( 'body' ).bind( 'update_checkout', this.update_checkout ); + $( 'body' ).bind( 'init_checkout', this.init_checkout ); - var updateTimer, - dirtyInput = false, - xhr; + // Payment methods + this.$order_review.on( 'click', '.payment_methods input.input-radio', this.payment_method_selected ); - function update_checkout() { + // Form submission + this.$checkout_form.on( 'submit', this.submit ); - if ( xhr ) xhr.abort(); + // Inline validation + this.$checkout_form.on( 'blur input change', '.input-text, select', this.validate_field ); - var shipping_methods = []; + // Inputs/selects which update totals + this.$checkout_form.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]', this.trigger_update_checkout ); + 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( 'input change', '.address-field select', this.input_changed ); + this.$checkout_form.on( 'input keydown', '.address-field input.input-text, .update_totals_on_change input.input-text', this.queue_update_checkout ); - $( 'select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]' ).each( function( index, input ) { - shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val(); - } ); + // Address fields + this.$checkout_form.on( 'change', '#ship-to-different-address input', this.ship_to_different_address ); - var payment_method = $( '#order_review input[name=payment_method]:checked' ).val(), - country = $( '#billing_country' ).val(), - state = $( '#billing_state' ).val(), - postcode = $( 'input#billing_postcode' ).val(), - city = $( '#billing_city' ).val(), - address = $( 'input#billing_address_1' ).val(), - address_2 = $( 'input#billing_address_2' ).val(), - s_country, - s_state, - s_postcode, - s_city, - s_address, - s_address_2; + // Trigger events + this.$order_review.find( 'input[name=payment_method]:checked' ).triggerHandler( 'click' ); + this.$checkout_form.find( '#ship-to-different-address input' ).change(); - if ( $( '#ship-to-different-address input' ).is( ':checked' ) ) { - s_country = $( '#shipping_country' ).val(); - s_state = $( '#shipping_state' ).val(); - s_postcode = $( 'input#shipping_postcode' ).val(); - s_city = $( '#shipping_city' ).val(); - s_address = $( 'input#shipping_address_1' ).val(); - s_address_2 = $( 'input#shipping_address_2' ).val(); - } else { - s_country = country; - s_state = state; - s_postcode = postcode; - s_city = city; - s_address = address; - s_address_2 = address_2; - } - - $( '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' ).block({ - message: null, - overlayCSS: { - background: '#fff', - opacity: 0.6 + // Update on page load + if ( wc_checkout_params.is_checkout === '1' ) { + $( 'body' ).trigger( 'init_checkout' ); } - }); - - var data = { - action: 'woocommerce_update_order_review', - security: wc_checkout_params.update_order_review_nonce, - shipping_method: shipping_methods, - payment_method: payment_method, - country: country, - state: state, - postcode: postcode, - city: city, - address: address, - address_2: address_2, - s_country: s_country, - s_state: s_state, - s_postcode: s_postcode, - s_city: s_city, - s_address: s_address, - s_address_2: s_address_2, - post_data: $( 'form.checkout' ).serialize() - }; - - xhr = $.ajax({ - type: 'POST', - url: wc_checkout_params.ajax_url, - data: data, - success: function( data ) { - - // Remove old errors - $( '.woocommerce-error, .woocommerce-message' ).remove(); - - // Always update the fragements - if ( data ) { - - if ( data.fragments ) { - - $.each( data.fragments, function ( key, value ) { - $( key ).replaceWith( value ); - $( key ).unblock(); - } ); - - } - - } - - // Check for error - if ( 'failure' == data.result ) { - - var $form = $( 'form.checkout' ); - - if ( 'true' === data.reload ) { - window.location.reload(); - return; - } - - // Add new errors - if ( data.messages ) { - $form.prepend( data.messages ); - } else { - $form.prepend( data ); - } - - // Lose focus for all fields - $form.find( '.input-text, select' ).blur(); - - // Scroll to top - $( 'html, body' ).animate( { - scrollTop: ( $( 'form.checkout' ).offset().top - 100 ) - }, 1000 ); - - } - - // Trigger click event on selected payment method - $( '.woocommerce-checkout' ).find( 'input[name=payment_method]:checked' ).trigger( 'click' ); - - // Fire updated_checkout event - $( 'body' ).trigger( 'updated_checkout' ); - + if ( wc_checkout_params.option_guest_checkout === 'yes' ) { + $( 'input#createaccount' ).change( this.toggle_create_account ).change(); } - - }); - } - - // Event for updating the checkout - $( 'body' ).bind( 'update_checkout', function() { - clearTimeout( updateTimer ); - update_checkout(); - }); - - $( '.checkout_coupon, div.shipping_address' ).hide(); - - $( 'a.showlogin' ).click( function() { - $( 'form.login' ).slideToggle(); - - return false; - }); - - $( 'a.showcoupon' ).click( function() { - $( '.checkout_coupon' ).slideToggle( 400, function() { - $( '#coupon_code' ).focus(); - }); - - return false; - }); - - $( '#ship-to-different-address input' ).change( function() { - $( 'div.shipping_address' ).hide(); - if ( $( this ).is( ':checked' ) ) { - $( 'div.shipping_address' ).slideDown(); - } - }).change(); - - if ( wc_checkout_params.option_guest_checkout === 'yes' ) { - $( 'div.create-account' ).hide(); - - $( 'input#createaccount' ).change( function() { - $( 'div.create-account' ).hide(); + }, + toggle_create_account: function( e ) { + $( 'div.create-account' ).hide(); if ( $( this ).is( ':checked' ) ) { $( 'div.create-account' ).slideDown(); } - }).change(); - } + }, + init_checkout: function( e ) { + $( '#billing_country, #shipping_country, .country_to_state' ).change(); + $( 'body' ).trigger( 'update_checkout' ); + }, + maybe_input_changed: function( e ) { + if ( wc_checkout_form.dirtyInput ) { + wc_checkout_form.input_changed(); + } + }, + input_changed: function( e ) { + wc_checkout_form.dirtyInput = this; + wc_checkout_form.maybe_update_checkout(); + }, + queue_update_checkout: function( e ) { + var code = e.keyCode || e.which || 0; - $( '#order_review' ) + if ( code === 9 ) { + return true; + } - /* Payment option selection */ + wc_checkout_form.dirtyInput = this; + wc_checkout_form.reset_update_checkout_timer(); + wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.maybe_update_checkout, '1000' ); + }, + trigger_update_checkout: function( e ) { + wc_checkout_form.reset_update_checkout_timer(); + wc_checkout_form.dirtyInput = false; + $( 'body' ).trigger( 'update_checkout' ); + }, + maybe_update_checkout: function() { + var update_totals = true; - .on( 'click', '.payment_methods input.input-radio', function() { - if ( $( '.payment_methods input.input-radio' ).length > 1 ) { - var target_payment_box = $( 'div.payment_box.' + $( this ).attr( 'ID' ) ); + if ( $( wc_checkout_form.dirtyInput ).size() ) { + $required_inputs = $( wc_checkout_form.dirtyInput ).closest( 'div' ).find( '.address-field.validate-required' ); - if ( $( this ).is( ':checked' ) && ! target_payment_box.is( ':visible' ) ) { - $( 'div.payment_box' ).filter( ':visible' ).slideUp( 250 ); - - if ( $( this ).is( ':checked' ) ) { - $( 'div.payment_box.' + $( this ).attr( 'ID' ) ).slideDown( 250 ); + if ( $required_inputs.size() ) { + $required_inputs.each( function( e ) { + if ( $( this ).find( 'input.input-text' ).val() === '' ) { + update_totals = false; + } + }); } } - } else { - $( 'div.payment_box' ).show(); - } + if ( update_totals ) { + wc_checkout_form.trigger_update_checkout(); + } + }, + ship_to_different_address: function( e ) { + $( 'div.shipping_address' ).hide(); + if ( $( this ).is( ':checked' ) ) { + $( 'div.shipping_address' ).slideDown(); + } + }, + payment_method_selected: function( e ) { + if ( $( '.payment_methods input.input-radio' ).length > 1 ) { + var target_payment_box = $( 'div.payment_box.' + $( this ).attr( 'ID' ) ); - if ( $( this ).data( 'order_button_text' ) ) { - $( '#place_order' ).val( $( this ).data( 'order_button_text' ) ); - } else { - $( '#place_order' ).val( $( '#place_order' ).data( 'value' ) ); - } - }) + if ( $( this ).is( ':checked' ) && ! target_payment_box.is( ':visible' ) ) { + $( 'div.payment_box' ).filter( ':visible' ).slideUp( 250 ); - // Trigger initial click - .find( 'input[name=payment_method]:checked' ).click(); - - // Used for input change events below - function input_changed() { - var update_totals = true; - - if ( $( dirtyInput ).size() ) { - - $required_inputs = $( dirtyInput ).closest( 'div' ).find( '.address-field.validate-required' ); - - if ( $required_inputs.size() ) { - $required_inputs.each( function() { - if ( $( this ).find( 'input.input-text' ).val() === '' ) { - update_totals = false; + if ( $( this ).is( ':checked' ) ) { + $( 'div.payment_box.' + $( this ).attr( 'ID' ) ).slideDown( 250 ); } - }); + } + } else { + $( 'div.payment_box' ).show(); } - } - - if ( update_totals ) { - dirtyInput = false; - $( 'body' ).trigger( 'update_checkout' ); - } - } - - $( 'form.checkout' ) - - /* Update totals/taxes/shipping */ - // Inputs/selects which update totals instantly - .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( updateTimer ); - dirtyInput = false; - $( 'body' ).trigger( 'update_checkout' ); - }) - - // Address-fields which refresh totals when all required fields are filled - .on( 'change', '.address-field input.input-text, .update_totals_on_change input.input-text', function() { - if ( dirtyInput ) { - input_changed(); - } - }) - - .on( 'input change', '.address-field select', function() { - dirtyInput = this; - input_changed(); - }) - - .on( 'input keydown', '.address-field input.input-text, .update_totals_on_change input.input-text', function( e ){ - var code = e.keyCode || e.which || 0; - - if ( code === 9 ) { - return true; - } - - dirtyInput = this; - clearTimeout( updateTimer ); - updateTimer = setTimeout( input_changed, '1000' ); - }) - - /* Inline validation */ - - .on( 'blur input change', '.input-text, select', function() { - var $this = $( this ), - $parent = $this.closest( '.form-row' ), - validated = true; - - if ( $parent.is( '.validate-required' ) ) { - if ( $this.val() === '' ) { - $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' ); - validated = false; + if ( $( this ).data( 'order_button_text' ) ) { + $( '#place_order' ).val( $( this ).data( 'order_button_text' ) ); + } else { + $( '#place_order' ).val( $( '#place_order' ).data( 'value' ) ); } - } + }, + reset_update_checkout_timer: function() { + clearTimeout( wc_checkout_form.updateTimer ); + }, + validate_field: function( e ) { + var $this = $( this ), + $parent = $this.closest( '.form-row' ), + validated = true; - if ( $parent.is( '.validate-email' ) ) { - if ( $this.val() ) { - - /* http://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 ( $parent.is( '.validate-required' ) ) { + 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 ( $parent.is( '.validate-email' ) ) { + if ( $this.val() ) { - /* AJAX Form Submission */ + /* http://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); - .submit( function() { - clearTimeout( updateTimer ); - - var $form = $( this ); - - if ( $form.is( '.processing' ) ) { - return false; - } - - // Trigger a handler to let gateways manipulate the checkout if needed - if ( $form.triggerHandler( 'checkout_place_order' ) !== false && $form.triggerHandler( 'checkout_place_order_' + $( '#order_review input[name=payment_method]:checked' ).val() ) !== false ) { - - $form.addClass( 'processing' ); - - var form_data = $form.data(); - - if ( form_data["blockUI.isBlocked"] != 1 ) { - $form.block({ - message: null, - overlayCSS: { - background: '#fff', - opacity: 0.6 + if ( ! pattern.test( $this.val() ) ) { + $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-email' ); + validated = false; } - }); + } } - $.ajax({ + if ( validated ) { + $parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field' ).addClass( 'woocommerce-validated' ); + } + }, + update_checkout: function( e ) { + if ( wc_checkout_form.xhr ) { + wc_checkout_form.xhr.abort(); + } + + var shipping_methods = []; + + $( 'select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]' ).each( function( index, input ) { + shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val(); + } ); + + var payment_method = $( '#order_review input[name=payment_method]:checked' ).val(), + country = $( '#billing_country' ).val(), + state = $( '#billing_state' ).val(), + postcode = $( 'input#billing_postcode' ).val(), + city = $( '#billing_city' ).val(), + address = $( 'input#billing_address_1' ).val(), + address_2 = $( 'input#billing_address_2' ).val(), + s_country, + s_state, + s_postcode, + s_city, + s_address, + s_address_2; + + if ( $( '#ship-to-different-address input' ).is( ':checked' ) ) { + s_country = $( '#shipping_country' ).val(); + s_state = $( '#shipping_state' ).val(); + s_postcode = $( 'input#shipping_postcode' ).val(); + s_city = $( '#shipping_city' ).val(); + s_address = $( 'input#shipping_address_1' ).val(); + s_address_2 = $( 'input#shipping_address_2' ).val(); + } else { + s_country = country; + s_state = state; + s_postcode = postcode; + s_city = city; + s_address = address; + s_address_2 = address_2; + } + + $( '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' ).block({ + message: null, + overlayCSS: { + background: '#fff', + opacity: 0.6 + } + }); + + var data = { + action: 'woocommerce_update_order_review', + security: wc_checkout_params.update_order_review_nonce, + shipping_method: shipping_methods, + payment_method: payment_method, + country: country, + state: state, + postcode: postcode, + city: city, + address: address, + address_2: address_2, + s_country: s_country, + s_state: s_state, + s_postcode: s_postcode, + s_city: s_city, + s_address: s_address, + s_address_2: s_address_2, + post_data: $( 'form.checkout' ).serialize() + }; + + wc_checkout_form.xhr = $.ajax({ type: 'POST', - url: wc_checkout_params.checkout_url, - data: $form.serialize(), - success: function( code ) { - var result = ''; + url: wc_checkout_params.ajax_url, + data: data, + success: function( data ) { + // Remove old errors + $( '.woocommerce-error, .woocommerce-message' ).remove(); - try { - // Get the valid JSON only from the returned string - if ( code.indexOf( '' ) >= 0 ) - code = code.split( '' )[1]; // Strip off before after WC_START - - if ( code.indexOf( '' ) >= 0 ) - code = code.split( '' )[0]; // Strip off anything after WC_END - - // Parse - result = $.parseJSON( code ); - - if ( result.result === 'success' ) { - if ( result.redirect.indexOf( "https://" ) != -1 || result.redirect.indexOf( "http://" ) != -1 ) { - window.location = result.redirect; - } else { - window.location = decodeURI( result.redirect ); - } - } else if ( result.result === 'failure' ) { - throw 'Result failure'; - } else { - throw 'Invalid response'; - } + // Always update the fragements + if ( data && data.fragments ) { + $.each( data.fragments, function ( key, value ) { + $( key ).replaceWith( value ); + $( key ).unblock(); + } ); } - catch( err ) { + // Check for error + if ( 'failure' == data.result ) { - if ( result.reload === 'true' ) { + var $form = $( 'form.checkout' ); + + if ( 'true' === data.reload ) { window.location.reload(); return; } - // Remove old errors - $( '.woocommerce-error, .woocommerce-message' ).remove(); - // Add new errors - if ( result.messages ) { - $form.prepend( result.messages ); + if ( data.messages ) { + $form.prepend( data.messages ); } else { - $form.prepend( code ); + $form.prepend( data ); } - // Cancel processing - $form.removeClass( 'processing' ).unblock(); - // Lose focus for all fields $form.find( '.input-text, select' ).blur(); // Scroll to top - $( 'html, body' ).animate({ + $( 'html, body' ).animate( { scrollTop: ( $( 'form.checkout' ).offset().top - 100 ) }, 1000 ); - // Trigger update in case we need a fresh nonce - if ( result.refresh === 'true' ) - $( 'body' ).trigger( 'update_checkout' ); + } - $( 'body' ).trigger( 'checkout_error' ); + // Trigger click e on selected payment method + $( '.woocommerce-checkout' ).find( 'input[name=payment_method]:checked' ).triggerHandler( 'click' ); + + // Fire updated_checkout e + $( 'body' ).trigger( 'updated_checkout' ); + } + + }); + }, + submit: function( e ) { + wc_checkout_form.reset_update_checkout_timer(); + var $form = $( this ); + + if ( $form.is( '.processing' ) ) { + return false; + } + + // Trigger a handler to let gateways manipulate the checkout if needed + if ( $form.triggerHandler( 'checkout_place_order' ) !== false && $form.triggerHandler( 'checkout_place_order_' + $( '#order_review input[name=payment_method]:checked' ).val() ) !== false ) { + + $form.addClass( 'processing' ); + + var form_data = $form.data(); + + if ( form_data["blockUI.isBlocked"] != 1 ) { + $form.block({ + message: null, + overlayCSS: { + background: '#fff', + opacity: 0.6 + } + }); + } + + $.ajax({ + type: 'POST', + url: wc_checkout_params.checkout_url, + data: $form.serialize(), + success: function( code ) { + var result = ''; + + try { + // Get the valid JSON only from the returned string + if ( code.indexOf( '' ) >= 0 ) + code = code.split( '' )[1]; // Strip off before after WC_START + + if ( code.indexOf( '' ) >= 0 ) + code = code.split( '' )[0]; // Strip off anything after WC_END + + // Parse + result = $.parseJSON( code ); + + if ( result.result === 'success' ) { + if ( result.redirect.indexOf( "https://" ) != -1 || result.redirect.indexOf( "http://" ) != -1 ) { + window.location = result.redirect; + } else { + window.location = decodeURI( result.redirect ); + } + } else if ( result.result === 'failure' ) { + throw 'Result failure'; + } else { + throw 'Invalid response'; + } + } + + catch( err ) { + + if ( result.reload === 'true' ) { + window.location.reload(); + return; + } + + // Remove old errors + $( '.woocommerce-error, .woocommerce-message' ).remove(); + + // Add new errors + if ( result.messages ) { + $form.prepend( result.messages ); + } else { + $form.prepend( code ); + } + + // Cancel processing + $form.removeClass( 'processing' ).unblock(); + + // Lose focus for all fields + $form.find( '.input-text, select' ).blur(); + + // Scroll to top + $( 'html, body' ).animate({ + scrollTop: ( $( 'form.checkout' ).offset().top - 100 ) + }, 1000 ); + + // Trigger update in case we need a fresh nonce + if ( result.refresh === 'true' ) { + $( 'body' ).trigger( 'update_checkout' ); + } + + $( 'body' ).trigger( 'checkout_error' ); + } + }, + dataType: 'html' + }); + + } + + return false; + } + }; + + var wc_checkout_coupons = { + init: function() { + $( 'body' ).on( 'click', 'a.showcoupon', this.show_coupon_form ); + $( 'body' ).on( 'click', '.woocommerce-remove-coupon', this.remove_coupon ); + $( 'form.checkout_coupon' ).hide().submit( this.submit ); + }, + show_coupon_form: function( e ) { + $( '.checkout_coupon' ).slideToggle( 400, function( e ) { + $( '.checkout_coupon' ).find(':input:eq(0)').focus() + }); + return false; + }, + submit: function( e ) { + var $form = $( this ); + + if ( $form.is( '.processing' ) ) return false; + + $form.addClass( 'processing' ).block({ + message: null, + overlayCSS: { + background: '#fff', + opacity: 0.6 + } + }); + + var data = { + action: 'woocommerce_apply_coupon', + security: wc_checkout_params.apply_coupon_nonce, + coupon_code: $form.find( 'input[name=coupon_code]' ).val() + }; + + $.ajax({ + type: 'POST', + url: wc_checkout_params.ajax_url, + data: data, + success: function( code ) { + $( '.woocommerce-error, .woocommerce-message' ).remove(); + $form.removeClass( 'processing' ).unblock(); + + if ( code ) { + $form.before( code ); + $form.slideUp(); + + $( 'body' ).trigger( 'update_checkout' ); } }, dataType: 'html' }); - } + return false; + }, + remove_coupon: function( e ) { + e.preDefault(); - return false; - }); + var container = $( this ).parents( '.woocommerce-checkout-review-order' ), + coupon = $( this ).data( 'coupon' ); - /* AJAX Coupon Form Submission */ - $( 'form.checkout_coupon' ).submit( function() { - var $form = $( this ); - - if ( $form.is( '.processing' ) ) return false; - - $form.addClass( 'processing' ).block({ - message: null, - overlayCSS: { - background: '#fff', - opacity: 0.6 - } - }); - - var data = { - action: 'woocommerce_apply_coupon', - security: wc_checkout_params.apply_coupon_nonce, - coupon_code: $form.find( 'input[name=coupon_code]' ).val() - }; - - $.ajax({ - type: 'POST', - url: wc_checkout_params.ajax_url, - data: data, - success: function( code ) { - $( '.woocommerce-error, .woocommerce-message' ).remove(); - $form.removeClass( 'processing' ).unblock(); - - if ( code ) { - $form.before( code ); - $form.slideUp(); - - $( 'body' ).trigger( 'update_checkout' ); + container.addClass( 'processing' ).block({ + message: null, + overlayCSS: { + background: '#fff', + opacity: 0.6 } - }, - dataType: 'html' - }); + }); - return false; - }); + var data = { + action: 'woocommerce_remove_coupon', + security: wc_checkout_params.remove_coupon_nonce, + coupon: coupon + }; - $( '.woocommerce-checkout-review-order' ).on( 'click', '.woocommerce-remove-coupon', function( e ) { - e.preventDefault(); + $.ajax({ + type: 'POST', + url: wc_checkout_params.ajax_url, + data: data, + success: function( code ) { + $( '.woocommerce-error, .woocommerce-message' ).remove(); + container.removeClass( 'processing' ).unblock(); - var container = $( this ).parents( '.woocommerce-checkout-review-order' ), - coupon = $( this ).data( 'coupon' ); + if ( code ) { + container.before( code ); - container.addClass( 'processing' ).block({ - message: null, - overlayCSS: { - background: '#fff', - opacity: 0.6 - } - }); + $( 'body' ).trigger( 'update_checkout' ); - 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 - .bind( 'init_checkout', function() { - $( '#billing_country, #shipping_country, .country_to_state' ).change(); - $( 'body' ).trigger( 'update_checkout' ); - }); - - // Update on page load - if ( wc_checkout_params.is_checkout === '1' ) { - $( 'body' ).trigger( 'init_checkout' ); + // remove coupon code from coupon field + $( 'form.checkout_coupon' ).find( 'input[name="coupon_code"]' ).val( '' ); + } + }, + dataType: 'html' + }); + } } + + var wc_checkout_login_form = { + init: function() { + $( 'body' ).on( 'click', 'a.showlogin', this.show_login_form ); + }, + show_login_form: function( e ) { + $( 'form.login' ).slideToggle(); + return false; + } + } + + wc_checkout_form.init(); + wc_checkout_coupons.init(); + wc_checkout_login_form.init(); }); diff --git a/assets/js/frontend/checkout.min.js b/assets/js/frontend/checkout.min.js index 61a60a4341e..bdf10754231 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){if(a(".woocommerce-error, .woocommerce-message").remove(),b&&b.fragments&&a.each(b.fragments,function(b,c){a(b).replaceWith(c),a(b).unblock()}),"failure"==b.result){var c=a("form.checkout");if("true"===b.reload)return void window.location.reload();c.prepend(b.messages?b.messages:b),c.find(".input-text, select").blur(),a("html, body").animate({scrollTop:a("form.checkout").offset().top-100},1e3)}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(".woocommerce-checkout-review-order").on("click",".woocommerce-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={action:"woocommerce_remove_coupon",security:wc_checkout_params.remove_coupon_nonce,coupon:d};a.ajax({type:"POST",url:wc_checkout_params.ajax_url,data:e,success:function(b){a(".woocommerce-error, .woocommerce-message").remove(),c.removeClass("processing").unblock(),b&&(c.before(b),a("body").trigger("update_checkout"),a("form.checkout_coupon").find('input[name="coupon_code"]').val(""))},dataType:"html"})}),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(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("body").bind("update_checkout",this.reset_update_checkout_timer),a("body").bind("update_checkout",this.update_checkout),a("body").bind("init_checkout",this.init_checkout),this.$order_review.on("click",".payment_methods input.input-radio",this.payment_method_selected),this.$checkout_form.on("submit",this.submit),this.$checkout_form.on("blur input change",".input-text, select",this.validate_field),this.$checkout_form.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]",this.trigger_update_checkout),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("input change",".address-field select",this.input_changed),this.$checkout_form.on("input 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.$order_review.find("input[name=payment_method]:checked").triggerHandler("click"),this.$checkout_form.find("#ship-to-different-address input").change(),"1"===wc_checkout_params.is_checkout&&a("body").trigger("init_checkout"),"yes"===wc_checkout_params.option_guest_checkout&&a("input#createaccount").change(this.toggle_create_account).change()},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("body").trigger("update_checkout")},maybe_input_changed:function(){b.dirtyInput&&b.input_changed()},input_changed:function(){b.dirtyInput=this,b.maybe_update_checkout()},queue_update_checkout:function(a){var c=a.keyCode||a.which||0;return 9===c?!0:(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("body").trigger("update_checkout")},maybe_update_checkout:function(){var c=!0;a(b.dirtyInput).size()&&($required_inputs=a(b.dirtyInput).closest("div").find(".address-field.validate-required"),$required_inputs.size()&&$required_inputs.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()},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("#place_order").val(a(this).data("order_button_text")?a(this).data("order_button_text"):a("#place_order").data("value"))},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")&&""===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")},update_checkout:function(){b.xhr&&b.xhr.abort();var c=[];a("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(){c[a(this).data("index")]=a(this).val()});var d,e,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")?(d=a("#shipping_country").val(),e=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()):(d=k,e=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:c,payment_method:j,country:k,state:l,postcode:m,city:n,address:o,address_2:p,s_country:d,s_state:e,s_postcode:f,s_city:g,s_address:h,s_address_2:i,post_data:a("form.checkout").serialize()};b.xhr=a.ajax({type:"POST",url:wc_checkout_params.ajax_url,data:q,success:function(b){if(a(".woocommerce-error, .woocommerce-message").remove(),b&&b.fragments&&a.each(b.fragments,function(b,c){a(b).replaceWith(c),a(b).unblock()}),"failure"==b.result){var c=a("form.checkout");if("true"===b.reload)return void window.location.reload();c.prepend(b.messages?b.messages:b),c.find(".input-text, select").blur(),a("html, body").animate({scrollTop:a("form.checkout").offset().top-100},1e3)}a(".woocommerce-checkout").find("input[name=payment_method]:checked").triggerHandler("click"),a("body").trigger("updated_checkout")}})},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_"+a("#order_review input[name=payment_method]:checked").val())!==!1){c.addClass("processing");var d=c.data();1!=d["blockUI.isBlocked"]&&c.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),a.ajax({type:"POST",url:wc_checkout_params.checkout_url,data:c.serialize(),success:function(b){var d="";try{if(b.indexOf("")>=0&&(b=b.split("")[1]),b.indexOf("")>=0&&(b=b.split("")[0]),d=a.parseJSON(b),"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(),c.prepend(d.messages?d.messages:b),c.removeClass("processing").unblock(),c.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}},c={init:function(){a("body").on("click","a.showcoupon",this.show_coupon_form),a("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={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},remove_coupon:function(b){b.preDefault();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={action:"woocommerce_remove_coupon",security:wc_checkout_params.remove_coupon_nonce,coupon:d};a.ajax({type:"POST",url:wc_checkout_params.ajax_url,data:e,success:function(b){a(".woocommerce-error, .woocommerce-message").remove(),c.removeClass("processing").unblock(),b&&(c.before(b),a("body").trigger("update_checkout"),a("form.checkout_coupon").find('input[name="coupon_code"]').val(""))},dataType:"html"})}},d={init:function(){a("body").on("click","a.showlogin",this.show_login_form)},show_login_form:function(){return a("form.login").slideToggle(),!1}};b.init(),c.init(),d.init()}); \ No newline at end of file diff --git a/includes/admin/settings/class-wc-settings-shipping.php b/includes/admin/settings/class-wc-settings-shipping.php index d29071028a1..333da52dece 100644 --- a/includes/admin/settings/class-wc-settings-shipping.php +++ b/includes/admin/settings/class-wc-settings-shipping.php @@ -115,7 +115,7 @@ class WC_Settings_Shipping extends WC_Settings_Page { 'title' => __( 'Shipping Destination', 'woocommerce' ), 'desc' => __( 'This controls which shipping address is used by default.', 'woocommerce' ), 'id' => 'woocommerce_ship_to_destination', - 'default' => 'shipping', + 'default' => 'billing', 'type' => 'radio', 'options' => array( 'shipping' => __( 'Default to shipping address', 'woocommerce' ),