From db01f502bfddf9eb75f5eed90620f8b7227d31f2 Mon Sep 17 00:00:00 2001 From: Guilherme Pressutto Date: Thu, 22 Feb 2024 11:51:15 -0300 Subject: [PATCH] Checking if wc.wcSettings is correctly set in checkout.js (#8245) Co-authored-by: Francesco --- changelog/fix-check-wc | 5 +++++ client/utils/checkout.js | 28 +++++++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 changelog/fix-check-wc diff --git a/changelog/fix-check-wc b/changelog/fix-check-wc new file mode 100644 index 00000000000..0bed41891f4 --- /dev/null +++ b/changelog/fix-check-wc @@ -0,0 +1,5 @@ +Significance: patch +Type: fix +Comment: Checking if wc.wcSettings is correctly set in checkou.js. This will not change anything as other extensions and even WC itself depends on it being correctly set, so it's just a minor enhancement. + + diff --git a/client/utils/checkout.js b/client/utils/checkout.js index 4a67666e827..42c9fcfc589 100644 --- a/client/utils/checkout.js +++ b/client/utils/checkout.js @@ -8,18 +8,12 @@ */ export const getConfig = ( name ) => { // Classic checkout or blocks-based one. - let config = null; if ( typeof wcpayConfig !== 'undefined' ) { - config = wcpayConfig; - } else if ( typeof wcpay_upe_config !== 'undefined' ) { - config = wcpay_upe_config; - } else if ( typeof wc !== 'undefined' ) { - config = wc.wcSettings.getSetting( 'woocommerce_payments_data' ); - } else { - return null; + return wcpayConfig[ name ]; } - return config[ name ] || null; + // eslint-disable-next-line no-use-before-define + return getUPEConfig( name ); }; /** @@ -30,10 +24,18 @@ export const getConfig = ( name ) => { */ export const getUPEConfig = ( name ) => { // Classic checkout or blocks-based one. - const config = - typeof wcpay_upe_config !== 'undefined' - ? wcpay_upe_config - : wc.wcSettings.getSetting( 'woocommerce_payments_data' ); + let config = null; + if ( typeof wcpay_upe_config !== 'undefined' ) { + config = wcpay_upe_config; + } else if ( + typeof wc === 'object' && + typeof wc.wcSettings !== 'undefined' + ) { + // If getSettings or woocommerce_payments_data is not available, default to an empty object so we return null bellow. + config = wc.wcSettings.getSetting( 'woocommerce_payments_data' ) || {}; + } else { + return null; + } return config[ name ] || null; };