Skip to content

Commit

Permalink
Add logic to fallback to option text when value is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelzaleski committed Dec 17, 2024
1 parent b47ab71 commit 08d18fc
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions client/express-checkout/utils/shipping-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,24 @@ const updateShortcodeField = ( formSelector, fieldName, value ) => {
const field = document.querySelector(
`${ formSelector } [name="${ fieldName }"]`
);
if ( field ) {

if ( ! field ) return;

// Check if the field is a dropdown (country/state).
if ( field.tagName === 'SELECT' && /country|state/.test( fieldName ) ) {
const options = Array.from( field.options );
const match = options.find(
( opt ) => opt.value === value || opt.textContent.trim() === value
);

if ( match ) {
field.value = match.value;
jQuery( field ).trigger( 'change' ).trigger( 'close' );
}
} else {
// Default behavior for text inputs.
field.value = value;
jQuery( field ).trigger( 'change' ).trigger( 'close' );
jQuery( field ).trigger( 'change' );
}
};

Expand Down

0 comments on commit 08d18fc

Please sign in to comment.