| 1 |
// Most functionality copied from woocommerce/assets/js/checkout.js |
| 2 |
// Thanks Mike! |
| 3 |
|
| 4 |
jQuery(document).ready(function($) { |
| 5 |
|
| 6 |
// Hide custom NL fields by default when country not NL |
| 7 |
var billing_country = $('#billing_country').val(); |
| 8 |
var shipping_country = $('#shipping_country').val(); |
| 9 |
if (billing_country != 'NL') { |
| 10 |
$('#billing_street_name_field').hide(); |
| 11 |
$('#billing_house_number_field').hide(); |
| 12 |
$('#billing_house_number_suffix_field').hide(); |
| 13 |
} |
| 14 |
if (shipping_country != 'NL') { |
| 15 |
$('#shipping_street_name_field').hide(); |
| 16 |
$('#shipping_house_number_field').hide(); |
| 17 |
$('#shipping_house_number_suffix_field').hide(); |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
/* Localisation */ |
| 22 |
var locale_json = woocommerce_params.locale.replace(/"/g, '"'); |
| 23 |
var locale = $.parseJSON( locale_json ); |
| 24 |
var required = ' <abbr class="required" title="' + woocommerce_params.i18n_required_text + '">*</abbr>'; |
| 25 |
|
| 26 |
$('body') |
| 27 |
|
| 28 |
// Handle locale |
| 29 |
.bind('country_to_state_changing', function( event, country, wrapper ){ |
| 30 |
var thisform = wrapper; |
| 31 |
|
| 32 |
var $postcodefield = thisform.find('#billing_postcode_field, #shipping_postcode_field'); |
| 33 |
var $cityfield = thisform.find('#billing_city_field, #shipping_city_field'); |
| 34 |
var $statefield = thisform.find('#billing_state_field, #shipping_state_field'); |
| 35 |
var $emailfield = thisform.find('#billing_email_field'); |
| 36 |
var $phonefield = thisform.find('#billing_phone_field'); |
| 37 |
var $address1field = thisform.find('#billing_address_1_field, #shipping_address_1_field'); |
| 38 |
var $address2field = thisform.find('#billing_address_2_field, #shipping_address_2_field'); |
| 39 |
var $streetfield = thisform.find('#billing_street_name_field, #shipping_street_name_field'); |
| 40 |
var $numberfield = thisform.find('#billing_house_number_field, #shipping_house_number_field'); |
| 41 |
var $suffixfield = thisform.find('#billing_house_number_suffix_field, #shipping_house_number_suffix_field'); |
| 42 |
|
| 43 |
if (country == 'NL') { |
| 44 |
//show custom NL fields |
| 45 |
$streetfield.show(); |
| 46 |
$numberfield.show(); |
| 47 |
$suffixfield.show(); |
| 48 |
//$emailfield.add( $phonefield ).removeClass('form-row-first form-row-last').addClass('form-row-wide'); |
| 49 |
|
| 50 |
// Hide regular address classes |
| 51 |
$address1field.find('label abbr').remove(); |
| 52 |
$address1field.hide(); |
| 53 |
$address2field.hide(); |
| 54 |
|
| 55 |
// Place postcode & city on one line |
| 56 |
$postcodefield.removeClass('form-row-wide').addClass('form-row-first'); |
| 57 |
$cityfield.removeClass('form-row-wide').addClass('form-row-last'); |
| 58 |
|
| 59 |
// Mark required fields |
| 60 |
if ($streetfield.find('label abbr').size()==0) $streetfield.find('label').append( required ); |
| 61 |
if ($numberfield.find('label abbr').size()==0) $numberfield.find('label').append( required ); |
| 62 |
|
| 63 |
// Add validation required classes |
| 64 |
$streetfield.addClass('validate-required'); |
| 65 |
$numberfield.addClass('validate-required'); |
| 66 |
} else { |
| 67 |
// Hide custom NL fields |
| 68 |
$streetfield.hide(); |
| 69 |
$numberfield.hide(); |
| 70 |
$suffixfield.hide(); |
| 71 |
|
| 72 |
// Unmark required fields |
| 73 |
$streetfield.find('label abbr').remove(); |
| 74 |
$numberfield.find('label abbr').remove(); |
| 75 |
|
| 76 |
// Remove validation required classes |
| 77 |
$streetfield.removeClass('validate-required'); |
| 78 |
$numberfield.removeClass('validate-required'); |
| 79 |
} |
| 80 |
|
| 81 |
if ( typeof locale[country] != 'undefined' ) { |
| 82 |
var thislocale = locale[country]; |
| 83 |
} else { |
| 84 |
var thislocale = locale['default']; |
| 85 |
} |
| 86 |
|
| 87 |
// Handle locale fields |
| 88 |
var locale_fields = { |
| 89 |
'address_1' : '#billing_address_1_field, #shipping_address_1_field', |
| 90 |
'address_2' : '#billing_address_2_field, #shipping_address_2_field', |
| 91 |
}; |
| 92 |
|
| 93 |
$.each( locale_fields, function( key, value ) { |
| 94 |
|
| 95 |
var field = thisform.find( value ); |
| 96 |
|
| 97 |
if ( thislocale[key] ) { |
| 98 |
|
| 99 |
if ( thislocale[key]['label'] ) { |
| 100 |
field.find('label').html( thislocale[key]['label'] ); |
| 101 |
} |
| 102 |
|
| 103 |
if ( thislocale[key]['placeholder'] ) { |
| 104 |
field.find('input').attr( 'placeholder', thislocale[key]['placeholder'] ); |
| 105 |
} |
| 106 |
|
| 107 |
field.find('label abbr').remove(); |
| 108 |
|
| 109 |
if ( typeof thislocale[key]['required'] == 'undefined' && locale['default'][key]['required'] == true ) { |
| 110 |
field.find('label').append( required ); |
| 111 |
} else if ( thislocale[key]['required'] == true ) { |
| 112 |
field.find('label').append( required ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( key !== 'state' ) { |
| 116 |
if ( thislocale[key]['hidden'] == true ) { |
| 117 |
field.hide().find('input').val(''); |
| 118 |
} else { |
| 119 |
field.show(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
} else if ( locale['default'][key] ) { |
| 124 |
if ( locale['default'][key]['required'] == true ) { |
| 125 |
if (field.find('label abbr').size()==0) field.find('label').append( required ); |
| 126 |
} |
| 127 |
if ( key !== 'state' && (typeof locale['default'][key]['hidden'] == 'undefined' || locale['default'][key]['hidden'] == false) ) { |
| 128 |
field.show(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
}); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
}) |
| 137 |
|
| 138 |
// Init trigger |
| 139 |
.bind('init_checkout', function() { |
| 140 |
$('#billing_country, #shipping_country, .country_to_state').change(); |
| 141 |
}); |
| 142 |
|
| 143 |
// Update on page load |
| 144 |
if ( woocommerce_params.is_checkout == 1 ) { |
| 145 |
$('body').trigger('init_checkout'); |
| 146 |
} |
| 147 |
|
| 148 |
}); |