users.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*global wc_users_params */
  2. jQuery( function ( $ ) {
  3. /**
  4. * Users country and state fields
  5. */
  6. var wc_users_fields = {
  7. states: null,
  8. init: function() {
  9. if ( typeof wc_users_params.countries !== 'undefined' ) {
  10. /* State/Country select boxes */
  11. this.states = $.parseJSON( wc_users_params.countries.replace( /"/g, '"' ) );
  12. }
  13. $( '.js_field-country' ).selectWoo().change( this.change_country );
  14. $( '.js_field-country' ).trigger( 'change', [ true ] );
  15. $( document.body ).on( 'change', 'select.js_field-state', this.change_state );
  16. $( document.body ).on( 'click', 'button.js_copy-billing', this.copy_billing );
  17. },
  18. change_country: function( e, stickValue ) {
  19. // Check for stickValue before using it
  20. if ( typeof stickValue === 'undefined' ) {
  21. stickValue = false;
  22. }
  23. // Prevent if we don't have the metabox data
  24. if ( wc_users_fields.states === null ) {
  25. return;
  26. }
  27. var $this = $( this ),
  28. country = $this.val(),
  29. $state = $this.parents( '.form-table' ).find( ':input.js_field-state' ),
  30. $parent = $state.parent(),
  31. input_name = $state.attr( 'name' ),
  32. input_id = $state.attr( 'id' ),
  33. value = $this.data( 'woocommerce.stickState-' + country ) ? $this.data( 'woocommerce.stickState-' + country ) : $state.val();
  34. if ( stickValue ){
  35. $this.data( 'woocommerce.stickState-' + country, value );
  36. }
  37. // Remove the previous DOM element
  38. $parent.show().find( '.select2-container' ).remove();
  39. if ( ! $.isEmptyObject( wc_users_fields.states[ country ] ) ) {
  40. var $states_select = $( '<select name="' + input_name + '" id="' + input_id + '" class="js_field-state" style="width: 25em;"></select>' ),
  41. state = wc_users_fields.states[ country ];
  42. $states_select.append( $( '<option value="">' + wc_users_params.i18n_select_state_text + '</option>' ) );
  43. $.each( state, function( index ) {
  44. $states_select.append( $( '<option value="' + index + '">' + state[ index ] + '</option>' ) );
  45. } );
  46. $states_select.val( value );
  47. $state.replaceWith( $states_select );
  48. $states_select.show().selectWoo().hide().change();
  49. } else {
  50. $state.replaceWith( '<input type="text" class="js_field-state" name="' + input_name + '" id="' + input_id + '" value="' + value + '" />' );
  51. }
  52. // This event has a typo - deprecated in 2.5.0
  53. $( document.body ).trigger( 'contry-change.woocommerce', [country, $( this ).closest( 'div' )] );
  54. $( document.body ).trigger( 'country-change.woocommerce', [country, $( this ).closest( 'div' )] );
  55. },
  56. change_state: function() {
  57. // Here we will find if state value on a select has changed and stick it to the country data
  58. var $this = $( this ),
  59. state = $this.val(),
  60. $country = $this.parents( '.form-table' ).find( ':input.js_field-country' ),
  61. country = $country.val();
  62. $country.data( 'woocommerce.stickState-' + country, state );
  63. },
  64. copy_billing: function( event ) {
  65. event.preventDefault();
  66. $( '#fieldset-billing' ).find( 'input, select' ).each( function( i, el ) {
  67. // The address keys match up, except for the prefix
  68. var shipName = el.name.replace( /^billing_/, 'shipping_' );
  69. // Swap prefix, then check if there are any elements
  70. var shipEl = $( '[name="' + shipName + '"]' );
  71. // No corresponding shipping field, skip this item
  72. if ( ! shipEl.length ) {
  73. return;
  74. }
  75. // Found a matching shipping element, update the value
  76. shipEl.val( el.value ).trigger( 'change' );
  77. } );
  78. }
  79. };
  80. wc_users_fields.init();
  81. });