2015-01-13 12:03:50 +00:00
|
|
|
/*global wc_users_params */
|
|
|
|
jQuery( function ( $ ) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Users country and state fields
|
|
|
|
*/
|
|
|
|
var wc_users_fields = {
|
|
|
|
states: null,
|
|
|
|
init: function() {
|
2015-07-10 05:13:30 +00:00
|
|
|
if ( typeof wc_users_params.countries !== 'undefined' ) {
|
2015-01-13 12:03:50 +00:00
|
|
|
/* State/Country select boxes */
|
2020-10-22 13:11:27 +00:00
|
|
|
this.states = JSON.parse( wc_users_params.countries.replace( /"/g, '"' ) );
|
2015-01-13 12:03:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 19:44:33 +00:00
|
|
|
$( '.js_field-country' ).selectWoo().change( this.change_country );
|
2015-01-13 12:03:50 +00:00
|
|
|
$( '.js_field-country' ).trigger( 'change', [ true ] );
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).on( 'change', 'select.js_field-state', this.change_state );
|
2017-05-15 21:09:02 +00:00
|
|
|
|
|
|
|
$( document.body ).on( 'click', 'button.js_copy-billing', this.copy_billing );
|
2015-01-13 12:03:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
change_country: function( e, stickValue ) {
|
|
|
|
// Check for stickValue before using it
|
|
|
|
if ( typeof stickValue === 'undefined' ) {
|
|
|
|
stickValue = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent if we don't have the metabox data
|
|
|
|
if ( wc_users_fields.states === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var $this = $( this ),
|
|
|
|
country = $this.val(),
|
|
|
|
$state = $this.parents( '.form-table' ).find( ':input.js_field-state' ),
|
|
|
|
$parent = $state.parent(),
|
|
|
|
input_name = $state.attr( 'name' ),
|
|
|
|
input_id = $state.attr( 'id' ),
|
2019-04-09 15:54:22 +00:00
|
|
|
stickstatefield = 'woocommerce.stickState-' + country,
|
|
|
|
value = $this.data( stickstatefield ) ? $this.data( stickstatefield ) : $state.val(),
|
|
|
|
placeholder = $state.attr( 'placeholder' ),
|
|
|
|
$newstate;
|
2015-01-13 12:03:50 +00:00
|
|
|
|
|
|
|
if ( stickValue ){
|
|
|
|
$this.data( 'woocommerce.stickState-' + country, value );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the previous DOM element
|
|
|
|
$parent.show().find( '.select2-container' ).remove();
|
|
|
|
|
|
|
|
if ( ! $.isEmptyObject( wc_users_fields.states[ country ] ) ) {
|
2019-04-09 15:54:22 +00:00
|
|
|
var state = wc_users_fields.states[ country ],
|
|
|
|
$defaultOption = $( '<option value=""></option>' )
|
|
|
|
.text( wc_users_fields.i18n_select_state_text );
|
2015-01-13 12:03:50 +00:00
|
|
|
|
2019-04-09 15:54:22 +00:00
|
|
|
$newstate = $( '<select style="width: 25em;"></select>' )
|
|
|
|
.prop( 'id', input_id )
|
|
|
|
.prop( 'name', input_name )
|
|
|
|
.prop( 'placeholder', placeholder )
|
|
|
|
.addClass( 'js_field-state' )
|
|
|
|
.append( $defaultOption );
|
2015-01-13 12:03:50 +00:00
|
|
|
|
2015-07-10 04:32:30 +00:00
|
|
|
$.each( state, function( index ) {
|
2019-04-09 15:54:22 +00:00
|
|
|
var $option = $( '<option></option>' )
|
|
|
|
.prop( 'value', index )
|
|
|
|
.text( state[ index ] );
|
|
|
|
$newstate.append( $option );
|
2015-01-13 12:03:50 +00:00
|
|
|
} );
|
|
|
|
|
2019-04-09 15:54:22 +00:00
|
|
|
$newstate.val( value );
|
2015-01-13 12:03:50 +00:00
|
|
|
|
2019-04-09 15:54:22 +00:00
|
|
|
$state.replaceWith( $newstate );
|
2015-01-13 12:03:50 +00:00
|
|
|
|
2021-01-07 22:08:36 +00:00
|
|
|
$newstate.show().selectWoo().hide().trigger( 'change' );
|
2015-01-13 12:03:50 +00:00
|
|
|
} else {
|
2019-04-09 15:54:22 +00:00
|
|
|
$newstate = $( '<input type="text" />' )
|
|
|
|
.prop( 'id', input_id )
|
|
|
|
.prop( 'name', input_name )
|
|
|
|
.prop( 'placeholder', placeholder )
|
|
|
|
.addClass( 'js_field-state regular-text' )
|
|
|
|
.val( value );
|
|
|
|
$state.replaceWith( $newstate );
|
2015-01-13 12:03:50 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 09:04:23 +00:00
|
|
|
// This event has a typo - deprecated in 2.5.0
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).trigger( 'contry-change.woocommerce', [country, $( this ).closest( 'div' )] );
|
2015-10-01 09:04:23 +00:00
|
|
|
$( document.body ).trigger( 'country-change.woocommerce', [country, $( this ).closest( 'div' )] );
|
2015-01-13 12:03:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
change_state: function() {
|
|
|
|
// Here we will find if state value on a select has changed and stick it to the country data
|
|
|
|
var $this = $( this ),
|
|
|
|
state = $this.val(),
|
|
|
|
$country = $this.parents( '.form-table' ).find( ':input.js_field-country' ),
|
|
|
|
country = $country.val();
|
|
|
|
|
|
|
|
$country.data( 'woocommerce.stickState-' + country, state );
|
2017-05-15 21:09:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
copy_billing: function( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
$( '#fieldset-billing' ).find( 'input, select' ).each( function( i, el ) {
|
|
|
|
// The address keys match up, except for the prefix
|
|
|
|
var shipName = el.name.replace( /^billing_/, 'shipping_' );
|
|
|
|
// Swap prefix, then check if there are any elements
|
|
|
|
var shipEl = $( '[name="' + shipName + '"]' );
|
|
|
|
// No corresponding shipping field, skip this item
|
|
|
|
if ( ! shipEl.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Found a matching shipping element, update the value
|
|
|
|
shipEl.val( el.value ).trigger( 'change' );
|
|
|
|
} );
|
2015-07-10 04:32:30 +00:00
|
|
|
}
|
|
|
|
};
|
2015-01-13 12:03:50 +00:00
|
|
|
|
|
|
|
wc_users_fields.init();
|
|
|
|
|
|
|
|
});
|