2015-04-13 15:37:22 +00:00
|
|
|
/* global wc_checkout_params */
|
2014-03-19 03:42:53 +00:00
|
|
|
jQuery( function( $ ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2013-12-04 19:15:24 +00:00
|
|
|
// wc_checkout_params is required to continue, ensure the object exists
|
2015-01-06 17:39:09 +00:00
|
|
|
if ( typeof wc_checkout_params === 'undefined' ) {
|
2013-12-04 19:15:24 +00:00
|
|
|
return false;
|
2015-01-06 17:39:09 +00:00
|
|
|
}
|
2013-12-04 19:15:24 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
$.blockUI.defaults.overlayCSS.cursor = 'default';
|
2014-12-04 08:40:49 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
var wc_checkout_form = {
|
|
|
|
updateTimer: false,
|
|
|
|
dirtyInput: false,
|
|
|
|
xhr: false,
|
|
|
|
$order_review: $( '#order_review' ),
|
|
|
|
$checkout_form: $( 'form.checkout' ),
|
2015-04-13 15:37:22 +00:00
|
|
|
init: function() {
|
2015-04-03 12:21:47 +00:00
|
|
|
$( document.body ).bind( 'update_checkout', this.update_checkout );
|
|
|
|
$( document.body ).bind( 'init_checkout', this.init_checkout );
|
2015-01-06 17:39:09 +00:00
|
|
|
|
|
|
|
// Payment methods
|
2015-10-30 15:23:12 +00:00
|
|
|
this.$checkout_form.on( 'click', 'input[name="payment_method"]', this.payment_method_selected );
|
2015-01-06 17:39:09 +00:00
|
|
|
|
2015-11-30 18:09:09 +00:00
|
|
|
if ( $( document.body ).hasClass( 'woocommerce-order-pay' ) ) {
|
|
|
|
this.$order_review.on( 'click', 'input[name="payment_method"]', this.payment_method_selected );
|
|
|
|
}
|
|
|
|
|
2017-02-23 11:51:27 +00:00
|
|
|
// Prevent HTML5 validation which can conflict.
|
|
|
|
this.$checkout_form.attr( 'novalidate', 'novalidate' );
|
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
// Form submission
|
|
|
|
this.$checkout_form.on( 'submit', this.submit );
|
|
|
|
|
|
|
|
// Inline validation
|
2017-02-23 11:51:27 +00:00
|
|
|
this.$checkout_form.on( 'input blur change', '.input-text, select, input:checkbox', this.validate_field );
|
2015-01-06 17:39:09 +00:00
|
|
|
|
2015-10-06 11:40:23 +00:00
|
|
|
// Manual trigger
|
|
|
|
this.$checkout_form.on( 'update', this.trigger_update_checkout );
|
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
// Inputs/selects which update totals
|
2016-09-05 14:42:25 +00:00
|
|
|
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 );
|
2015-09-16 12:17:36 +00:00
|
|
|
this.$checkout_form.on( 'change', '.address-field select', this.input_changed );
|
2015-01-06 17:39:09 +00:00
|
|
|
this.$checkout_form.on( 'change', '.address-field input.input-text, .update_totals_on_change input.input-text', this.maybe_input_changed );
|
2017-08-22 10:54:44 +00:00
|
|
|
this.$checkout_form.on( 'keydown', '.address-field input.input-text, .update_totals_on_change input.input-text', this.queue_update_checkout );
|
2015-01-06 17:39:09 +00:00
|
|
|
|
|
|
|
// Address fields
|
|
|
|
this.$checkout_form.on( 'change', '#ship-to-different-address input', this.ship_to_different_address );
|
|
|
|
|
|
|
|
// Trigger events
|
|
|
|
this.$checkout_form.find( '#ship-to-different-address input' ).change();
|
2015-10-30 15:23:12 +00:00
|
|
|
this.init_payment_methods();
|
2015-01-06 17:39:09 +00:00
|
|
|
|
|
|
|
// Update on page load
|
|
|
|
if ( wc_checkout_params.is_checkout === '1' ) {
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).trigger( 'init_checkout' );
|
2015-01-06 17:39:09 +00:00
|
|
|
}
|
|
|
|
if ( wc_checkout_params.option_guest_checkout === 'yes' ) {
|
|
|
|
$( 'input#createaccount' ).change( this.toggle_create_account ).change();
|
|
|
|
}
|
2015-04-13 15:37:22 +00:00
|
|
|
},
|
2016-07-21 17:39:34 +00:00
|
|
|
init_payment_methods: function( selectedPaymentMethod ) {
|
2015-10-30 15:23:12 +00:00
|
|
|
var $payment_methods = $( '.woocommerce-checkout' ).find( 'input[name="payment_method"]' );
|
|
|
|
|
2016-07-26 19:37:16 +00:00
|
|
|
// If there is one method, we can hide the radio input
|
|
|
|
if ( 1 === $payment_methods.length ) {
|
|
|
|
$payment_methods.eq(0).hide();
|
|
|
|
}
|
|
|
|
|
2016-08-02 17:05:18 +00:00
|
|
|
// If there was a previously selected method, check that one.
|
2016-07-21 17:39:34 +00:00
|
|
|
if ( selectedPaymentMethod ) {
|
|
|
|
$( '#' + selectedPaymentMethod ).prop( 'checked', true );
|
2016-08-02 17:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there are none selected, select the first.
|
|
|
|
if ( 0 === $payment_methods.filter( ':checked' ).length ) {
|
|
|
|
$payment_methods.eq(0).prop( 'checked', true );
|
2015-10-30 15:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger click event for selected method
|
|
|
|
$payment_methods.filter( ':checked' ).eq(0).trigger( 'click' );
|
|
|
|
},
|
|
|
|
get_payment_method: function() {
|
get_payment_method relies on a more generic DOM element
When checking the payment method, I think it would be better to rely on a more generic DOM element to make sure the find() method will find what it is looking for, in case theme authors have changed the position of things inside the form.
Ive stumble into this because I wanted to display the order review in the top of the page, before the billing form, but I wanted to keep the payment form at the end of the page, so, after changing the order_review DIV position, I did:
```
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20);
add_action( 'woocommerce_checkout_after_customer_details', 'woocommerce_checkout_payment');
```
After I did this, the get_payment_method stopped working.
I might be wrong, but I think this proposal gives more flexibility to theme authors, while not breaking anything. This tweak Ive done dont look really ugly to me, I think I should be able to move things inside the checkout form around.
Thanks for looking into it
cheers
2016-11-27 18:51:22 +00:00
|
|
|
return wc_checkout_form.$checkout_form.find( 'input[name="payment_method"]:checked' ).val();
|
2015-10-30 15:23:12 +00:00
|
|
|
},
|
|
|
|
payment_method_selected: function() {
|
|
|
|
if ( $( '.payment_methods input.input-radio' ).length > 1 ) {
|
|
|
|
var target_payment_box = $( 'div.payment_box.' + $( this ).attr( 'ID' ) );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$( 'div.payment_box' ).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $( this ).data( 'order_button_text' ) ) {
|
|
|
|
$( '#place_order' ).val( $( this ).data( 'order_button_text' ) );
|
|
|
|
} else {
|
|
|
|
$( '#place_order' ).val( $( '#place_order' ).data( 'value' ) );
|
|
|
|
}
|
|
|
|
},
|
2015-07-09 18:04:00 +00:00
|
|
|
toggle_create_account: function() {
|
2015-04-13 15:37:22 +00:00
|
|
|
$( 'div.create-account' ).hide();
|
2014-12-04 08:40:49 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
if ( $( this ).is( ':checked' ) ) {
|
2017-02-23 11:51:27 +00:00
|
|
|
// Ensure password is not pre-populated.
|
|
|
|
$( '#account_password' ).val( '' ).change();
|
2015-01-06 17:39:09 +00:00
|
|
|
$( 'div.create-account' ).slideDown();
|
|
|
|
}
|
2015-04-13 15:37:22 +00:00
|
|
|
},
|
2015-07-09 18:04:00 +00:00
|
|
|
init_checkout: function() {
|
2015-04-13 15:37:22 +00:00
|
|
|
$( '#billing_country, #shipping_country, .country_to_state' ).change();
|
|
|
|
$( document.body ).trigger( 'update_checkout' );
|
|
|
|
},
|
|
|
|
maybe_input_changed: function( e ) {
|
|
|
|
if ( wc_checkout_form.dirtyInput ) {
|
2015-06-05 18:27:44 +00:00
|
|
|
wc_checkout_form.input_changed( e );
|
2015-01-06 17:39:09 +00:00
|
|
|
}
|
2015-04-13 15:37:22 +00:00
|
|
|
},
|
|
|
|
input_changed: function( e ) {
|
2015-06-05 18:27:44 +00:00
|
|
|
wc_checkout_form.dirtyInput = e.target;
|
2015-01-06 17:39:09 +00:00
|
|
|
wc_checkout_form.maybe_update_checkout();
|
2015-04-13 15:37:22 +00:00
|
|
|
},
|
|
|
|
queue_update_checkout: function( e ) {
|
|
|
|
var code = e.keyCode || e.which || 0;
|
2015-01-06 17:39:09 +00:00
|
|
|
|
|
|
|
if ( code === 9 ) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-04 08:40:49 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
wc_checkout_form.dirtyInput = this;
|
|
|
|
wc_checkout_form.reset_update_checkout_timer();
|
|
|
|
wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.maybe_update_checkout, '1000' );
|
2015-04-13 15:37:22 +00:00
|
|
|
},
|
2015-07-09 18:04:00 +00:00
|
|
|
trigger_update_checkout: function() {
|
2015-04-13 15:37:22 +00:00
|
|
|
wc_checkout_form.reset_update_checkout_timer();
|
|
|
|
wc_checkout_form.dirtyInput = false;
|
|
|
|
$( document.body ).trigger( 'update_checkout' );
|
|
|
|
},
|
2015-01-06 17:39:09 +00:00
|
|
|
maybe_update_checkout: function() {
|
|
|
|
var update_totals = true;
|
|
|
|
|
2016-03-01 13:08:25 +00:00
|
|
|
if ( $( wc_checkout_form.dirtyInput ).length ) {
|
2015-04-03 12:21:47 +00:00
|
|
|
var $required_inputs = $( wc_checkout_form.dirtyInput ).closest( 'div' ).find( '.address-field.validate-required' );
|
2015-01-06 17:39:09 +00:00
|
|
|
|
2016-03-01 13:08:25 +00:00
|
|
|
if ( $required_inputs.length ) {
|
2015-07-09 18:04:00 +00:00
|
|
|
$required_inputs.each( function() {
|
2015-01-06 17:39:09 +00:00
|
|
|
if ( $( this ).find( 'input.input-text' ).val() === '' ) {
|
|
|
|
update_totals = false;
|
|
|
|
}
|
|
|
|
});
|
2014-12-04 08:40:49 +00:00
|
|
|
}
|
2015-01-06 17:39:09 +00:00
|
|
|
}
|
|
|
|
if ( update_totals ) {
|
|
|
|
wc_checkout_form.trigger_update_checkout();
|
|
|
|
}
|
|
|
|
},
|
2015-07-09 18:04:00 +00:00
|
|
|
ship_to_different_address: function() {
|
2015-04-13 15:37:22 +00:00
|
|
|
$( 'div.shipping_address' ).hide();
|
2015-01-06 17:39:09 +00:00
|
|
|
if ( $( this ).is( ':checked' ) ) {
|
|
|
|
$( 'div.shipping_address' ).slideDown();
|
|
|
|
}
|
2015-04-13 15:37:22 +00:00
|
|
|
},
|
|
|
|
reset_update_checkout_timer: function() {
|
|
|
|
clearTimeout( wc_checkout_form.updateTimer );
|
|
|
|
},
|
2017-05-25 10:20:05 +00:00
|
|
|
is_valid_json: function( raw_json ) {
|
|
|
|
try {
|
|
|
|
var json = $.parseJSON( raw_json );
|
|
|
|
|
|
|
|
return ( json && 'object' === typeof json );
|
|
|
|
} catch ( e ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2017-02-23 11:51:27 +00:00
|
|
|
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;
|
2014-12-04 08:40:49 +00:00
|
|
|
|
2017-02-23 11:51:27 +00:00
|
|
|
if ( 'input' === event_type ) {
|
|
|
|
$parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email woocommerce-validated' );
|
|
|
|
}
|
2014-12-04 08:40:49 +00:00
|
|
|
|
2017-02-23 11:51:27 +00:00
|
|
|
if ( 'focusout' === event_type || 'change' === event_type ) {
|
2014-12-04 08:40:49 +00:00
|
|
|
|
2017-02-23 11:51:27 +00:00
|
|
|
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' );
|
2015-01-06 17:39:09 +00:00
|
|
|
validated = false;
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 08:40:49 +00:00
|
|
|
|
2017-02-23 11:51:27 +00:00
|
|
|
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' );
|
|
|
|
}
|
2015-01-06 17:39:09 +00:00
|
|
|
}
|
|
|
|
},
|
2015-11-20 14:51:38 +00:00
|
|
|
update_checkout: function( event, args ) {
|
2015-02-16 15:45:25 +00:00
|
|
|
// Small timeout to prevent multiple requests when several fields update at the same time
|
|
|
|
wc_checkout_form.reset_update_checkout_timer();
|
2015-11-20 14:51:38 +00:00
|
|
|
wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.update_checkout_action, '5', args );
|
2015-02-16 15:45:25 +00:00
|
|
|
},
|
2015-11-20 14:51:38 +00:00
|
|
|
update_checkout_action: function( args ) {
|
2015-04-13 15:37:22 +00:00
|
|
|
if ( wc_checkout_form.xhr ) {
|
2015-01-06 17:39:09 +00:00
|
|
|
wc_checkout_form.xhr.abort();
|
|
|
|
}
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2016-03-01 13:08:25 +00:00
|
|
|
if ( $( 'form.checkout' ).length === 0 ) {
|
2015-02-16 14:57:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-20 14:51:38 +00:00
|
|
|
args = typeof args !== 'undefined' ? args : {
|
|
|
|
update_shipping_method: true
|
|
|
|
};
|
2015-01-06 17:39:09 +00:00
|
|
|
|
2015-11-25 18:46:57 +00:00
|
|
|
var 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 = country,
|
|
|
|
s_state = state,
|
|
|
|
s_postcode = postcode,
|
|
|
|
s_city = city,
|
|
|
|
s_address = address,
|
|
|
|
s_address_2 = address_2;
|
2015-01-06 17:39:09 +00:00
|
|
|
|
2015-09-07 17:51:10 +00:00
|
|
|
if ( $( '#ship-to-different-address' ).find( 'input' ).is( ':checked' ) ) {
|
2015-11-25 18:46:57 +00:00
|
|
|
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();
|
2015-01-06 17:39:09 +00:00
|
|
|
}
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
var data = {
|
|
|
|
security: wc_checkout_params.update_order_review_nonce,
|
2015-11-20 14:51:38 +00:00
|
|
|
payment_method: wc_checkout_form.get_payment_method(),
|
2015-01-06 17:39:09 +00:00
|
|
|
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()
|
|
|
|
};
|
|
|
|
|
2015-11-20 14:51:38 +00:00
|
|
|
if ( false !== args.update_shipping_method ) {
|
2016-05-24 00:34:43 +00:00
|
|
|
var shipping_methods = {};
|
2015-11-20 14:51:38 +00:00
|
|
|
|
|
|
|
$( 'select.shipping_method, input[name^="shipping_method"][type="radio"]:checked, input[name^="shipping_method"][type="hidden"]' ).each( function() {
|
|
|
|
shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val();
|
|
|
|
} );
|
|
|
|
|
|
|
|
data.shipping_method = shipping_methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
$( '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' ).block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
background: '#fff',
|
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
wc_checkout_form.xhr = $.ajax({
|
|
|
|
type: 'POST',
|
2015-07-27 16:55:37 +00:00
|
|
|
url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'update_order_review' ),
|
2015-01-06 17:39:09 +00:00
|
|
|
data: data,
|
|
|
|
success: function( data ) {
|
2016-07-21 17:39:34 +00:00
|
|
|
|
2016-07-26 20:41:55 +00:00
|
|
|
var selectedPaymentMethod = $( '.woocommerce-checkout input[name="payment_method"]:checked' ).attr( 'id' );
|
2016-07-21 17:39:34 +00:00
|
|
|
|
2015-12-04 02:20:22 +00:00
|
|
|
// Reload the page if requested
|
2017-03-06 19:52:38 +00:00
|
|
|
if ( true === data.reload ) {
|
2015-12-04 02:20:22 +00:00
|
|
|
window.location.reload();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-23 12:31:34 +00:00
|
|
|
// Remove any notices added previously
|
|
|
|
$( '.woocommerce-NoticeGroup-updateOrderReview' ).remove();
|
|
|
|
|
2016-02-10 18:34:37 +00:00
|
|
|
var termsCheckBoxChecked = $( '#terms' ).prop( 'checked' );
|
|
|
|
|
2017-01-15 10:58:21 +00:00
|
|
|
// Save payment details to a temporary object
|
|
|
|
var paymentDetails = {};
|
|
|
|
$( '.payment_box input' ).each( function() {
|
|
|
|
var ID = $( this ).attr( 'id' );
|
|
|
|
|
|
|
|
if ( ID ) {
|
2017-01-25 03:11:39 +00:00
|
|
|
if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) {
|
2017-01-15 10:58:21 +00:00
|
|
|
paymentDetails[ ID ] = $( this ).prop( 'checked' );
|
|
|
|
} else {
|
|
|
|
paymentDetails[ ID ] = $( this ).val();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-01-27 22:41:33 +00:00
|
|
|
// Always update the fragments
|
2015-01-06 17:39:09 +00:00
|
|
|
if ( data && data.fragments ) {
|
|
|
|
$.each( data.fragments, function ( key, value ) {
|
|
|
|
$( key ).replaceWith( value );
|
|
|
|
$( key ).unblock();
|
|
|
|
} );
|
|
|
|
}
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2016-02-10 18:34:37 +00:00
|
|
|
// Recheck the terms and conditions box, if needed
|
|
|
|
if ( termsCheckBoxChecked ) {
|
|
|
|
$( '#terms' ).prop( 'checked', true );
|
|
|
|
}
|
|
|
|
|
2017-05-15 16:59:22 +00:00
|
|
|
// Fill in the payment details if possible without overwriting data if set.
|
2017-01-15 10:58:21 +00:00
|
|
|
if ( ! $.isEmptyObject( paymentDetails ) ) {
|
|
|
|
$( '.payment_box input' ).each( function() {
|
|
|
|
var ID = $( this ).attr( 'id' );
|
|
|
|
|
|
|
|
if ( ID ) {
|
2017-01-25 03:11:39 +00:00
|
|
|
if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) {
|
2017-01-25 03:19:10 +00:00
|
|
|
$( this ).prop( 'checked', paymentDetails[ ID ] ).change();
|
2017-05-15 16:59:22 +00:00
|
|
|
} else if ( 0 === $( this ).val().length ) {
|
2017-01-25 02:35:06 +00:00
|
|
|
$( this ).val( paymentDetails[ ID ] ).change();
|
2017-01-15 10:58:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
// Check for error
|
2015-07-09 18:04:00 +00:00
|
|
|
if ( 'failure' === data.result ) {
|
2014-03-19 03:42:53 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
var $form = $( 'form.checkout' );
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2016-06-23 12:47:24 +00:00
|
|
|
// Remove notices from all sources
|
|
|
|
$( '.woocommerce-error, .woocommerce-message' ).remove();
|
|
|
|
|
2016-06-23 12:31:34 +00:00
|
|
|
// Add new errors returned by this event
|
2015-01-06 17:39:09 +00:00
|
|
|
if ( data.messages ) {
|
2017-03-06 22:45:45 +00:00
|
|
|
$form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-updateOrderReview">' + data.messages + '</div>' );
|
2015-01-06 17:39:09 +00:00
|
|
|
} else {
|
|
|
|
$form.prepend( data );
|
|
|
|
}
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
// Lose focus for all fields
|
2016-07-05 22:21:51 +00:00
|
|
|
$form.find( '.input-text, select, input:checkbox' ).blur();
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
// Scroll to top
|
|
|
|
$( 'html, body' ).animate( {
|
|
|
|
scrollTop: ( $( 'form.checkout' ).offset().top - 100 )
|
|
|
|
}, 1000 );
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
}
|
2012-12-28 13:02:12 +00:00
|
|
|
|
2016-06-06 17:20:10 +00:00
|
|
|
// Re-init methods
|
2016-07-21 17:39:34 +00:00
|
|
|
wc_checkout_form.init_payment_methods( selectedPaymentMethod );
|
2012-12-28 13:02:12 +00:00
|
|
|
|
2016-10-14 11:38:55 +00:00
|
|
|
// Fire updated_checkout event.
|
2016-09-08 09:17:37 +00:00
|
|
|
$( document.body ).trigger( 'updated_checkout', [ data ] );
|
2015-01-06 17:39:09 +00:00
|
|
|
}
|
2014-03-19 03:42:53 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
});
|
2015-04-13 15:37:22 +00:00
|
|
|
},
|
2015-07-09 18:04:00 +00:00
|
|
|
submit: function() {
|
2015-01-06 17:39:09 +00:00
|
|
|
wc_checkout_form.reset_update_checkout_timer();
|
|
|
|
var $form = $( this );
|
2014-03-19 03:42:53 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
if ( $form.is( '.processing' ) ) {
|
|
|
|
return false;
|
2012-10-08 02:01:43 +00:00
|
|
|
}
|
2013-02-05 10:46:31 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
// Trigger a handler to let gateways manipulate the checkout if needed
|
2015-10-30 15:23:12 +00:00
|
|
|
if ( $form.triggerHandler( 'checkout_place_order' ) !== false && $form.triggerHandler( 'checkout_place_order_' + wc_checkout_form.get_payment_method() ) !== false ) {
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
$form.addClass( 'processing' );
|
2014-04-07 11:42:54 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
var form_data = $form.data();
|
2014-04-07 11:42:54 +00:00
|
|
|
|
2015-07-09 18:04:00 +00:00
|
|
|
if ( 1 !== form_data['blockUI.isBlocked'] ) {
|
2015-01-06 17:39:09 +00:00
|
|
|
$form.block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
background: '#fff',
|
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-04-07 11:42:54 +00:00
|
|
|
|
2015-12-03 10:51:39 +00:00
|
|
|
// ajaxSetup is global, but we use it to ensure JSON is valid once returned.
|
|
|
|
$.ajaxSetup( {
|
|
|
|
dataFilter: function( raw_response, dataType ) {
|
|
|
|
// We only want to work with JSON
|
|
|
|
if ( 'json' !== dataType ) {
|
|
|
|
return raw_response;
|
|
|
|
}
|
2015-12-02 22:31:43 +00:00
|
|
|
|
2017-05-25 10:20:05 +00:00
|
|
|
if ( wc_checkout_form.is_valid_json( raw_response ) ) {
|
|
|
|
return raw_response;
|
|
|
|
} else {
|
2016-06-06 17:20:10 +00:00
|
|
|
// Attempt to fix the malformed JSON
|
2017-05-25 10:20:05 +00:00
|
|
|
var maybe_valid_json = raw_response.match( /{"result.*}/ );
|
2015-12-02 22:31:43 +00:00
|
|
|
|
2017-05-25 10:20:05 +00:00
|
|
|
if ( null === maybe_valid_json ) {
|
2015-12-02 22:31:43 +00:00
|
|
|
console.log( 'Unable to fix malformed JSON' );
|
2017-05-25 10:20:05 +00:00
|
|
|
} else if ( wc_checkout_form.is_valid_json( maybe_valid_json[0] ) ) {
|
2015-12-03 10:51:39 +00:00
|
|
|
console.log( 'Fixed malformed JSON. Original:' );
|
|
|
|
console.log( raw_response );
|
2017-05-25 10:20:05 +00:00
|
|
|
raw_response = maybe_valid_json[0];
|
|
|
|
} else {
|
|
|
|
console.log( 'Unable to fix malformed JSON' );
|
2015-12-02 22:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return raw_response;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: wc_checkout_params.checkout_url,
|
|
|
|
data: $form.serialize(),
|
2015-05-15 12:51:51 +00:00
|
|
|
dataType: 'json',
|
|
|
|
success: function( result ) {
|
2015-01-06 17:39:09 +00:00
|
|
|
try {
|
2017-03-06 19:52:38 +00:00
|
|
|
if ( 'success' === result.result ) {
|
2015-07-09 18:04:00 +00:00
|
|
|
if ( -1 === result.redirect.indexOf( 'https://' ) || -1 === result.redirect.indexOf( 'http://' ) ) {
|
2015-01-06 17:39:09 +00:00
|
|
|
window.location = result.redirect;
|
|
|
|
} else {
|
|
|
|
window.location = decodeURI( result.redirect );
|
|
|
|
}
|
2017-03-06 19:52:38 +00:00
|
|
|
} else if ( 'failure' === result.result ) {
|
2015-01-06 17:39:09 +00:00
|
|
|
throw 'Result failure';
|
|
|
|
} else {
|
|
|
|
throw 'Invalid response';
|
|
|
|
}
|
2015-05-15 12:51:51 +00:00
|
|
|
} catch( err ) {
|
|
|
|
// Reload page
|
2017-03-06 19:52:38 +00:00
|
|
|
if ( true === result.reload ) {
|
2015-01-06 17:39:09 +00:00
|
|
|
window.location.reload();
|
|
|
|
return;
|
|
|
|
}
|
2014-04-07 11:42:54 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
// Trigger update in case we need a fresh nonce
|
2017-03-06 19:52:38 +00:00
|
|
|
if ( true === result.refresh ) {
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).trigger( 'update_checkout' );
|
2015-01-06 17:39:09 +00:00
|
|
|
}
|
2012-12-28 13:02:12 +00:00
|
|
|
|
2015-05-15 12:51:51 +00:00
|
|
|
// Add new errors
|
|
|
|
if ( result.messages ) {
|
|
|
|
wc_checkout_form.submit_error( result.messages );
|
|
|
|
} else {
|
|
|
|
wc_checkout_form.submit_error( '<div class="woocommerce-error">' + wc_checkout_params.i18n_checkout_error + '</div>' );
|
|
|
|
}
|
2015-01-06 17:39:09 +00:00
|
|
|
}
|
|
|
|
},
|
2015-05-15 12:51:51 +00:00
|
|
|
error: function( jqXHR, textStatus, errorThrown ) {
|
|
|
|
wc_checkout_form.submit_error( '<div class="woocommerce-error">' + errorThrown + '</div>' );
|
|
|
|
}
|
2015-01-06 17:39:09 +00:00
|
|
|
});
|
|
|
|
}
|
2012-07-17 14:09:18 +00:00
|
|
|
|
|
|
|
return false;
|
2015-05-15 12:51:51 +00:00
|
|
|
},
|
|
|
|
submit_error: function( error_message ) {
|
2017-03-07 20:15:07 +00:00
|
|
|
$( '.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message' ).remove();
|
2017-03-06 22:45:45 +00:00
|
|
|
wc_checkout_form.$checkout_form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + error_message + '</div>' );
|
2015-05-15 12:51:51 +00:00
|
|
|
wc_checkout_form.$checkout_form.removeClass( 'processing' ).unblock();
|
2016-07-05 22:21:51 +00:00
|
|
|
wc_checkout_form.$checkout_form.find( '.input-text, select, input:checkbox' ).blur();
|
2015-05-15 12:51:51 +00:00
|
|
|
$( 'html, body' ).animate({
|
|
|
|
scrollTop: ( $( 'form.checkout' ).offset().top - 100 )
|
|
|
|
}, 1000 );
|
|
|
|
$( document.body ).trigger( 'checkout_error' );
|
2015-04-13 15:37:22 +00:00
|
|
|
}
|
2015-01-06 17:39:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var wc_checkout_coupons = {
|
|
|
|
init: function() {
|
2015-04-03 12:21:47 +00:00
|
|
|
$( document.body ).on( 'click', 'a.showcoupon', this.show_coupon_form );
|
|
|
|
$( document.body ).on( 'click', '.woocommerce-remove-coupon', this.remove_coupon );
|
2015-01-06 17:39:09 +00:00
|
|
|
$( 'form.checkout_coupon' ).hide().submit( this.submit );
|
|
|
|
},
|
2015-07-09 18:04:00 +00:00
|
|
|
show_coupon_form: function() {
|
|
|
|
$( '.checkout_coupon' ).slideToggle( 400, function() {
|
|
|
|
$( '.checkout_coupon' ).find( ':input:eq(0)' ).focus();
|
2015-01-06 17:39:09 +00:00
|
|
|
});
|
|
|
|
return false;
|
2015-04-13 15:37:22 +00:00
|
|
|
},
|
2015-07-09 18:04:00 +00:00
|
|
|
submit: function() {
|
2015-01-06 17:39:09 +00:00
|
|
|
var $form = $( this );
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2015-07-09 18:04:00 +00:00
|
|
|
if ( $form.is( '.processing' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
$form.addClass( 'processing' ).block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
background: '#fff',
|
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
});
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
var data = {
|
|
|
|
security: wc_checkout_params.apply_coupon_nonce,
|
2015-10-30 15:23:12 +00:00
|
|
|
coupon_code: $form.find( 'input[name="coupon_code"]' ).val()
|
2015-01-06 17:39:09 +00:00
|
|
|
};
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
$.ajax({
|
2014-03-19 03:42:53 +00:00
|
|
|
type: 'POST',
|
2015-07-27 16:55:37 +00:00
|
|
|
url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'apply_coupon' ),
|
2015-01-06 17:39:09 +00:00
|
|
|
data: data,
|
2014-03-19 03:42:53 +00:00
|
|
|
success: function( code ) {
|
2015-01-06 17:39:09 +00:00
|
|
|
$( '.woocommerce-error, .woocommerce-message' ).remove();
|
|
|
|
$form.removeClass( 'processing' ).unblock();
|
2013-09-04 10:26:19 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
if ( code ) {
|
|
|
|
$form.before( code );
|
|
|
|
$form.slideUp();
|
2013-09-04 10:26:19 +00:00
|
|
|
|
2015-11-20 14:51:38 +00:00
|
|
|
$( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
|
2013-09-04 10:26:19 +00:00
|
|
|
}
|
|
|
|
},
|
2014-03-19 03:42:53 +00:00
|
|
|
dataType: 'html'
|
2012-07-17 14:09:18 +00:00
|
|
|
});
|
2012-12-10 11:06:14 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
return false;
|
2015-04-13 15:37:22 +00:00
|
|
|
},
|
|
|
|
remove_coupon: function( e ) {
|
|
|
|
e.preventDefault();
|
2015-01-06 17:39:09 +00:00
|
|
|
|
|
|
|
var container = $( this ).parents( '.woocommerce-checkout-review-order' ),
|
|
|
|
coupon = $( this ).data( 'coupon' );
|
|
|
|
|
|
|
|
container.addClass( 'processing' ).block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
background: '#fff',
|
|
|
|
opacity: 0.6
|
2012-12-28 13:02:12 +00:00
|
|
|
}
|
2015-01-06 17:39:09 +00:00
|
|
|
});
|
2014-12-08 06:15:03 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
var data = {
|
|
|
|
security: wc_checkout_params.remove_coupon_nonce,
|
|
|
|
coupon: coupon
|
|
|
|
};
|
2014-12-08 06:15:03 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
2015-07-27 16:55:37 +00:00
|
|
|
url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_coupon' ),
|
2015-01-06 17:39:09 +00:00
|
|
|
data: data,
|
|
|
|
success: function( code ) {
|
|
|
|
$( '.woocommerce-error, .woocommerce-message' ).remove();
|
|
|
|
container.removeClass( 'processing' ).unblock();
|
2014-12-08 06:15:03 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
if ( code ) {
|
2015-02-02 11:53:07 +00:00
|
|
|
$( 'form.woocommerce-checkout' ).before( code );
|
2014-12-08 06:15:03 +00:00
|
|
|
|
2015-11-20 14:51:38 +00:00
|
|
|
$( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
|
2014-12-08 06:15:03 +00:00
|
|
|
|
2016-06-06 17:20:10 +00:00
|
|
|
// Remove coupon code from coupon field
|
2015-01-06 17:39:09 +00:00
|
|
|
$( 'form.checkout_coupon' ).find( 'input[name="coupon_code"]' ).val( '' );
|
|
|
|
}
|
|
|
|
},
|
2015-07-09 18:04:00 +00:00
|
|
|
error: function ( jqXHR ) {
|
2015-01-26 14:24:00 +00:00
|
|
|
if ( wc_checkout_params.debug_mode ) {
|
2016-06-06 17:20:10 +00:00
|
|
|
/* jshint devel: true */
|
2015-01-26 14:24:00 +00:00
|
|
|
console.log( jqXHR.responseText );
|
|
|
|
}
|
|
|
|
},
|
2015-01-06 17:39:09 +00:00
|
|
|
dataType: 'html'
|
|
|
|
});
|
2015-04-13 15:37:22 +00:00
|
|
|
}
|
2015-07-09 18:04:00 +00:00
|
|
|
};
|
2014-12-08 06:15:03 +00:00
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
var wc_checkout_login_form = {
|
|
|
|
init: function() {
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).on( 'click', 'a.showlogin', this.show_login_form );
|
2015-01-06 17:39:09 +00:00
|
|
|
},
|
2015-07-09 18:04:00 +00:00
|
|
|
show_login_form: function() {
|
2016-12-20 16:16:55 +00:00
|
|
|
$( 'form.login, form.woocommerce-form--login' ).slideToggle();
|
2015-01-06 17:39:09 +00:00
|
|
|
return false;
|
2015-04-13 15:37:22 +00:00
|
|
|
}
|
2015-07-09 18:04:00 +00:00
|
|
|
};
|
2015-01-06 17:39:09 +00:00
|
|
|
|
2017-01-17 15:18:23 +00:00
|
|
|
var wc_terms_toggle = {
|
|
|
|
init: function() {
|
|
|
|
$( document.body ).on( 'click', 'a.woocommerce-terms-and-conditions-link', this.toggle_terms );
|
|
|
|
},
|
|
|
|
|
|
|
|
toggle_terms: function() {
|
2017-07-04 13:03:08 +00:00
|
|
|
if ( $( '.woocommerce-terms-and-conditions' ).length ) {
|
|
|
|
$( '.woocommerce-terms-and-conditions' ).slideToggle();
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-17 15:18:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-06 17:39:09 +00:00
|
|
|
wc_checkout_form.init();
|
|
|
|
wc_checkout_coupons.init();
|
|
|
|
wc_checkout_login_form.init();
|
2017-01-17 15:18:23 +00:00
|
|
|
wc_terms_toggle.init();
|
2013-06-13 04:28:46 +00:00
|
|
|
});
|