Skip to content

Commit

Permalink
Updates to 2.9.4
Browse files Browse the repository at this point in the history
  • Loading branch information
WooCommerce committed May 22, 2024
1 parent 7a3b26d commit 0f1e51f
Show file tree
Hide file tree
Showing 19 changed files with 637 additions and 237 deletions.
1 change: 1 addition & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
data._billing_country = $( '#_billing_country' ).val();
data._shipping_country = $( '#_shipping_country' ).val();
data._billing_postcode = $( '#_billing_postcode' ).val();
data._shipping_postcode = $( '#_shipping_postcode' ).val();
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion assets/js/admin.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 112 additions & 22 deletions assets/js/eu-vat.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,144 @@
jQuery(function(){
/* global wc_eu_vat_params, wc_address_i18n_params */
/* eslint-disable camelcase */
jQuery( function () {
const useShippingCountry = wc_eu_vat_params.use_shipping_country;
const billingVatNumber = '#woocommerce_eu_vat_number_field';
const shippingVatNumber = '#woocommerce_eu_vat_number_shipping_field';

function field_is_required( field, is_required ) {
if ( is_required ) {
field.find( 'label .optional' ).remove();
field.addClass( 'validate-required' );

if ( field.find( 'label .required' ).length === 0 ) {
field.find( 'label' ).append(
'&nbsp;<abbr class="required" title="' +
wc_address_i18n_params.i18n_required_text +
'">*</abbr>'
);
field
.find( 'label' )
.append(
'<abbr class="required" title="' +
wc_address_i18n_params.i18n_required_text +
'">*</abbr>'
);
}
} else {
field.find( 'label .required' ).remove();
field.removeClass( 'validate-required woocommerce-invalid woocommerce-invalid-required-field' );
field.removeClass(
'validate-required woocommerce-invalid woocommerce-invalid-required-field'
);

if ( field.find( 'label .optional' ).length === 0 ) {
field.find( 'label' ).append( '&nbsp;<span class="optional">(' + wc_address_i18n_params.i18n_optional_text + ')</span>' );
field
.find( 'label' )
.append(
'<span class="optional">(' +
wc_address_i18n_params.i18n_optional_text +
')</span>'
);
}
}
}

jQuery( 'form.checkout, form#order_review').on( 'change', '#billing_country', function() {
var country = jQuery( '#billing_country' ).val();
var check_countries = wc_eu_vat_params.eu_countries;
var b2b_enabled = wc_eu_vat_params.b2b_required;
// Handle change billing/shipping country.
function changeCountry() {
const billingCountry = jQuery( '#billing_country' ).val();
const shippingCountry = jQuery( '#shipping_country' ).val();
const shipToDifferent = jQuery(
'#ship-to-different-address-checkbox'
).is( ':checked' );
const validateShippingCountry =
useShippingCountry && shippingCountry && shipToDifferent;
const country = validateShippingCountry
? shippingCountry
: billingCountry;
const check_countries = wc_eu_vat_params.eu_countries;
const b2b_enabled = wc_eu_vat_params.b2b_required;

const vat_number_field = validateShippingCountry
? jQuery( shippingVatNumber )
: jQuery( billingVatNumber );

field_is_required( jQuery( '#woocommerce_eu_vat_number_field' ), false );
field_is_required( vat_number_field, false );

if ( country && jQuery.inArray( country, check_countries ) >= 0 ) {
jQuery( '#woocommerce_eu_vat_number_field' ).fadeIn();
if ( 'yes' === b2b_enabled ) {
field_is_required( jQuery( '#woocommerce_eu_vat_number_field' ), true );
vat_number_field.fadeIn();
if ( b2b_enabled === 'yes' ) {
field_is_required( vat_number_field, true );
}
} else {
jQuery( '#woocommerce_eu_vat_number_field' ).fadeOut();
vat_number_field.fadeOut();
}
});
}

jQuery( 'form.checkout, form#order_review' ).on(
'change',
'#billing_country',
changeCountry
);
jQuery( '#billing_country' ).trigger( 'change' );

if ( useShippingCountry ) {
jQuery( 'form.checkout, form#order_review' ).on(
'change',
'#shipping_country',
changeCountry
);
jQuery( '#shipping_country' ).trigger( 'change' );
}

// Trigger country change on ship to different address checkbox change.
jQuery( 'form.checkout, form#order_review' ).on(
'change',
'#ship-to-different-address-checkbox',
function () {
if ( ! useShippingCountry ) {
return;
}

const isChecked = jQuery(
'#ship-to-different-address-checkbox'
).is( ':checked' );

if ( isChecked ) {
jQuery( billingVatNumber ).fadeOut();
jQuery( '#shipping_country' ).trigger( 'change' );
} else {
jQuery( shippingVatNumber ).fadeOut();
jQuery( '#billing_country' ).trigger( 'change' );
}
}
);
jQuery( '#ship-to-different-address-checkbox' ).trigger( 'change' );

/* Validate EU VAT Number field only on change event */
jQuery( 'form.checkout, form#order_review' ).on( 'change', '#woocommerce_eu_vat_number', function() {
jQuery( 'body' ).trigger( 'update_checkout' );
} );
jQuery( 'form.checkout, form#order_review' ).on(
'change',
billingVatNumber,
function () {
jQuery( 'body' ).trigger( 'update_checkout' );
}
);

if ( useShippingCountry ) {
jQuery( 'form.checkout, form#order_review' ).on(
'change',
shippingVatNumber,
function () {
jQuery( 'body' ).trigger( 'update_checkout' );
}
);
}

/**
* Handles checkout field UI when VAT field validation fails.
*/
jQuery( document.body ).on( 'updated_checkout', function( e, data ) {
$vat_field = jQuery( '#woocommerce_eu_vat_number' );
const shippingCountry = jQuery( '#shipping_country' ).val();
const shipToDifferent = jQuery(
'#ship-to-different-address-checkbox'
).is( ':checked' );
const $vat_field =
useShippingCountry && shippingCountry && shipToDifferent
? jQuery( shippingVatNumber )
: jQuery( billingVatNumber );

if ( ! $vat_field.is( ':visible' ) ) {
return;
Expand Down
2 changes: 1 addition & 1 deletion assets/js/eu-vat.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/frontend.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wc-blocks-checkout', 'wc-settings', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => 'e3d7d07c67aecd20c69de0d94c963346');
<?php return array('dependencies' => array('wc-blocks-checkout', 'wc-settings', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '16638e3a3498458e80c2f3e08daa4d3e');
Loading

0 comments on commit 0f1e51f

Please sign in to comment.