Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some issues in add cart item API route #11

Open
zakiabdessem opened this issue Apr 6, 2023 · 0 comments
Open

some issues in add cart item API route #11

zakiabdessem opened this issue Apr 6, 2023 · 0 comments

Comments

@zakiabdessem
Copy link

zakiabdessem commented Apr 6, 2023

I would like to report some errors that I have found in your add_cart_item controller. These issues have gone unnoticed and I believe they need to be addressed. Here are the details:

When an item is added to the cart and there are existing items in the cart (if(itemIndex > -1){...}), the price of the new item(s) is not being updated. This could lead to incorrect billing and confusion for the user.

Similarly, when there are existing items in the cart (if(itemIndex > -1){...}), the old total price of the item is not being subtracted from the bill. This could cause issues if the admin changes the price of a product item and the user ends up buying it at the old price.

To resolve these issues, I suggest the following new code:

if (itemIndex > -1) {
let productItem = cart.items[itemIndex];
cart.bill -= productItem.quantity * productItem.price; // Subtract the old total price of the item from the bill
productItem.quantity += quantity;
productItem.price = price * productItem.quantity; // Update the price of the item in the cart
cart.items[itemIndex] = productItem;
} else {
cart.items.push({ productId, name, quantity, price: price * quantity });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant