-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.html
More file actions
248 lines (212 loc) · 7.97 KB
/
Copy pathcart.html
File metadata and controls
248 lines (212 loc) · 7.97 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Cart - Suyambu Lingam Pazhamudhir Cholai</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
font-family: 'Poppins', sans-serif;
background: linear-gradient(to right, #FFDEE9, #B5FFFC);
color: #333;
}
.nav-bar {
background: #8B5E3B;
color: white;
padding: 1rem 2rem;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}
.nav-bar h1 {
margin: 0;
font-size: 2rem;
}
.nav-links button {
background: #D7263D;
color: white;
border: none;
padding: 0.8rem 1.5rem;
cursor: pointer;
transition: background 0.3s;
border-radius: 8px;
}
.nav-links button:hover {
background: #E76F51;
transform: scale(1.1);
}
.container {
margin-top: 20px;
}
.cart-item {
display: flex;
align-items: center;
justify-content: space-between;
background: white;
padding: 15px;
margin-bottom: 10px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.cart-item img {
width: 60px;
height: 60px;
border-radius: 8px;
margin-right: 10px;
}
.quantity-btn {
background-color: #4CAF50;
color: white;
border: none;
padding: 5px 10px;
border-radius: 8px;
cursor: pointer;
margin: 0 5px;
}
.quantity-btn:hover {
background-color: #45a049;
transform: scale(1.1);
}
.remove-btn {
background-color: #D7263D;
color: white;
border: none;
padding: 5px 10px;
border-radius: 8px;
cursor: pointer;
transition: background 0.3s;
}
.remove-btn:hover {
background-color: #E76F51;
transform: scale(1.1);
}
#cart-items p {
margin: 5px 0;
}
.empty-cart {
text-align: center;
margin-top: 50px;
font-size: 1.5rem;
color: #555;
}
.order-button {
display: block;
margin: 30px auto;
background: #ff6600;
color: white;
padding: 12px 30px;
font-size: 1.2rem;
border: none;
border-radius: 10px;
cursor: pointer;
transition: 0.3s ease;
}
.order-button:hover {
background: #e65c00;
transform: scale(1.05);
}
</style>
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg nav-bar">
<div class="container-fluid">
<h1 class="navbar-brand text-white">Suyambu Lingam Pazhamudhir Cholai</h1>
<div class="nav-links">
<a href="index.html"><button>Home</button></a>
<a href="login.html"><button>Login</button></a>
<a href="cart.html"><button>Cart</button></a>
<a href="orders.html"><button>My Orders</button></a>
</div>
</div>
</nav>
<div class="container">
<h2 class="text-center mt-4">🛒 My Cart</h2>
<div id="cart-items" class="mt-4"></div>
<div id="total-price" class="text-center mt-4"></div>
<button class="order-button" onclick="placeOrder()">🧾 Order Now</button>
</div>
<footer class="text-center py-3 bg-dark text-white">
<p>© Suyambu Lingam Pazhamudhir Cholai. All rights reserved.</p>
</footer>
<script>
let cart = JSON.parse(localStorage.getItem('cart')) || [];
function displayCart() {
const cartContainer = document.getElementById('cart-items');
const totalPriceContainer = document.getElementById('total-price');
cartContainer.innerHTML = '';
if (cart.length === 0) {
cartContainer.innerHTML = '<p class="empty-cart">Your cart is empty! 🥲</p>';
totalPriceContainer.innerHTML = '';
return;
}
let totalPrice = 0;
cart.forEach((item, index) => {
const itemTotal = item.quantity * item.price;
totalPrice += itemTotal;
cartContainer.innerHTML += `
<div class="cart-item">
<div><img src="${item.img}" alt="${item.name}"></div>
<div>
<p><strong>${item.name}</strong></p>
<p>Quantity: ${item.quantity} ${item.unit}</p>
<div>
<button class="quantity-btn" onclick="changeQuantity(${index}, -${item.name === 'Dragonfruit' ? 1 : 0.5})">➖</button>
<span>${item.quantity}</span>
<button class="quantity-btn" onclick="changeQuantity(${index}, ${item.name === 'Dragonfruit' ? 1 : 0.5})">➕</button>
</div>
<p>Price: Rs. ${itemTotal.toFixed(2)}</p>
</div>
<button onclick="removeFromCart(${index})" class="remove-btn">❌ Remove</button>
</div>
`;
});
totalPriceContainer.innerHTML = `<h4>Total: Rs. ${totalPrice.toFixed(2)}</h4>`;
}
function changeQuantity(index, change) {
if (cart[index].quantity + change > 0) {
cart[index].quantity = parseFloat((cart[index].quantity + change).toFixed(2));
} else {
cart.splice(index, 1);
}
localStorage.setItem('cart', JSON.stringify(cart));
displayCart();
}
function removeFromCart(index) {
cart.splice(index, 1);
localStorage.setItem('cart', JSON.stringify(cart));
displayCart();
}
function placeOrder() {
if (cart.length === 0) {
alert("Your cart is empty!");
return;
}
// Generate a unique order ID
const orderId = 'ORD-' + Date.now();
const orderDate = new Date().toISOString();
// Create order object
const order = {
id: orderId,
date: orderDate,
items: JSON.parse(JSON.stringify(cart)), // Deep copy the cart items
status: "Pending",
totalAmount: cart.reduce((total, item) => total + (item.quantity * item.price), 0)
};
// Get existing orders from localStorage or initialize empty array
let orders = JSON.parse(localStorage.getItem('orders')) || [];
// Add new order
orders.push(order);
// Save to localStorage
localStorage.setItem('orders', JSON.stringify(orders));
// Clear cart
localStorage.removeItem('cart');
cart = [];
// Show success message
alert(`Order placed successfully! ✅\nOrder ID: ${orderId}`);
// Refresh cart display
displayCart();
}
document.addEventListener('DOMContentLoaded', displayCart);
</script>
</body>
</html>