-
Notifications
You must be signed in to change notification settings - Fork 216
/
cart.html
119 lines (97 loc) · 3.37 KB
/
cart.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping Cart</title>
<link rel="stylesheet" href="cart.css">
</head>
<body>
<div class="shopping-cart">
<div class="title">Shopping Bag</div>
<!-- Container for dynamic products -->
<div id="cart-items-container">Your cart is empty.</div>
<!-- Total price section -->
<div class="total-price">
<span>Total: $<span id="total-price">0.00</span></span>
</div>
<div>
</div>
</div>
<script>
// Function to retrieve cart items from localStorage
const getCartItems = () => {
const cartItemsString = localStorage.getItem('cart');
return cartItemsString ? JSON.parse(cartItemsString) : [];
};
// Function to update the cart items in localStorage
const updateCartInLocalStorage = (cartItems) => {
localStorage.setItem('cart', JSON.stringify(cartItems));
};
// Function to display cart items
const displayCartItems = () => {
const cartItems = getCartItems();
const cartContainer = document.getElementById('cart-items-container');
cartContainer.innerHTML = ''; // Clear existing items
let total = 0;
if (cartItems.length === 0) {
cartContainer.innerHTML = '<p>Your cart is empty.</p>'; // Show message if cart is empty
} else {
cartContainer.innerHTML = `
<table class="cart-table">
<thead>
<tr>
<th>Image</th>
<th>Product Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
${cartItems.map((item,index) => {
const quantity = item.quantity || 1; // Default to 1 if quantity is undefined
const itemTotal = item.price * quantity;
return `
<tr class="cart-row">
<td><img src="${item.image}" alt="${item.name}" class="item-image" /></td>
<td>${item.name}</td>
<td>$${item.price}</td>
<td>
<button onclick="changeQuantity('${item.name}', -1)">-</button>
<span id="${item.name}-quantity">${quantity}</span>
<button onclick="changeQuantity('${item.name}', 1)">+</button>
</td>
<td>$${itemTotal.toFixed(2)}</td>
<td><button onclick="removeFromCart(${index})" class="removebtn"}>Remove</button></td>
</tr>
`;
}).join('')}
</tbody>
</table>
`;
// Calculate total price
total = cartItems.reduce((sum, item) => sum + item.price * (item.quantity || 1), 0);
}
document.getElementById('total-price').innerText = total.toFixed(2);
};
const changeQuantity = (itemName, change) => {
let cartItems = getCartItems();
const item = cartItems.find(item => item.name === itemName);
if (item) {
item.quantity = (item.quantity || 0) + change;
if (item.quantity < 1) {
cartItems = cartItems.filter(cartItem => cartItem.name !== itemName); // Remove item
}
updateCartInLocalStorage(cartItems); // Update local storage
displayCartItems(); // Re-render the cart items
}
};
// Call displayCartItems when the page loads to show existing cart items
document.addEventListener('DOMContentLoaded', () => {
displayCartItems();
});
</script>
</body>
</html>