cart context with variation
Unanswered
Moncef posted this in #help-forum
MoncefOP
hello guys i have a problem when i try to add and obj to existing array like when i choose varient color it should be like this [{...},{...},{...},{...}] but it go like this [Array(3), Array(3), {…}]
const AddToCart = (product, color) => {
const newCartItem = {
...product,
// selectedSize,
selectedColor: color,
quantity: 1,
};
product.selectedColor = color;
const checkproduct = cartItems.find(
(item) =>
item.id === product.id && item.selectedColor === product.selectedColor
);
if (checkproduct) {
console.log("worked");
setCartItems(
cartItems.map((cartItem) =>
cartItem.id === product.id &&
cartItem.selectedColor === product.selectedColor
? { ...cartItem, quantity: cartItem.quantity + 1 }
: cartItems
)
);
} else {
setCartItems([...cartItems, newCartItem]);
}
};
const AddToCart = (product, color) => {
const newCartItem = {
...product,
// selectedSize,
selectedColor: color,
quantity: 1,
};
product.selectedColor = color;
const checkproduct = cartItems.find(
(item) =>
item.id === product.id && item.selectedColor === product.selectedColor
);
if (checkproduct) {
console.log("worked");
setCartItems(
cartItems.map((cartItem) =>
cartItem.id === product.id &&
cartItem.selectedColor === product.selectedColor
? { ...cartItem, quantity: cartItem.quantity + 1 }
: cartItems
)
);
} else {
setCartItems([...cartItems, newCartItem]);
}
};