Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 31f8bea

Browse files
author
Steph Dietz
committed
update empty cart
1 parent 2a13fbf commit 31f8bea

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

backend/functions/assembly/crud.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ export function addToCart(cartId: string, productId: string): string {
261261
const cartItemId = cartId + "_" + productId;
262262

263263
if (cart === null || cart === "") {
264+
// If cart does not exist or is empty, create a new cart with the product
264265
const upsertResult = upsertCart(cartId, productId);
265266
if (upsertResult !== cartId) {
266267
console.log("Failed to create new cart:");
@@ -273,8 +274,10 @@ export function addToCart(cartId: string, productId: string): string {
273274
return quantityResult;
274275
}
275276
} else {
276-
const cartItems = cart.split(",");
277+
const cartItems = cart.split(",").filter((item) => item.trim() !== "");
278+
277279
if (cartItems.includes(productId)) {
280+
// If product is already in the cart, increase the quantity
278281
const cartItemQuantity = collections.getText(
279282
consts.cartItemsCollection,
280283
cartItemId,
@@ -297,6 +300,7 @@ export function addToCart(cartId: string, productId: string): string {
297300
return upsertQuantityResult.error;
298301
}
299302
} else {
303+
// Add the new product to the existing cart
300304
const updatedCart = cart.length > 0 ? cart + "," + productId : productId;
301305
const upsertResult = upsertCart(cartId, updatedCart);
302306
if (upsertResult !== cartId) {

0 commit comments

Comments
 (0)