From 526b7125baf44e3202d7660f28d1e2043f694c17 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Fri, 18 Oct 2024 09:59:34 -0500 Subject: [PATCH 1/7] Expose allowed shipping countries to the frontend --- ...class-wc-payments-express-checkout-button-handler.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php index 682b4eac903..d024c3ba611 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php @@ -240,11 +240,12 @@ public function scripts() { 'pay_for_order' => wp_create_nonce( 'pay_for_order' ), ], 'checkout' => [ - 'currency_code' => strtolower( get_woocommerce_currency() ), - 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), - 'needs_shipping' => WC()->cart->needs_shipping(), + 'currency_code' => strtolower( get_woocommerce_currency() ), + 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), + 'needs_shipping' => WC()->cart->needs_shipping(), // Defaults to 'required' to match how core initializes this option. - 'needs_payer_phone' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ), + 'needs_payer_phone' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ), + 'allowed_shipping_countries' => array_keys( WC()->countries->get_shipping_countries() ?? [] ), ], 'button' => $this->get_button_settings(), 'login_confirmation' => $this->get_login_confirmation_settings(), From c9feecf50411e48bb1e4bec2503ddf3fa3d5121d Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Fri, 18 Oct 2024 10:00:03 -0500 Subject: [PATCH 2/7] Define allowedShippingCountries on Stripe for Express Checkouts --- client/express-checkout/blocks/hooks/use-express-checkout.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client/express-checkout/blocks/hooks/use-express-checkout.js b/client/express-checkout/blocks/hooks/use-express-checkout.js index 090a16354a7..88f72e76829 100644 --- a/client/express-checkout/blocks/hooks/use-express-checkout.js +++ b/client/express-checkout/blocks/hooks/use-express-checkout.js @@ -68,6 +68,8 @@ export const useExpressCheckout = ( { }; } ), + allowedShippingCountries: getExpressCheckoutData( 'checkout' ) + .allowed_shipping_countries, }; // Click event from WC Blocks. From 022a7ba2f58fb84a79e6679764dc223a46a96ab6 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Fri, 18 Oct 2024 11:51:48 -0500 Subject: [PATCH 3/7] Add changelog entry --- ...t-for-allowedshippingcountries-in-express-checkout-element | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/9104-add-support-for-allowedshippingcountries-in-express-checkout-element diff --git a/changelog/9104-add-support-for-allowedshippingcountries-in-express-checkout-element b/changelog/9104-add-support-for-allowedshippingcountries-in-express-checkout-element new file mode 100644 index 00000000000..5eae7cb8972 --- /dev/null +++ b/changelog/9104-add-support-for-allowedshippingcountries-in-express-checkout-element @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add support for allowedShippingCountries in Express Checkout Element. From d77f1741db912343aed3ea5ac1657d80f8329b66 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Fri, 18 Oct 2024 15:26:29 -0500 Subject: [PATCH 4/7] Add allowedShippingCountries para to ECE in product and shortcode pages --- client/express-checkout/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/client/express-checkout/index.js b/client/express-checkout/index.js index f4efa4aa10f..535431b7fd1 100644 --- a/client/express-checkout/index.js +++ b/client/express-checkout/index.js @@ -304,6 +304,9 @@ jQuery( ( $ ) => { shippingAddressRequired: options.requestShipping, phoneNumberRequired: options.requestPhone, shippingRates, + allowedShippingCountries: getExpressCheckoutData( + 'checkout' + ).allowed_shipping_countries, }; onClickHandler( event ); From 6b82bd5f67fa79b6442485da51d623ef6b339822 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Fri, 18 Oct 2024 18:51:40 -0500 Subject: [PATCH 5/7] Do not send list of countries if store ships to all countries --- ...wc-payments-express-checkout-button-handler.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php index d024c3ba611..33a4a068336 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php @@ -240,12 +240,11 @@ public function scripts() { 'pay_for_order' => wp_create_nonce( 'pay_for_order' ), ], 'checkout' => [ - 'currency_code' => strtolower( get_woocommerce_currency() ), - 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), - 'needs_shipping' => WC()->cart->needs_shipping(), + 'currency_code' => strtolower( get_woocommerce_currency() ), + 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), + 'needs_shipping' => WC()->cart->needs_shipping(), // Defaults to 'required' to match how core initializes this option. - 'needs_payer_phone' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ), - 'allowed_shipping_countries' => array_keys( WC()->countries->get_shipping_countries() ?? [] ), + 'needs_payer_phone' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ), ], 'button' => $this->get_button_settings(), 'login_confirmation' => $this->get_login_confirmation_settings(), @@ -258,6 +257,11 @@ public function scripts() { 'is_checkout_page' => $this->express_checkout_helper->is_checkout(), ]; + // No need to send a list of countries if the option is set to ship to all countries. + if ( get_option( 'woocommerce_ship_to_countries' ) !== 'all' ) { + $payment_request_params['checkout']['allowed_shipping_countries'] = array_keys( WC()->countries->get_shipping_countries() ?? [] ); + } + WC_Payments::register_script_with_dependencies( 'WCPAY_EXPRESS_CHECKOUT_ECE', 'dist/express-checkout', [ 'jquery', 'stripe' ] ); WC_Payments_Utils::enqueue_style( From 606ec49423a6fe45b4a57213671b77afe2502803 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Sat, 19 Oct 2024 23:42:02 -0500 Subject: [PATCH 6/7] Skip sending full country list when unnecessary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don’t send the full country list in the two cases where all countries would be sent --- .../class-wc-payments-express-checkout-button-handler.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php index 33a4a068336..f43a0b131f6 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php @@ -257,8 +257,12 @@ public function scripts() { 'is_checkout_page' => $this->express_checkout_helper->is_checkout(), ]; - // No need to send a list of countries if the option is set to ship to all countries. - if ( get_option( 'woocommerce_ship_to_countries' ) !== 'all' ) { + // No need to send a list of countries if the store: + // 1. ships to all countries. Regardless of whom it sells to. + $ship_to_all = get_option( 'woocommerce_ship_to_countries' ) === 'all'; + // 2. ships to all countries it sells to and store sells to all countries. + $ship_to_all_and_selling_to_all = get_option( 'woocommerce_allowed_countries' ) === 'all' && empty( get_option( 'woocommerce_ship_to_countries' ) ); + if ( ! $ship_to_all && ! $ship_to_all_and_selling_to_all ) { $payment_request_params['checkout']['allowed_shipping_countries'] = array_keys( WC()->countries->get_shipping_countries() ?? [] ); } From 62c6c56dd703b66c6ae36720465fee9b22276b5c Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Mon, 28 Oct 2024 17:00:08 -0500 Subject: [PATCH 7/7] Remove the condition that enables optimization when sending all country codes --- ...ayments-express-checkout-button-handler.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php index f43a0b131f6..d024c3ba611 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php @@ -240,11 +240,12 @@ public function scripts() { 'pay_for_order' => wp_create_nonce( 'pay_for_order' ), ], 'checkout' => [ - 'currency_code' => strtolower( get_woocommerce_currency() ), - 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), - 'needs_shipping' => WC()->cart->needs_shipping(), + 'currency_code' => strtolower( get_woocommerce_currency() ), + 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), + 'needs_shipping' => WC()->cart->needs_shipping(), // Defaults to 'required' to match how core initializes this option. - 'needs_payer_phone' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ), + 'needs_payer_phone' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ), + 'allowed_shipping_countries' => array_keys( WC()->countries->get_shipping_countries() ?? [] ), ], 'button' => $this->get_button_settings(), 'login_confirmation' => $this->get_login_confirmation_settings(), @@ -257,15 +258,6 @@ public function scripts() { 'is_checkout_page' => $this->express_checkout_helper->is_checkout(), ]; - // No need to send a list of countries if the store: - // 1. ships to all countries. Regardless of whom it sells to. - $ship_to_all = get_option( 'woocommerce_ship_to_countries' ) === 'all'; - // 2. ships to all countries it sells to and store sells to all countries. - $ship_to_all_and_selling_to_all = get_option( 'woocommerce_allowed_countries' ) === 'all' && empty( get_option( 'woocommerce_ship_to_countries' ) ); - if ( ! $ship_to_all && ! $ship_to_all_and_selling_to_all ) { - $payment_request_params['checkout']['allowed_shipping_countries'] = array_keys( WC()->countries->get_shipping_countries() ?? [] ); - } - WC_Payments::register_script_with_dependencies( 'WCPAY_EXPRESS_CHECKOUT_ECE', 'dist/express-checkout', [ 'jquery', 'stripe' ] ); WC_Payments_Utils::enqueue_style(