From 6c6c9b6a5e2d29dc9ee3c5897578b1968863e707 Mon Sep 17 00:00:00 2001 From: ramith-kulal Date: Wed, 21 Feb 2024 12:09:15 +0530 Subject: [PATCH] Implement dynamic price calculation based on quantity --- src/Components/CartItems/CartItems.css | 1 + src/Components/CartItems/CartItems.jsx | 27 +++++++++++++---------- src/Context/ShopContext.jsx | 30 ++++++++++++++++++++------ 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/Components/CartItems/CartItems.css b/src/Components/CartItems/CartItems.css index 17690a6..535620e 100644 --- a/src/Components/CartItems/CartItems.css +++ b/src/Components/CartItems/CartItems.css @@ -49,6 +49,7 @@ border: 2px solid #ebebeb; background: #fff; text-align: center; + color: rgb(5, 5, 5); } .cartitems-down{ diff --git a/src/Components/CartItems/CartItems.jsx b/src/Components/CartItems/CartItems.jsx index 451fa74..a6fa5bf 100644 --- a/src/Components/CartItems/CartItems.jsx +++ b/src/Components/CartItems/CartItems.jsx @@ -2,10 +2,15 @@ import React, { useContext } from "react"; import "./CartItems.css"; import { ShopContext } from "../../Context/ShopContext"; import remove_icon from "../Assets/cart_cross_icon.png"; - const CartItems = () => { - const { all_product, cartItems, removeFromCart, getTotalCartAmount,theme } = + const { cartItems, removeFromCart, getTotalCartAmount, theme } = useContext(ShopContext); + + // Function to calculate the total price for each item considering the quantity + const calculateTotalPrice = (item) => { + return item.new_price * item.quantity; + }; + return (
@@ -18,21 +23,21 @@ const CartItems = () => {

Remove


- {cartItems.map((e) => { + {cartItems.map((item) => { return ( -
+
- -

{e.name}

-

${e.new_price}

-

{e?.size}

- -

${e.new_price}

+ +

{item.name}

+

${item.new_price}

+

{item.size}

+ +

${calculateTotalPrice(item)}

{/* Display total price for each item */} { - removeFromCart(e.id); + removeFromCart(item.id); }} alt="" /> diff --git a/src/Context/ShopContext.jsx b/src/Context/ShopContext.jsx index 4be3db9..ed5db5a 100644 --- a/src/Context/ShopContext.jsx +++ b/src/Context/ShopContext.jsx @@ -7,20 +7,36 @@ const ShopContextProvider = (props) => { const [cartItems, setcartItems] = useState([]); const [theme,setTheme]=useState("dark"); const addToCart = (itemId, size, quantity) => { - const cartProduct = all_product.find((product) => product.id === itemId); - cartProduct.size = size; - cartProduct.Quantity = quantity; - console.log(cartProduct); - setcartItems([...cartItems, cartProduct]); + const existingCartItemIndex = cartItems.findIndex(item => item.id === itemId && item.size === size); + + if (existingCartItemIndex !== -1) { + const updatedCartItems = cartItems.map((item, index) => { + if (index === existingCartItemIndex) { + return { + ...item, + quantity: item.quantity + quantity + }; + } + return item; + }); + setcartItems(updatedCartItems); + } else { + const cartProduct = all_product.find((product) => product.id === itemId); + cartProduct.size = size; + cartProduct.quantity = quantity; + setcartItems([...cartItems, cartProduct]); + } }; + const removeFromCart = (itemId) => { setcartItems(cartItems.filter((product) => product.id !== itemId)); }; const getTotalCartAmount = () => { - const total = 0; - return cartItems.map((product) => total + product.new_price)[0] || 0; + return cartItems.reduce((total, product) => total + (product.new_price * product.quantity), 0); }; + + const getTotalCartItems = () => { return cartItems.length;