Commit dd0615bb413 for woocommerce
commit dd0615bb4131194ebe37762d79ff14542e24c287
Author: Diego Curbelo <diego@curbelo.com>
Date: Tue Oct 6 10:39:14 2026 -0300
Preselect the customer's default saved payment method in block checkout (#69266)
* Fix default saved payment method selection in block checkout
* Add changelog entry for saved payment method selection
diff --git a/plugins/woocommerce/changelog/68554-preselect-default-saved-payment-method b/plugins/woocommerce/changelog/68554-preselect-default-saved-payment-method
new file mode 100644
index 00000000000..69e8103f54b
--- /dev/null
+++ b/plugins/woocommerce/changelog/68554-preselect-default-saved-payment-method
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Preselect the customer's default saved payment method in block checkout.
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/block-data/payment/default-state.ts b/plugins/woocommerce/client/blocks/packages/public-api/block-data/payment/default-state.ts
index c6093e6ac28..fd238eadfe8 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/block-data/payment/default-state.ts
+++ b/plugins/woocommerce/client/blocks/packages/public-api/block-data/payment/default-state.ts
@@ -54,9 +54,12 @@ function getDefaultPaymentMethodData() {
const flatSavedPaymentMethods = Object.keys( savedPaymentMethods ).flatMap(
( type ) => savedPaymentMethods[ type ]
);
- const savedPaymentMethod = flatSavedPaymentMethods.find(
+ const gatewaySavedPaymentMethods = flatSavedPaymentMethods.filter(
( method ) => method.method.gateway === defaultPaymentMethod
);
+ const savedPaymentMethod =
+ gatewaySavedPaymentMethods.find( ( method ) => method.is_default ) ||
+ gatewaySavedPaymentMethods[ 0 ];
// If a saved payment method is found that matches the default payment method,
// use it.
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/block-data/payment/test/default-state.ts b/plugins/woocommerce/client/blocks/packages/public-api/block-data/payment/test/default-state.ts
index 109a4ac301a..e5d14cc6a27 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/block-data/payment/test/default-state.ts
+++ b/plugins/woocommerce/client/blocks/packages/public-api/block-data/payment/test/default-state.ts
@@ -199,6 +199,69 @@ describe( 'defaultPaymentState', () => {
'wc-stripe-payment-token': '123',
} );
} );
+
+ it( 'selects the default saved token for the active gateway, even when it is not first', () => {
+ const first = {
+ method: { gateway: 'stripe', brand: 'visa', last4: '1111' },
+ tokenId: 1,
+ is_default: false,
+ expires: '10/99',
+ actions: {},
+ };
+ const defaultToken = {
+ ...first,
+ tokenId: 2,
+ is_default: true,
+ };
+ const state = getDefaultPaymentStateWithMocks( {
+ checkoutData: { payment_method: 'stripe' },
+ customerPaymentMethods: {
+ cc: [ first, defaultToken ],
+ },
+ } );
+
+ expect( state.paymentMethodData ).toEqual( {
+ token: '2',
+ payment_method: 'stripe',
+ 'wc-stripe-payment-token': '2',
+ } );
+ } );
+
+ it( 'keeps the first token for the active gateway when its tokens have no default', () => {
+ const first = {
+ method: { gateway: 'stripe', brand: 'visa', last4: '1111' },
+ tokenId: 1,
+ is_default: false,
+ expires: '10/99',
+ actions: {},
+ };
+ const state = getDefaultPaymentStateWithMocks( {
+ checkoutData: { payment_method: 'stripe' },
+ customerPaymentMethods: {
+ cc: [
+ first,
+ { ...first, tokenId: 3 },
+ {
+ method: {
+ gateway: 'other',
+ brand: 'visa',
+ last4: '2222',
+ },
+ tokenId: 2,
+ is_default: true,
+ expires: '10/99',
+ actions: {},
+ },
+ ],
+ },
+ } );
+
+ expect( state.paymentMethodData ).toEqual( {
+ token: '1',
+ payment_method: 'stripe',
+ 'wc-stripe-payment-token': '1',
+ } );
+ } );
} );
describe( 'Saved payment methods', () => {