-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat(plugin-ecommerce): expose method refreshCart in useCart (#14765) #14767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat(plugin-ecommerce): expose method refreshCart in useCart (#14765) #14767
Conversation
| ) => Promise<unknown> | ||
| paymentMethods: PaymentAdapterClient[] | ||
| /** | ||
| * Refresh the cart. |
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.
A little light on detail for what I would want to see from this jsdoc
|
While I'm not opposed to adding a refreshCart method, I want to understand more about the reasoning and the exact issue with the existing reducer's logic. It's true that we merge the same product ID/variant ID together and I would be open to explore either making that part customisable or providing a config option to not merge the items but I need some more information around this.
|
|
I added refreshCart() because when adding items to the cart directly through a PATCH request (instead of using the built-in addItem helper), the local cart state was not automatically updated. The addItem() method internally triggers Payload’s cart refresh logic, so the frontend always gets the newest cart.
Payload doesn’t automatically re-fetch the updated cart data. By explicitly calling:
I ensure that after the custom PATCH request succeeds, the cart in the frontend is immediately synchronized with the server. my use case (not finished yet):: I expect each added product to be displayed as a separate item in the cart, because it includes specific settings defined by the user at the moment it is added to the cart. |
What?
This PR adds a new method refreshCart to the E-commerce plugin.
Why?
Currently, the standard addItem method enforces uniqueness validation for products in the cart. This conflicts with certain use cases where the same product should be added as a new line item.
By exposing refreshCart, developers can manually refresh the cart state after directly modifying cart items, ensuring the UI stays in sync without being blocked by addItem’s uniqueness validation.
This addresses the feature request described in #14765
and the related discussion #14764
.
How?
Added refreshCart method to the E-commerce context.
This allows programmatic updates to the cart state without triggering the uniqueness validation of addItem.
The method can be called after direct modifications to the cart (e.g., via fetch requests or custom logic).
Fixes
Fixes #14765