|
1 |
| -jQuery(function(){ |
| 1 | +/* global wc_eu_vat_params, wc_address_i18n_params */ |
| 2 | +/* eslint-disable camelcase */ |
| 3 | +jQuery( function () { |
| 4 | + const useShippingCountry = wc_eu_vat_params.use_shipping_country; |
| 5 | + const billingVatNumber = '#woocommerce_eu_vat_number_field'; |
| 6 | + const shippingVatNumber = '#woocommerce_eu_vat_number_shipping_field'; |
| 7 | + |
2 | 8 | function field_is_required( field, is_required ) {
|
3 | 9 | if ( is_required ) {
|
4 | 10 | field.find( 'label .optional' ).remove();
|
5 | 11 | field.addClass( 'validate-required' );
|
6 | 12 |
|
7 | 13 | if ( field.find( 'label .required' ).length === 0 ) {
|
8 |
| - field.find( 'label' ).append( |
9 |
| - ' <abbr class="required" title="' + |
10 |
| - wc_address_i18n_params.i18n_required_text + |
11 |
| - '">*</abbr>' |
12 |
| - ); |
| 14 | + field |
| 15 | + .find( 'label' ) |
| 16 | + .append( |
| 17 | + '<abbr class="required" title="' + |
| 18 | + wc_address_i18n_params.i18n_required_text + |
| 19 | + '">*</abbr>' |
| 20 | + ); |
13 | 21 | }
|
14 | 22 | } else {
|
15 | 23 | field.find( 'label .required' ).remove();
|
16 |
| - field.removeClass( 'validate-required woocommerce-invalid woocommerce-invalid-required-field' ); |
| 24 | + field.removeClass( |
| 25 | + 'validate-required woocommerce-invalid woocommerce-invalid-required-field' |
| 26 | + ); |
17 | 27 |
|
18 | 28 | if ( field.find( 'label .optional' ).length === 0 ) {
|
19 |
| - field.find( 'label' ).append( ' <span class="optional">(' + wc_address_i18n_params.i18n_optional_text + ')</span>' ); |
| 29 | + field |
| 30 | + .find( 'label' ) |
| 31 | + .append( |
| 32 | + '<span class="optional">(' + |
| 33 | + wc_address_i18n_params.i18n_optional_text + |
| 34 | + ')</span>' |
| 35 | + ); |
20 | 36 | }
|
21 | 37 | }
|
22 | 38 | }
|
23 | 39 |
|
24 |
| - jQuery( 'form.checkout, form#order_review').on( 'change', '#billing_country', function() { |
25 |
| - var country = jQuery( '#billing_country' ).val(); |
26 |
| - var check_countries = wc_eu_vat_params.eu_countries; |
27 |
| - var b2b_enabled = wc_eu_vat_params.b2b_required; |
| 40 | + // Handle change billing/shipping country. |
| 41 | + function changeCountry() { |
| 42 | + const billingCountry = jQuery( '#billing_country' ).val(); |
| 43 | + const shippingCountry = jQuery( '#shipping_country' ).val(); |
| 44 | + const shipToDifferent = jQuery( |
| 45 | + '#ship-to-different-address-checkbox' |
| 46 | + ).is( ':checked' ); |
| 47 | + const validateShippingCountry = |
| 48 | + useShippingCountry && shippingCountry && shipToDifferent; |
| 49 | + const country = validateShippingCountry |
| 50 | + ? shippingCountry |
| 51 | + : billingCountry; |
| 52 | + const check_countries = wc_eu_vat_params.eu_countries; |
| 53 | + const b2b_enabled = wc_eu_vat_params.b2b_required; |
| 54 | + |
| 55 | + const vat_number_field = validateShippingCountry |
| 56 | + ? jQuery( shippingVatNumber ) |
| 57 | + : jQuery( billingVatNumber ); |
28 | 58 |
|
29 |
| - field_is_required( jQuery( '#woocommerce_eu_vat_number_field' ), false ); |
| 59 | + field_is_required( vat_number_field, false ); |
30 | 60 |
|
31 | 61 | if ( country && jQuery.inArray( country, check_countries ) >= 0 ) {
|
32 |
| - jQuery( '#woocommerce_eu_vat_number_field' ).fadeIn(); |
33 |
| - if ( 'yes' === b2b_enabled ) { |
34 |
| - field_is_required( jQuery( '#woocommerce_eu_vat_number_field' ), true ); |
| 62 | + vat_number_field.fadeIn(); |
| 63 | + if ( b2b_enabled === 'yes' ) { |
| 64 | + field_is_required( vat_number_field, true ); |
35 | 65 | }
|
36 | 66 | } else {
|
37 |
| - jQuery( '#woocommerce_eu_vat_number_field' ).fadeOut(); |
| 67 | + vat_number_field.fadeOut(); |
38 | 68 | }
|
39 |
| - }); |
| 69 | + } |
| 70 | + |
| 71 | + jQuery( 'form.checkout, form#order_review' ).on( |
| 72 | + 'change', |
| 73 | + '#billing_country', |
| 74 | + changeCountry |
| 75 | + ); |
40 | 76 | jQuery( '#billing_country' ).trigger( 'change' );
|
41 | 77 |
|
| 78 | + if ( useShippingCountry ) { |
| 79 | + jQuery( 'form.checkout, form#order_review' ).on( |
| 80 | + 'change', |
| 81 | + '#shipping_country', |
| 82 | + changeCountry |
| 83 | + ); |
| 84 | + jQuery( '#shipping_country' ).trigger( 'change' ); |
| 85 | + } |
| 86 | + |
| 87 | + // Trigger country change on ship to different address checkbox change. |
| 88 | + jQuery( 'form.checkout, form#order_review' ).on( |
| 89 | + 'change', |
| 90 | + '#ship-to-different-address-checkbox', |
| 91 | + function () { |
| 92 | + if ( ! useShippingCountry ) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + const isChecked = jQuery( |
| 97 | + '#ship-to-different-address-checkbox' |
| 98 | + ).is( ':checked' ); |
| 99 | + |
| 100 | + if ( isChecked ) { |
| 101 | + jQuery( billingVatNumber ).fadeOut(); |
| 102 | + jQuery( '#shipping_country' ).trigger( 'change' ); |
| 103 | + } else { |
| 104 | + jQuery( shippingVatNumber ).fadeOut(); |
| 105 | + jQuery( '#billing_country' ).trigger( 'change' ); |
| 106 | + } |
| 107 | + } |
| 108 | + ); |
| 109 | + jQuery( '#ship-to-different-address-checkbox' ).trigger( 'change' ); |
| 110 | + |
42 | 111 | /* Validate EU VAT Number field only on change event */
|
43 |
| - jQuery( 'form.checkout, form#order_review' ).on( 'change', '#woocommerce_eu_vat_number', function() { |
44 |
| - jQuery( 'body' ).trigger( 'update_checkout' ); |
45 |
| - } ); |
| 112 | + jQuery( 'form.checkout, form#order_review' ).on( |
| 113 | + 'change', |
| 114 | + billingVatNumber, |
| 115 | + function () { |
| 116 | + jQuery( 'body' ).trigger( 'update_checkout' ); |
| 117 | + } |
| 118 | + ); |
| 119 | + |
| 120 | + if ( useShippingCountry ) { |
| 121 | + jQuery( 'form.checkout, form#order_review' ).on( |
| 122 | + 'change', |
| 123 | + shippingVatNumber, |
| 124 | + function () { |
| 125 | + jQuery( 'body' ).trigger( 'update_checkout' ); |
| 126 | + } |
| 127 | + ); |
| 128 | + } |
46 | 129 |
|
47 | 130 | /**
|
48 | 131 | * Handles checkout field UI when VAT field validation fails.
|
49 | 132 | */
|
50 | 133 | jQuery( document.body ).on( 'updated_checkout', function( e, data ) {
|
51 |
| - $vat_field = jQuery( '#woocommerce_eu_vat_number' ); |
| 134 | + const shippingCountry = jQuery( '#shipping_country' ).val(); |
| 135 | + const shipToDifferent = jQuery( |
| 136 | + '#ship-to-different-address-checkbox' |
| 137 | + ).is( ':checked' ); |
| 138 | + const $vat_field = |
| 139 | + useShippingCountry && shippingCountry && shipToDifferent |
| 140 | + ? jQuery( shippingVatNumber ) |
| 141 | + : jQuery( billingVatNumber ); |
52 | 142 |
|
53 | 143 | if ( ! $vat_field.is( ':visible' ) ) {
|
54 | 144 | return;
|
|
0 commit comments