@@ -22,10 +22,19 @@ interface Context {
2222 addingToCart : Set < string > ;
2323 clearCart : ( ) => void ;
2424 addToCart : ( variantId : string , quantity ?: number ) => Promise < void > ;
25- seedCart : ( variantId : string , quantity ?: number ) => Promise < void > ;
25+ seedCart : (
26+ variantId : string ,
27+ quantity ?: number ,
28+ buyerIdentityMode ?: BuyerIdentityMode ,
29+ ) => Promise < void > ;
2630 removeFromCart : ( variantId : string ) => Promise < void > ;
2731}
2832
33+ type AddToCartOptions = {
34+ forceNewCart ?: boolean ;
35+ buyerIdentityMode ?: BuyerIdentityMode ;
36+ } ;
37+
2938const defaultCartId = undefined ;
3039const defaultCheckoutURL = undefined ;
3140const defaultTotalQuantity = 0 ;
@@ -114,19 +123,30 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
114123 } , [ cartId , fetchCart , setTotalQuantity ] ) ;
115124
116125 const addToCart = useCallback (
117- async ( variantId : string , quantity = 1 , forceNewCart = false ) => {
126+ async (
127+ variantId : string ,
128+ quantity = 1 ,
129+ {
130+ forceNewCart = false ,
131+ buyerIdentityMode = appConfig . buyerIdentityMode ,
132+ } : AddToCartOptions = { } ,
133+ ) => {
118134 if ( ! Number . isInteger ( quantity ) || quantity < 1 ) {
119135 throw new Error ( 'Cart quantity must be a positive integer' ) ;
120136 }
121137
122138 let id = forceNewCart ? undefined : cartId ;
139+ const cartAppConfig = {
140+ ...appConfig ,
141+ buyerIdentityMode,
142+ } ;
123143
124144 dispatch ( { type : 'add' , variantId} ) ;
125145
126146 try {
127147 if (
128148 ! id &&
129- appConfig . buyerIdentityMode === BuyerIdentityMode . CustomerAccount &&
149+ buyerIdentityMode === BuyerIdentityMode . CustomerAccount &&
130150 ! isAuthenticated
131151 ) {
132152 const signInRequiredMessage =
@@ -144,14 +164,11 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
144164
145165 if ( ! id ) {
146166 let customerAccessToken : string | undefined ;
147- if (
148- appConfig . buyerIdentityMode === BuyerIdentityMode . CustomerAccount
149- ) {
150- customerAccessToken =
151- ( await getValidAccessToken ( ) ) ?? undefined ;
167+ if ( buyerIdentityMode === BuyerIdentityMode . CustomerAccount ) {
168+ customerAccessToken = ( await getValidAccessToken ( ) ) ?? undefined ;
152169 }
153170 const cartInput = createBuyerIdentityCartInput (
154- appConfig ,
171+ cartAppConfig ,
155172 customerAccessToken ,
156173 ) ;
157174 const cart = await createCart ( { variables : { input : cartInput } } ) ;
@@ -198,8 +215,15 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
198215 ) ;
199216
200217 const seedCart = useCallback (
201- async ( variantId : string , quantity = 1 ) => {
202- await addToCart ( variantId , quantity , true ) ;
218+ async (
219+ variantId : string ,
220+ quantity = 1 ,
221+ buyerIdentityMode ?: BuyerIdentityMode ,
222+ ) => {
223+ await addToCart ( variantId , quantity , {
224+ forceNewCart : true ,
225+ buyerIdentityMode,
226+ } ) ;
203227 } ,
204228 [ addToCart ] ,
205229 ) ;
0 commit comments