woocommerce/assets/js/frontend/checkout.js

358 lines
10 KiB
JavaScript
Raw Normal View History

jQuery(document).ready(function($) {
var updateTimer;
2012-07-20 12:46:05 +00:00
var dirtyInput = false;
var xhr;
2012-12-10 11:06:14 +00:00
function update_checkout() {
2012-12-10 11:06:14 +00:00
if (xhr) xhr.abort();
2012-12-10 11:06:14 +00:00
if ( $('select#shipping_method').size() > 0 )
var method = $('select#shipping_method').val();
else
var method = $('input[name=shipping_method]:checked').val();
2012-12-10 11:06:14 +00:00
var payment_method = $('#order_review input[name=payment_method]:checked').val();
var country = $('#billing_country').val();
var state = $('#billing_state').val();
2012-09-23 16:16:39 +00:00
var postcode = $('input#billing_postcode').val();
2012-12-10 11:06:14 +00:00
var city = $('input#billing_city').val();
var address = $('input#billing_address_1').val();
var address_2 = $('input#billing_address_2').val();
if ( $('#shiptobilling input').is(':checked') || $('#shiptobilling input').size() == 0 ) {
var s_country = country;
var s_state = state;
var s_postcode = postcode;
2012-09-23 16:16:39 +00:00
var s_city = city;
var s_address = address;
var s_address_2 = address_2;
} else {
var s_country = $('#shipping_country').val();
var s_state = $('#shipping_state').val();
var s_postcode = $('input#shipping_postcode').val();
2012-09-23 16:16:39 +00:00
var s_city = $('input#shipping_city').val();
var s_address = $('input#shipping_address_1').val();
var s_address_2 = $('input#shipping_address_2').val();
}
2012-12-10 11:06:14 +00:00
2012-08-25 09:32:14 +00:00
$('#order_methods, #order_review').block({message: null, overlayCSS: {background: '#fff url(' + woocommerce_params.ajax_loader_url + ') no-repeat center', opacity: 0.6}});
2012-12-10 11:06:14 +00:00
var data = {
action: 'woocommerce_update_order_review',
security: woocommerce_params.update_order_review_nonce,
2012-12-10 11:06:14 +00:00
shipping_method: method,
payment_method: payment_method,
2012-12-10 11:06:14 +00:00
country: country,
state: state,
postcode: postcode,
2012-09-23 16:16:39 +00:00
city: city,
address: address,
address_2: address_2,
2012-12-10 11:06:14 +00:00
s_country: s_country,
s_state: s_state,
s_postcode: s_postcode,
2012-09-23 16:16:39 +00:00
s_city: s_city,
s_address: s_address,
s_address_2: s_address_2,
post_data: $('form.checkout').serialize()
};
2012-12-10 11:06:14 +00:00
xhr = $.ajax({
type: 'POST',
url: woocommerce_params.ajax_url,
data: data,
success: function( response ) {
if ( response ) {
$('#order_review').after(response).remove();
2012-12-10 11:06:14 +00:00
$('#order_review input[name=payment_method]:checked');
$('body').trigger('updated_checkout');
}
}
});
2012-12-10 11:06:14 +00:00
}
2012-12-10 11:06:14 +00:00
// Event for updating the checkout
$('body').bind('update_checkout', function() {
clearTimeout(updateTimer);
update_checkout();
});
2012-12-10 11:06:14 +00:00
$('p.password, form.login, .checkout_coupon, div.shipping_address').hide();
2012-12-10 11:06:14 +00:00
$('input.show_password').change(function(){
$('p.password').slideToggle();
});
2012-12-10 11:06:14 +00:00
$('a.showlogin').click(function(){
$('form.login').slideToggle();
return false;
});
2012-12-10 11:06:14 +00:00
$('a.showcoupon').click(function(){
$('.checkout_coupon').slideToggle();
return false;
});
2012-12-10 11:06:14 +00:00
$('#shiptobilling input').change(function(){
$('div.shipping_address').hide();
if (!$(this).is(':checked')) {
$('div.shipping_address').slideDown();
}
}).change();
2012-12-10 11:06:14 +00:00
if (woocommerce_params.option_guest_checkout=='yes') {
2012-12-10 11:06:14 +00:00
$('div.create-account').hide();
2012-12-10 11:06:14 +00:00
$('input#createaccount').change(function(){
$('div.create-account').hide();
if ($(this).is(':checked')) {
$('div.create-account').slideDown();
}
}).change();
2012-12-10 11:06:14 +00:00
}
2012-12-10 11:06:14 +00:00
2012-11-07 21:23:45 +00:00
$('.payment_methods input.input-radio').live('click', function(){
if ( $('.payment_methods input.input-radio').length > 1 ) {
$('div.payment_box').filter(':visible').slideUp(250);
if ($(this).is(':checked')) {
$('div.payment_box.' + $(this).attr('ID')).slideDown(250);
}
2012-11-07 21:23:45 +00:00
} else {
$('div.payment_box').show();
}
});
2012-12-10 11:06:14 +00:00
$('#order_review input[name=payment_method]:checked').click();
2012-12-10 11:06:14 +00:00
/* Update totals */
2012-07-20 12:46:05 +00:00
// Inputs/selects which update totals instantly
$('select#shipping_method, input[name=shipping_method], #shiptobilling input, .update_totals_on_change select').live('change', function(){
clearTimeout( updateTimer );
2012-07-20 12:46:05 +00:00
$('body').trigger('update_checkout');
});
2012-12-10 11:06:14 +00:00
2012-07-20 12:46:05 +00:00
// Inputs which update totals on change
function input_changed() {
dirtyInput = false;
$('body').trigger('update_checkout');
2012-07-20 12:46:05 +00:00
}
$('.update_totals_on_change input').live('change', function(){
if ( dirtyInput ) {
clearTimeout( updateTimer );
$('body').trigger('update_checkout');
}
});
$('.update_totals_on_change input.input-text').live('keydown', function( e ){
2012-07-20 12:46:05 +00:00
var code = e.keyCode || e.which;
if ( code == '9' )
return;
dirtyInput = true;
clearTimeout( updateTimer );
updateTimer = setTimeout( input_changed, '1000' );
});
2012-12-10 11:06:14 +00:00
/* AJAX Coupon Form Submission */
$('form.checkout_coupon').submit( function() {
var $form = $(this);
2012-12-10 11:06:14 +00:00
if ( $form.is('.processing') ) return false;
2012-12-10 11:06:14 +00:00
2012-08-25 09:32:14 +00:00
$form.addClass('processing').block({message: null, overlayCSS: {background: '#fff url(' + woocommerce_params.ajax_loader_url + ') no-repeat center', opacity: 0.6}});
2012-12-10 11:06:14 +00:00
var data = {
action: 'woocommerce_apply_coupon',
security: woocommerce_params.apply_coupon_nonce,
coupon_code: $form.find('input[name=coupon_code]').val()
};
2012-12-10 11:06:14 +00:00
$.ajax({
type: 'POST',
url: woocommerce_params.ajax_url,
data: data,
success: function( code ) {
$('.woocommerce_error, .woocommerce_message').remove();
2012-12-10 11:06:14 +00:00
$form.removeClass('processing').unblock();
if ( code ) {
$form.before( code );
$form.slideUp();
2012-12-10 11:06:14 +00:00
$('body').trigger('update_checkout');
}
},
dataType: "html"
});
return false;
});
2012-12-10 11:06:14 +00:00
/* AJAX Form Submission */
$('form.checkout').submit( function() {
2012-07-20 12:46:05 +00:00
clearTimeout( updateTimer );
2012-12-10 11:06:14 +00:00
var $form = $(this);
2012-12-10 11:06:14 +00:00
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 ) {
2012-12-10 11:06:14 +00:00
$form.addClass('processing');
var form_data = $form.data();
2012-12-10 11:06:14 +00:00
if ( form_data["blockUI.isBlocked"] != 1 )
2012-08-25 09:32:14 +00:00
$form.block({message: null, overlayCSS: {background: '#fff url(' + woocommerce_params.ajax_loader_url + ') no-repeat center', opacity: 0.6}});
2012-12-10 11:06:14 +00:00
$.ajax({
type: 'POST',
url: woocommerce_params.checkout_url,
data: $form.serialize(),
success: function( code ) {
try {
2012-12-10 11:06:14 +00:00
result = $.parseJSON( code );
if (result.result=='success') {
2012-12-10 11:06:14 +00:00
window.location = decodeURI(result.redirect);
2012-12-10 11:06:14 +00:00
} else if (result.result=='failure') {
2012-12-10 11:06:14 +00:00
$('.woocommerce_error, .woocommerce_message').remove();
$form.prepend( result.messages );
2012-12-10 11:06:14 +00:00
$form.removeClass('processing').unblock();
if (result.refresh=='true') $('body').trigger('update_checkout');
2012-12-10 11:06:14 +00:00
$('html, body').animate({
scrollTop: ($('form.checkout').offset().top - 100)
}, 1000);
2012-12-10 11:06:14 +00:00
} else {
throw "Invalid response";
}
}
catch(err) {
$('.woocommerce_error, .woocommerce_message').remove();
$form.prepend( code );
2012-12-10 11:06:14 +00:00
$form.removeClass('processing').unblock();
$('html, body').animate({
scrollTop: ($('form.checkout').offset().top - 100)
}, 1000);
}
},
dataType: "html"
});
2012-12-10 11:06:14 +00:00
}
return false;
});
2012-12-10 11:06:14 +00:00
/* Localisation */
var locale_json = woocommerce_params.locale.replace(/"/g, '"');
var locale = $.parseJSON( locale_json );
var required = ' <abbr class="required" title="' + woocommerce_params.required_text + '">*</abbr>';
// Handle locale
$('body').bind('country_to_state_changing', function( event, country, wrapper ){
2012-12-10 11:06:14 +00:00
var thisform = wrapper;
2012-12-10 11:06:14 +00:00
if ( locale[country] ) {
var thislocale = locale[country];
} else {
var thislocale = locale['default'];
}
2012-12-10 11:06:14 +00:00
// Handle locale fields
2012-12-10 11:06:14 +00:00
var locale_fields = {
'address_1' : '#billing_address_1_field, #shipping_address_1_field',
'address_2' : '#billing_address_2_field, #shipping_address_2_field',
'state' : '#billing_state_field, #shipping_state_field',
'postcode' : '#billing_postcode_field, #shipping_postcode_field',
'city' : '#billing_city_field, #shipping_city_field'
2012-12-10 11:06:14 +00:00
};
$.each( locale_fields, function( key, value ) {
var field = thisform.find( value );
2012-12-10 11:06:14 +00:00
if ( thislocale[key] ) {
2012-12-10 11:06:14 +00:00
if ( thislocale[key]['label'] ) {
field.find('label').html( thislocale[key]['label'] );
}
2012-12-10 11:06:14 +00:00
if ( thislocale[key]['placeholder'] ) {
field.find('input').attr( 'placeholder', thislocale[key]['placeholder'] );
2012-12-10 11:06:14 +00:00
}
field.find('label abbr').remove();
if ( typeof thislocale[key]['required'] == 'undefined' || thislocale[key]['required'] == true ) {
field.find('label').append( required );
}
2012-12-10 11:06:14 +00:00
if ( key !== 'state' ) {
if ( thislocale[key]['hidden'] == true ) {
field.fadeOut(200).find('input').val('');
} else {
field.fadeIn(500);
}
}
2012-12-10 11:06:14 +00:00
} else if ( locale['default'][key] ) {
if ( locale['default'][key]['required'] == true ) {
if (field.find('label abbr').size()==0) field.find('label').append( required );
}
if ( key !== 'state' && (typeof locale['default'][key]['hidden'] == 'undefined' || locale['default'][key]['hidden'] == false) ) {
field.fadeIn(500);
}
}
2012-12-10 11:06:14 +00:00
});
var postcodefield = thisform.find('#billing_postcode_field, #shipping_postcode_field');
var cityfield = thisform.find('#billing_city_field, #shipping_city_field');
2012-12-10 11:06:14 +00:00
// Re-order postcode/city
if ( thislocale['postcode_before_city'] ) {
if (cityfield.is('.form-row-first')) {
cityfield.fadeOut(200, function() {
cityfield.removeClass('form-row-first').addClass('form-row-last').insertAfter( postcodefield ).fadeIn(500);
});
postcodefield.fadeOut(200, function (){
postcodefield.removeClass('form-row-last').addClass('form-row-first').fadeIn(500);
});
}
} else {
if (cityfield.is('.form-row-last')) {
cityfield.fadeOut(200, function() {
cityfield.removeClass('form-row-last').addClass('form-row-first').insertBefore( postcodefield ).fadeIn(500);
});
postcodefield.fadeOut(200, function (){
postcodefield.removeClass('form-row-first').addClass('form-row-last').fadeIn(500);
});
}
}
2012-12-10 11:06:14 +00:00
});
2012-12-10 11:06:14 +00:00
2012-09-03 15:45:09 +00:00
// Init trigger
$('body').bind('init_checkout', function() {
$('#billing_country, #shipping_country, .country_to_state').change();
2012-09-03 15:45:09 +00:00
$('body').trigger('update_checkout');
});
2012-12-10 11:06:14 +00:00
2012-09-03 15:45:09 +00:00
// Update on page load
2012-12-10 11:06:14 +00:00
if ( woocommerce_params.is_checkout == 1 )
2012-09-03 15:45:09 +00:00
$('body').trigger('init_checkout');
2012-12-10 11:06:14 +00:00
});