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

Revert "Fix : Implement Dynamic Price Calculation in CartItems" #87

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Components/CartItems/CartItems.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
border: 2px solid #ebebeb;
background: #fff;
text-align: center;
color: rgb(5, 5, 5);
}

.cartitems-down{
Expand Down
27 changes: 11 additions & 16 deletions src/Components/CartItems/CartItems.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ 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 { cartItems, removeFromCart, getTotalCartAmount, theme } =
const { all_product, 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 (
<div className="cartitems">
<div className={`cartitems-format-main ci_${theme}`}>
Expand All @@ -23,21 +18,21 @@ const CartItems = () => {
<p>Remove</p>
</div>
<hr />
{cartItems.map((item) => {
{cartItems.map((e) => {
return (
<div key={item.id}>
<div>
<div className="cartitems-format cartitems-format-main">
<img src={item.image} alt="" className="carticon-product-icon" />
<p>{item.name}</p>
<p className="text-Align">${item.new_price}</p>
<p className="text-Align">{item.size}</p>
<button className="cartitems-quantity">{item.quantity}</button>
<p className="text-Align">${calculateTotalPrice(item)}</p> {/* Display total price for each item */}
<img src={e.image} alt="" className="carticon-product-icon" />
<p>{e.name}</p>
<p className="text-Align">${e.new_price}</p>
<p className="text-Align">{e?.size}</p>
<button className="cartitems-quantity">{e?.Quantity}</button>
<p className="text-Align">${e.new_price}</p>
<img
className="cartitems-remove-icon"
src={remove_icon}
onClick={() => {
removeFromCart(item.id);
removeFromCart(e.id);
}}
alt=""
/>
Expand Down
30 changes: 7 additions & 23 deletions src/Context/ShopContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,20 @@ const ShopContextProvider = (props) => {
const [cartItems, setcartItems] = useState([]);
const [theme,setTheme]=useState("dark");
const addToCart = (itemId, size, quantity) => {
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 cartProduct = all_product.find((product) => product.id === itemId);
cartProduct.size = size;
cartProduct.Quantity = quantity;
console.log(cartProduct);
setcartItems([...cartItems, cartProduct]);
};

const removeFromCart = (itemId) => {
setcartItems(cartItems.filter((product) => product.id !== itemId));
};

const getTotalCartAmount = () => {
return cartItems.reduce((total, product) => total + (product.new_price * product.quantity), 0);
const total = 0;
return cartItems.map((product) => total + product.new_price)[0] || 0;
};



const getTotalCartItems = () => {
return cartItems.length;
Expand Down
Loading