From 74afe046b805cfc5ef1955b3a99937c59f1c5cf4 Mon Sep 17 00:00:00 2001 From: ziad badry Date: Mon, 4 May 2026 00:24:24 +0300 Subject: [PATCH] fix: assign product quantities by id match instead of index in addNewCart --- src/controllers/cart.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/controllers/cart.js b/src/controllers/cart.js index a6acb95..951402b 100644 --- a/src/controllers/cart.js +++ b/src/controllers/cart.js @@ -79,9 +79,9 @@ export const addNewCart = ({ userId, products = [] }) => { let totalQuantity = 0; // get products in the relevant schema - const someProducts = productsByIds.map((p, idx) => { - // get quantity of the product - const quantity = productQty[idx]; + const someProducts = productsByIds.map((p) => { + const originalIndex = productIds.indexOf(p.id); // match by id + const quantity = productQty[originalIndex]; // total price (price * quantity) const priceWithQty = p.price * quantity; @@ -219,4 +219,4 @@ export const deleteCartById = ({ id }) => { cart.deletedOn = new Date().toISOString(); return cart; -}; +}; \ No newline at end of file