From 08d18fcbe1a5d0c09749708d0bea8c5c1c834ba4 Mon Sep 17 00:00:00 2001 From: Rafael Zaleski Date: Tue, 17 Dec 2024 11:05:24 -0300 Subject: [PATCH] Add logic to fallback to option text when value is not found --- .../express-checkout/utils/shipping-fields.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/client/express-checkout/utils/shipping-fields.js b/client/express-checkout/utils/shipping-fields.js index b7c8301ef1a..a9e7deb54f3 100644 --- a/client/express-checkout/utils/shipping-fields.js +++ b/client/express-checkout/utils/shipping-fields.js @@ -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' ); } };