diff --git a/components/cart/actions.ts b/components/cart/actions.ts index 21d4e9ddf..ea0e9969a 100644 --- a/components/cart/actions.ts +++ b/components/cart/actions.ts @@ -84,17 +84,17 @@ export async function getCart() { function updateCartCookie(cart: ExtendedCart): string | undefined { const cartId = cookies().get('sw-context-token')?.value; - - if (!cartId && cart && cart.token) { - cookies().set('sw-context-token', cart.token); - return cart.token; - } - + // cartId is set, but not valid anymore, update the cookie if (cartId && cart && cart.token && cart.token !== cartId) { cookies().set('sw-context-token', cart.token); return cart.token; } - + // cartId is not set (undefined), case for new cart, set the cookie + if (!cartId && cart && cart.token) { + cookies().set('sw-context-token', cart.token); + return cart.token; + } + // cartId is set and the same like cart.token, return it return cartId; }