If my shopping cart does not exist, I first initialize the cart via a POST endpoint. This requires creating a cart cookie which has to be returned in the response for the cookie to be set in the browser.
In the case of creating a new cart cookie, I never reach the code to update the cart due to the return. Is there a way to create the cart cookie without the return statement in the action?
export async function action({ params ,request }: ActionArgs) {
let cartId = await getCartCookie(request)
// if no cart cookie, create the cookie
if(!cartId) {
const cartCookie = await createCartCookie(request)
// to create the cookie here, we need to return it. Can this be avoided?
return cartCookie
}
// else, cart cookie exists. Update cart with cartId
const { product } = await client.products.retrieve(params.productId);
const variant_id = product.variants[0].id
const { cart }= await client.carts.lineItems.create(cartId, {
variant_id,
quantity: 1,
})
return cart.items
};