Skip to content

Commit 529726e

Browse files
authored
feat(plugin-ecommerce): add new method refreshCart in useCart (#14765) (#14767)
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](#14765) and the related discussion [#14764](#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
1 parent 67cc734 commit 529726e

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

packages/plugin-ecommerce/src/react/provider/index.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const defaultContext: EcommerceContextType = {
5050
initiatePayment: async () => {},
5151
isLoading: false,
5252
paymentMethods: [],
53+
refreshCart: async () => {},
5354
removeItem: async () => {},
5455
setCurrency: () => {},
5556
updateAddress: async () => {},
@@ -252,6 +253,14 @@ export const EcommerceProvider: React.FC<ContextProps> = ({
252253
[baseAPIURL, cartQuery, cartsSlug, cartSecret],
253254
)
254255

256+
const refreshCart = useCallback<EcommerceContextType['refreshCart']>(async () => {
257+
if (!cartID) {
258+
return
259+
}
260+
const updatedCart = await getCart(cartID)
261+
setCart(updatedCart)
262+
}, [cartID, getCart])
263+
255264
const deleteCart = useCallback(
256265
async (cartID: DefaultDocumentIDType) => {
257266
// Build query params with secret if provided
@@ -898,6 +907,7 @@ export const EcommerceProvider: React.FC<ContextProps> = ({
898907
initiatePayment,
899908
isLoading,
900909
paymentMethods,
910+
refreshCart,
901911
removeItem,
902912
selectedPaymentMethod,
903913
setCurrency,
@@ -960,8 +970,16 @@ export const useCurrency = () => {
960970
}
961971

962972
export function useCart<T extends CartsCollection>() {
963-
const { addItem, cart, clearCart, decrementItem, incrementItem, isLoading, removeItem } =
964-
useEcommerce()
973+
const {
974+
addItem,
975+
cart,
976+
clearCart,
977+
decrementItem,
978+
incrementItem,
979+
isLoading,
980+
refreshCart,
981+
removeItem,
982+
} = useEcommerce()
965983

966984
if (!addItem) {
967985
throw new Error('useCart must be used within an EcommerceProvider')
@@ -974,6 +992,7 @@ export function useCart<T extends CartsCollection>() {
974992
decrementItem,
975993
incrementItem,
976994
isLoading,
995+
refreshCart,
977996
removeItem,
978997
}
979998
}

packages/plugin-ecommerce/src/types/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,10 @@ export type EcommerceContextType<T extends EcommerceCollections = EcommerceColle
882882
*/
883883
isLoading: boolean
884884
paymentMethods: PaymentAdapterClient[]
885+
/**
886+
* Refresh the cart.
887+
*/
888+
refreshCart: () => Promise<void>
885889
/**
886890
* Remove an item from the cart by its index ID.
887891
*/

0 commit comments

Comments
 (0)