This commit is contained in:
insvrgent
2025-02-14 17:43:22 +07:00
parent 1b8eb2856d
commit 725bb867bf
9 changed files with 136 additions and 82 deletions

View File

@@ -18,7 +18,7 @@ export const getItemsByCafeId = (cafeId) => {
};
// Update quantity in localStorage for a specific cafeId and itemId
export const updateItemQtyInCart = (cafeId, itemId, qty) => {
export const updateItemQtyInCart = (cafeId, itemId, qty, price) => {
let cart = JSON.parse(getLocalStorage('cart')) || [];
const cafeIndex = cart.findIndex(cafeItem => cafeItem.cafeId === cafeId);
@@ -27,14 +27,15 @@ export const updateItemQtyInCart = (cafeId, itemId, qty) => {
if (itemIndex > -1) {
if (qty > 0) {
cart[cafeIndex].items[itemIndex].qty = qty; // Update qty if item exists
cart[cafeIndex].items[itemIndex].price = price; // Update qty if item exists
} else {
cart[cafeIndex].items.splice(itemIndex, 1); // Remove item if qty is 0
}
} else if (qty > 0) {
cart[cafeIndex].items.push({ itemId, qty }); // Add new item
cart[cafeIndex].items.push({ itemId, qty, price }); // Add new item
}
} else if (qty > 0) {
cart.push({ cafeId, items: [{ itemId, qty }] }); // Add new cafeId and item
cart.push({ cafeId, items: [{ itemId, qty, price }] }); // Add new cafeId and item
}
updateLocalStorage('cart', JSON.stringify(cart));