This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 220
Ensure shopper saved card is used as default payment method (default was being overwritten in some circumstances) #3131
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -341,6 +341,11 @@ export const PaymentMethodDataProvider = ( { children } ) => { | |
return; | ||
} | ||
|
||
// Customer has saved card/payment methods - no need to set a default. | ||
if ( customerPaymentMethods ) { | ||
return; | ||
} | ||
|
||
// If there's no active payment method, or the active payment method has | ||
// been removed (e.g. COD vs shipping methods), set one as active. | ||
if ( | ||
|
@@ -356,6 +361,7 @@ export const PaymentMethodDataProvider = ( { children } ) => { | |
paymentMethodsInitialized, | ||
paymentData.paymentMethods, | ||
setActivePaymentMethod, | ||
customerPaymentMethods, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this dependency needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, and it looks like it hasn't been needed for a while! Removed it and retested (just in case) 👍 |
||
] ); | ||
|
||
// emit events. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an alternative approach that doesn't require the
customerPaymentMethods
dependency. It would involve usinguseState
functional updates. Something like:This would ensure
currentActivePaymentMethod
is evaluated right at the moment when the state is updated, so the payment method activated inSavedPaymentMethodOptions
would already be active.I didn't do a lot of testing of this approach, so feel free to ignore, but wanted to propose it in case you think it might be a good alternative.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Smart! I like this, and of course since it's more general it avoids the issue with my truthy test below. Thanks for picking up on this, much better fix 🚀
The crux of the issue is that we were previously using a stale copy of
currentActivePaymentMethod
; so it makes sense that we'd fix this by using the functional form ofsetState
. The principle to keep in mind here (just reminding myself!) - if a state update depends on the previous value, use functional form.I've made the change and I'll do some more testing of a few scenarios. Note the other change from the sketch above is the reset of checkout status to
PRISTINE
when applying the default.