Skip to content

if i try to add same item after adding another item it says undefiend to all the other items beside newly added one #109

@IsuruWickramasinghe

Description

@IsuruWickramasinghe

const onAdd = (product, quantity) => {
const checkProductInCart = cartItems.find((item) => item._id === product._id);

settotalPrice((prevTotalPrice) => prevTotalPrice + product.price * quantity);
settotalQuantities((prevTotalQuantities) => prevTotalQuantities + quantity);

if (checkProductInCart) {
const updatedCartItems = cartItems.map((cartProduct) => {
if (cartProduct._id === product._id) {
return {
...cartProduct,
quantity: cartProduct.quantity + quantity
};
}
return cartProduct; // Add this return statement
});
setcartItems(updatedCartItems);
} else {
product.quantity = quantity;
setcartItems([...cartItems, { ...product }]);
}

toast.success(${selectedQuantities} ${product.name} added to the cart.);
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions