smolSWE commited on
Commit
c336ed9
·
verified ·
1 Parent(s): 19a1ee1

Implement cart, checkout, and confirmation pages

Browse files

Implemented cart functionality, checkout form, and order confirmation. Updated script.js with necessary functions and event listeners.

Files changed (4) hide show
  1. cart.html +9 -20
  2. checkout.html +13 -13
  3. confirmation.html +3 -4
  4. script.js +430 -0
cart.html CHANGED
@@ -6,6 +6,7 @@
6
  <title>Shopping Cart - Amazon Clone</title>
7
  <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
8
  <link rel="stylesheet" href="style.css">
 
9
  </head>
10
  <body>
11
  <nav class="bg-gray-800 p-4 text-white">
@@ -22,29 +23,17 @@
22
  </nav>
23
 
24
  <main class="container mx-auto mt-8">
25
- <h1>Shopping Cart</h1>
26
- <div class="grid grid-cols-1 gap-4">
27
- <div class="flex items-center border border-gray-300 rounded-md p-4">
28
- <img src="https://via.placeholder.com/100" alt="Product Image" class="w-24 h-24 mr-4">
29
- <div>
30
- <h2 class="text-lg font-bold">Product Title</h2>
31
- <p class="text-gray-600">$99.99</p>
32
- <div class="flex items-center mt-2">
33
- <button class="bg-gray-200 px-2 py-1 rounded-md">-</button>
34
- <input type="number" value="1" class="w-16 text-center border border-gray-300 rounded-md mx-2">
35
- <button class="bg-gray-200 px-2 py-1 rounded-md">+</button>
36
- <button class="text-red-500 hover:text-red-700 ml-4">Remove</button>
37
- </div>
38
- </div>
39
- </div>
40
  </div>
41
 
42
  <div class="mt-8">
43
  <h2 class="text-xl font-bold">Order Summary</h2>
44
- <p>Subtotal: $99.99</p>
45
- <p>Shipping: $5.00</p>
46
- <p class="font-bold">Total: $104.99</p>
47
- <button class="bg-yellow-500 text-gray-800 px-4 py-2 rounded-md hover:bg-yellow-400">Proceed to Checkout</button>
48
  </div>
49
  </main>
50
 
@@ -54,4 +43,4 @@
54
 
55
  <script src="script.js"></script>
56
  </body>
57
- </html>
 
6
  <title>Shopping Cart - Amazon Clone</title>
7
  <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
8
  <link rel="stylesheet" href="style.css">
9
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" integrity="sha512-9usAa10IRO0HhonpyAIVpjrylPvoDwiPUiKdWk5t3PyolY1cOd4DSE0Ga+ri4AuTroPR5aQvXU9xC6qOPnzFeg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
10
  </head>
11
  <body>
12
  <nav class="bg-gray-800 p-4 text-white">
 
23
  </nav>
24
 
25
  <main class="container mx-auto mt-8">
26
+ <h1 class="text-2xl font-bold mb-4">Shopping Cart</h1>
27
+ <div id="cart-items" class="grid grid-cols-1 gap-4">
28
+ <!-- Cart items will be dynamically added here -->
 
 
 
 
 
 
 
 
 
 
 
 
29
  </div>
30
 
31
  <div class="mt-8">
32
  <h2 class="text-xl font-bold">Order Summary</h2>
33
+ <p>Subtotal: <span id="subtotal">$0.00</span></p>
34
+ <p>Shipping: <span id="shipping">$5.00</span></p>
35
+ <p class="font-bold">Total: <span id="total">$0.00</span></p>
36
+ <a href="/checkout.html" class="bg-yellow-500 text-gray-800 px-4 py-2 rounded-md hover:bg-yellow-400 inline-block">Proceed to Checkout</a>
37
  </div>
38
  </main>
39
 
 
43
 
44
  <script src="script.js"></script>
45
  </body>
46
+ </html>
checkout.html CHANGED
@@ -22,31 +22,31 @@
22
  </nav>
23
 
24
  <main class="container mx-auto mt-8">
25
- <h1>Checkout</h1>
26
- <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
27
  <div>
28
- <h2 class="text-xl font-bold">Shipping Information</h2>
29
- <form>
30
  <div class="mb-4">
31
  <label for="name" class="block text-gray-700 text-sm font-bold mb-2">Name:</label>
32
- <input type="text" id="name" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
33
  </div>
34
  <div class="mb-4">
35
  <label for="address" class="block text-gray-700 text-sm font-bold mb-2">Address:</label>
36
- <input type="text" id="address" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
37
  </div>
38
  <div class="mb-4">
39
  <label for="payment" class="block text-gray-700 text-sm font-bold mb-2">Payment Information:</label>
40
- <input type="text" id="payment" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
41
  </div>
 
42
  </form>
43
  </div>
44
  <div>
45
- <h2 class="text-xl font-bold">Order Summary</h2>
46
- <p>Subtotal: $99.99</p>
47
- <p>Shipping: $5.00</p>
48
- <p class="font-bold">Total: $104.99</p>
49
- <button class="bg-yellow-500 text-gray-800 px-4 py-2 rounded-md hover:bg-yellow-400">Place Order</button>
50
  </div>
51
  </div>
52
  </main>
@@ -57,4 +57,4 @@
57
 
58
  <script src="script.js"></script>
59
  </body>
60
- </html>
 
22
  </nav>
23
 
24
  <main class="container mx-auto mt-8">
25
+ <h1 class="text-2xl font-bold mb-4">Checkout</h1>
26
+ <div class="grid grid-cols-2 gap-8">
27
  <div>
28
+ <h2 class="text-xl font-bold mb-2">Shipping Information</h2>
29
+ <form id="checkout-form">
30
  <div class="mb-4">
31
  <label for="name" class="block text-gray-700 text-sm font-bold mb-2">Name:</label>
32
+ <input type="text" id="name" name="name" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
33
  </div>
34
  <div class="mb-4">
35
  <label for="address" class="block text-gray-700 text-sm font-bold mb-2">Address:</label>
36
+ <input type="text" id="address" name="address" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
37
  </div>
38
  <div class="mb-4">
39
  <label for="payment" class="block text-gray-700 text-sm font-bold mb-2">Payment Information:</label>
40
+ <input type="text" id="payment" name="payment" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
41
  </div>
42
+ <button type="submit" class="bg-yellow-500 text-gray-800 px-4 py-2 rounded-md hover:bg-yellow-400">Place Order</button>
43
  </form>
44
  </div>
45
  <div>
46
+ <h2 class="text-xl font-bold mb-2">Order Summary</h2>
47
+ <p>Subtotal: <span id="subtotal">$0.00</span></p>
48
+ <p>Shipping: <span id="shipping">$5.00</span></p>
49
+ <p class="font-bold">Total: <span id="total">$0.00</span></p>
 
50
  </div>
51
  </div>
52
  </main>
 
57
 
58
  <script src="script.js"></script>
59
  </body>
60
+ </html>
confirmation.html CHANGED
@@ -22,10 +22,9 @@
22
  </nav>
23
 
24
  <main class="container mx-auto mt-8">
25
- <h1>Order Confirmation</h1>
26
  <p>Thank you for your order!</p>
27
- <p>Your order has been placed and will be shipped soon.</p>
28
- <p>Order ID: #1234567890</p>
29
  </main>
30
 
31
  <footer class="bg-gray-800 p-4 text-white text-center mt-8">
@@ -34,4 +33,4 @@
34
 
35
  <script src="script.js"></script>
36
  </body>
37
- </html>
 
22
  </nav>
23
 
24
  <main class="container mx-auto mt-8">
25
+ <h1 class="text-2xl font-bold mb-4">Order Confirmation</h1>
26
  <p>Thank you for your order!</p>
27
+ <p>Your order ID is: <span id="order-id"></span></p>
 
28
  </main>
29
 
30
  <footer class="bg-gray-800 p-4 text-white text-center mt-8">
 
33
 
34
  <script src="script.js"></script>
35
  </body>
36
+ </html>
script.js CHANGED
@@ -0,0 +1,430 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ document.addEventListener('DOMContentLoaded', function() {
3
+ // Function to get cart items from local storage
4
+ function getCartItems() {
5
+ const cartItems = localStorage.getItem('cart');
6
+ return cartItems ? JSON.parse(cartItems) : [];
7
+ }
8
+
9
+ // Function to set cart items in local storage
10
+ function setCartItems(cartItems) {
11
+ localStorage.setItem('cart', JSON.stringify(cartItems));
12
+ }
13
+
14
+ // Function to add an item to the cart
15
+ function addToCart(productId, quantity) {
16
+ const cartItems = getCartItems();
17
+ const existingItem = cartItems.find(item => item.productId === productId);
18
+
19
+ if (existingItem) {
20
+ existingItem.quantity += quantity;
21
+ } else {
22
+ cartItems.push({ productId, quantity });
23
+ }
24
+
25
+ setCartItems(cartItems);
26
+ updateCartUI();
27
+ }
28
+
29
+ // Function to update item quantity in the cart
30
+ function updateQuantity(productId, quantity) {
31
+ const cartItems = getCartItems();
32
+ const itemIndex = cartItems.findIndex(item => item.productId === productId);
33
+
34
+ if (itemIndex !== -1) {
35
+ cartItems[itemIndex].quantity = quantity;
36
+ if (cartItems[itemIndex].quantity <= 0) {
37
+ cartItems.splice(itemIndex, 1); // Remove item if quantity is 0 or less
38
+ }
39
+ setCartItems(cartItems);
40
+ updateCartUI();
41
+ }
42
+ }
43
+
44
+ // Function to remove an item from the cart
45
+ function removeItem(productId) {
46
+ const cartItems = getCartItems();
47
+ const itemIndex = cartItems.findIndex(item => item.productId === productId);
48
+
49
+ if (itemIndex !== -1) {
50
+ cartItems.splice(itemIndex, 1);
51
+ setCartItems(cartItems);
52
+ updateCartUI();
53
+ }
54
+ }
55
+
56
+ // Function to calculate cart totals
57
+ function calculateTotals() {
58
+ const cartItems = getCartItems();
59
+ let subtotal = 0;
60
+
61
+ // Dummy product prices (replace with actual product data)
62
+ const productPrices = {
63
+ "1": 20.00,
64
+ "2": 30.00,
65
+ "3": 40.00
66
+ };
67
+
68
+ cartItems.forEach(item => {
69
+ const productId = item.productId;
70
+ const quantity = item.quantity;
71
+ const price = productPrices[productId] || 0; // Get price from productPrices, default to 0
72
+ subtotal += price * quantity;
73
+ });
74
+
75
+ const shipping = 5.00;
76
+ const total = subtotal + shipping;
77
+
78
+ return { subtotal, shipping, total };
79
+ }
80
+
81
+ // Function to display cart items in cart.html
82
+ function displayCartItems() {
83
+ const cartItems = getCartItems();
84
+ const cartItemsContainer = document.getElementById('cart-items');
85
+
86
+ if (cartItemsContainer) {
87
+ cartItemsContainer.innerHTML = ''; // Clear existing items
88
+
89
+ cartItems.forEach(item => {
90
+ // Dummy product data (replace with actual product data)
91
+ const productId = item.productId;
92
+ const product = {
93
+ id: productId,
94
+ title: `Product ${productId}`,
95
+ price: 20.00,
96
+ image: `https://via.placeholder.com/100`
97
+ };
98
+ const cartItemDiv = document.createElement('div');
99
+ cartItemDiv.classList.add('flex', 'items-center', 'border', 'border-gray-300', 'rounded-md', 'p-4');
100
+
101
+ cartItemDiv.innerHTML = `
102
+ <img src="${product.image}" alt="Product Image" class="w-24 h-24 mr-4">
103
+ <div>
104
+ <h2 class="text-lg font-bold">${product.title}</h2>
105
+ <p class="text-gray-600">$${product.price.toFixed(2)}</p>
106
+ <div class="flex items-center mt-2">
107
+ <button class="bg-gray-200 px-2 py-1 rounded-md decrease-quantity" data-product-id="${product.id}">-</button>
108
+ <input type="number" value="${item.quantity}" class="w-16 text-center border border-gray-300 rounded-md mx-2 quantity-input" data-product-id="${product.id}">
109
+ <button class="bg-gray-200 px-2 py-1 rounded-md increase-quantity" data-product-id="${product.id}">+</button>
110
+ <button class="text-red-500 hover:text-red-700 ml-4 remove-item" data-product-id="${product.id}">Remove</button>
111
+ </div>
112
+ </div>
113
+ `;
114
+
115
+ cartItemsContainer.appendChild(cartItemDiv);
116
+ });
117
+
118
+ // Add event listeners to quantity buttons and remove buttons
119
+ const decreaseButtons = document.querySelectorAll('.decrease-quantity');
120
+ decreaseButtons.forEach(button => {
121
+ button.addEventListener('click', function() {
122
+ const productId = this.dataset.productId;
123
+ const quantityInput = this.parentNode.querySelector('.quantity-input');
124
+ let quantity = parseInt(quantityInput.value);
125
+ quantity = Math.max(1, quantity - 1); // Ensure quantity is not less than 1
126
+ quantityInput.value = quantity;
127
+ updateQuantity(productId, quantity);
128
+ });
129
+ });
130
+
131
+ const increaseButtons = document.querySelectorAll('.increase-quantity');
132
+ increaseButtons.forEach(button => {
133
+ button.addEventListener('click', function() {
134
+ const productId = this.dataset.productId;
135
+ const quantityInput = this.parentNode.querySelector('.quantity-input');
136
+ let quantity = parseInt(quantityInput.value);
137
+ quantity++;
138
+ quantityInput.value = quantity;
139
+ updateQuantity(productId, quantity);
140
+ });
141
+ });
142
+
143
+ const removeButtons = document.querySelectorAll('.remove-item');
144
+ removeButtons.forEach(button => {
145
+ button.addEventListener('click', function() {
146
+ const productId = this.dataset.productId;
147
+ removeItem(productId);
148
+ });
149
+ });
150
+ }
151
+ }
152
+
153
+ // Function to update the order summary in cart.html and checkout.html
154
+ function updateOrderSummary() {
155
+ const { subtotal, shipping, total } = calculateTotals();
156
+
157
+ const subtotalElements = document.querySelectorAll('#subtotal');
158
+ subtotalElements.forEach(element => element.textContent = `$${subtotal.toFixed(2)}`);
159
+
160
+ const shippingElements = document.querySelectorAll('#shipping');
161
+ shippingElements.forEach(element => element.textContent = `$${shipping.toFixed(2)}`);
162
+
163
+ const totalElements = document.querySelectorAll('#total');
164
+ totalElements.forEach(element => element.textContent = `$${total.toFixed(2)}`);
165
+ }
166
+
167
+ // Function to handle checkout
168
+ function handleCheckout() {
169
+ const checkoutForm = document.getElementById('checkout-form');
170
+ if (checkoutForm) {
171
+ checkoutForm.addEventListener('submit', function(event) {
172
+ event.preventDefault(); // Prevent form submission
173
+
174
+ // Simulate order processing
175
+ const orderId = Math.floor(Math.random() * 1000000);
176
+
177
+ // Store order ID in local storage
178
+ localStorage.setItem('orderId', orderId);
179
+
180
+ // Clear the cart
181
+ setCartItems([]);
182
+
183
+ // Redirect to confirmation page
184
+ window.location.href = '/confirmation.html';
185
+ });
186
+ }
187
+ }
188
+
189
+ // Function to display order confirmation details
190
+ function displayOrderConfirmation() {
191
+ const orderId = localStorage.getItem('orderId');
192
+ const orderIdElement = document.getElementById('order-id');
193
+
194
+ if (orderIdElement) {
195
+ orderIdElement.textContent = orderId;
196
+ }
197
+ }
198
+
199
+ // Initialize the cart page
200
+ if (window.location.pathname === '/cart.html') {
201
+ displayCartItems();
202
+ updateOrderSummary();
203
+ }
204
+
205
+ // Initialize the checkout page
206
+ if (window.location.pathname === '/checkout.html') {
207
+ updateOrderSummary();
208
+ handleCheckout();
209
+ }
210
+
211
+ // Initialize the confirmation page
212
+ if (window.location.pathname === '/confirmation.html') {
213
+ displayOrderConfirmation();
214
+ }
215
+ });
216
+
217
+ // Function to get cart items from local storage
218
+ function getCartItems() {
219
+ const cartItems = localStorage.getItem('cart');
220
+ return cartItems ? JSON.parse(cartItems) : [];
221
+ }
222
+
223
+ // Function to set cart items in local storage
224
+ function setCartItems(cartItems) {
225
+ localStorage.setItem('cart', JSON.stringify(cartItems));
226
+ }
227
+
228
+ // Function to add an item to the cart
229
+ function addToCart(productId, quantity) {
230
+ const cartItems = getCartItems();
231
+ const existingItem = cartItems.find(item => item.productId === productId);
232
+
233
+ if (existingItem) {
234
+ existingItem.quantity += quantity;
235
+ } else {
236
+ cartItems.push({ productId, quantity });
237
+ }
238
+
239
+ setCartItems(cartItems);
240
+ updateCartUI();
241
+ }
242
+
243
+ // Function to update item quantity in the cart
244
+ function updateQuantity(productId, quantity) {
245
+ const cartItems = getCartItems();
246
+ const itemIndex = cartItems.findIndex(item => item.productId === productId);
247
+
248
+ if (itemIndex !== -1) {
249
+ cartItems[itemIndex].quantity = quantity;
250
+ if (cartItems[itemIndex].quantity <= 0) {
251
+ cartItems.splice(itemIndex, 1); // Remove item if quantity is 0 or less
252
+ }
253
+ setCartItems(cartItems);
254
+ updateCartUI();
255
+ }
256
+ }
257
+
258
+ // Function to remove an item from the cart
259
+ function removeItem(productId) {
260
+ const cartItems = getCartItems();
261
+ const itemIndex = cartItems.findIndex(item => item.productId === productId);
262
+
263
+ if (itemIndex !== -1) {
264
+ cartItems.splice(itemIndex, 1);
265
+ setCartItems(cartItems);
266
+ updateCartUI();
267
+ }
268
+ }
269
+
270
+ // Function to calculate cart totals
271
+ function calculateTotals() {
272
+ const cartItems = getCartItems();
273
+ let subtotal = 0;
274
+
275
+ // Dummy product prices (replace with actual product data)
276
+ const productPrices = {
277
+ "1": 20.00,
278
+ "2": 30.00,
279
+ "3": 40.00
280
+ };
281
+
282
+ cartItems.forEach(item => {
283
+ const productId = item.productId;
284
+ const quantity = item.quantity;
285
+ const price = productPrices[productId] || 0; // Get price from productPrices, default to 0
286
+ subtotal += price * quantity;
287
+ });
288
+
289
+ const shipping = 5.00;
290
+ const total = subtotal + shipping;
291
+
292
+ return { subtotal, shipping, total };
293
+ }
294
+
295
+ // Function to display cart items in cart.html
296
+ function displayCartItems() {
297
+ const cartItems = getCartItems();
298
+ const cartItemsContainer = document.getElementById('cart-items');
299
+
300
+ if (cartItemsContainer) {
301
+ cartItemsContainer.innerHTML = ''; // Clear existing items
302
+
303
+ cartItems.forEach(item => {
304
+ // Dummy product data (replace with actual product data)
305
+ const productId = item.productId;
306
+ const product = {
307
+ id: productId,
308
+ title: `Product ${productId}`,
309
+ price: 20.00,
310
+ image: `https://via.placeholder.com/100`
311
+ };
312
+ const cartItemDiv = document.createElement('div');
313
+ cartItemDiv.classList.add('flex', 'items-center', 'border', 'border-gray-300', 'rounded-md', 'p-4');
314
+
315
+ cartItemDiv.innerHTML = `
316
+ <img src="${product.image}" alt="Product Image" class="w-24 h-24 mr-4">
317
+ <div>
318
+ <h2 class="text-lg font-bold">${product.title}</h2>
319
+ <p class="text-gray-600">$${product.price.toFixed(2)}</p>
320
+ <div class="flex items-center mt-2">
321
+ <button class="bg-gray-200 px-2 py-1 rounded-md decrease-quantity" data-product-id="${product.id}">-</button>
322
+ <input type="number" value="${item.quantity}" class="w-16 text-center border border-gray-300 rounded-md mx-2 quantity-input" data-product-id="${product.id}">
323
+ <button class="bg-gray-200 px-2 py-1 rounded-md increase-quantity" data-product-id="${product.id}">+</button>
324
+ <button class="text-red-500 hover:text-red-700 ml-4 remove-item" data-product-id="${product.id}">Remove</button>
325
+ </div>
326
+ </div>
327
+ `;
328
+
329
+ cartItemsContainer.appendChild(cartItemDiv);
330
+ });
331
+
332
+ // Add event listeners to quantity buttons and remove buttons
333
+ const decreaseButtons = document.querySelectorAll('.decrease-quantity');
334
+ decreaseButtons.forEach(button => {
335
+ button.addEventListener('click', function() {
336
+ const productId = this.dataset.productId;
337
+ const quantityInput = this.parentNode.querySelector('.quantity-input');
338
+ let quantity = parseInt(quantityInput.value);
339
+ quantity = Math.max(1, quantity - 1); // Ensure quantity is not less than 1
340
+ quantityInput.value = quantity;
341
+ updateQuantity(productId, quantity);
342
+ });
343
+ });
344
+
345
+ const increaseButtons = document.querySelectorAll('.increase-quantity');
346
+ increaseButtons.forEach(button => {
347
+ button.addEventListener('click', function() {
348
+ const productId = this.dataset.productId;
349
+ const quantityInput = this.parentNode.querySelector('.quantity-input');
350
+ let quantity = parseInt(quantityInput.value);
351
+ quantity++;
352
+ quantityInput.value = quantity;
353
+ updateQuantity(productId, quantity);
354
+ });
355
+ });
356
+
357
+ const removeButtons = document.querySelectorAll('.remove-item');
358
+ removeButtons.forEach(button => {
359
+ button.addEventListener('click', function() {
360
+ const productId = this.dataset.productId;
361
+ removeItem(productId);
362
+ });
363
+ });
364
+ }
365
+ }
366
+
367
+ // Function to update the order summary in cart.html and checkout.html
368
+ function updateOrderSummary() {
369
+ const { subtotal, shipping, total } = calculateTotals();
370
+
371
+ const subtotalElements = document.querySelectorAll('#subtotal');
372
+ subtotalElements.forEach(element => element.textContent = `$${subtotal.toFixed(2)}`);
373
+
374
+ const shippingElements = document.querySelectorAll('#shipping');
375
+ shippingElements.forEach(element => element.textContent = `$${shipping.toFixed(2)}`);
376
+
377
+ const totalElements = document.querySelectorAll('#total');
378
+ totalElements.forEach(element => element.textContent = `$${total.toFixed(2)}`);
379
+ }
380
+
381
+ // Function to handle checkout
382
+ function handleCheckout() {
383
+ const checkoutForm = document.getElementById('checkout-form');
384
+ if (checkoutForm) {
385
+ checkoutForm.addEventListener('submit', function(event) {
386
+ event.preventDefault(); // Prevent form submission
387
+
388
+ // Simulate order processing
389
+ const orderId = Math.floor(Math.random() * 1000000);
390
+
391
+ // Store order ID in local storage
392
+ localStorage.setItem('orderId', orderId);
393
+
394
+ // Clear the cart
395
+ setCartItems([]);
396
+
397
+ // Redirect to confirmation page
398
+ window.location.href = '/confirmation.html';
399
+ });
400
+ }
401
+ }
402
+
403
+ // Function to display order confirmation details
404
+ function displayOrderConfirmation() {
405
+ const orderId = localStorage.getItem('orderId');
406
+ const orderIdElement = document.getElementById('order-id');
407
+
408
+ if (orderIdElement) {
409
+ orderIdElement.textContent = orderId;
410
+ }
411
+ }
412
+
413
+ document.addEventListener('DOMContentLoaded', function() {
414
+ // Initialize the cart page
415
+ if (window.location.pathname === '/cart.html') {
416
+ displayCartItems();
417
+ updateOrderSummary();
418
+ }
419
+
420
+ // Initialize the checkout page
421
+ if (window.location.pathname === '/checkout.html') {
422
+ updateOrderSummary();
423
+ handleCheckout();
424
+ }
425
+
426
+ // Initialize the confirmation page
427
+ if (window.location.pathname === '/confirmation.html') {
428
+ displayOrderConfirmation();
429
+ }
430
+ });