Rammohan0504 commited on
Commit
be0332d
·
verified ·
1 Parent(s): 4855889

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +155 -89
templates/cart.html CHANGED
@@ -9,7 +9,7 @@
9
  body {
10
  font-family: Arial, sans-serif;
11
  background-color: #f8f9fa;
12
- }
13
  .cart-container {
14
  max-width: 800px;
15
  margin: 20px auto;
@@ -24,7 +24,6 @@
24
  align-items: center;
25
  border-bottom: 1px solid #dee2e6;
26
  padding: 10px 0;
27
- position: relative; /* Allow absolute positioning of the close button */
28
  }
29
  .cart-item img {
30
  width: 70px;
@@ -106,22 +105,6 @@
106
  color: red;
107
  margin-top: 10px;
108
  }
109
-
110
- /* Close button styling */
111
- .close-btn {
112
- position: absolute;
113
- top: 10px;
114
- right: 10px;
115
- font-size: 1.5rem;
116
- color: #dc3545;
117
- cursor: pointer;
118
- transition: color 0.2s ease;
119
- }
120
-
121
- .close-btn:hover {
122
- color: #a71d2a;
123
- }
124
-
125
  </style>
126
  </head>
127
  <body>
@@ -131,12 +114,11 @@
131
 
132
  <!-- Cart Items -->
133
  {% for item in cart_items %}
134
- <div class="cart-item" id="item-{{ item.Name }}">
135
- <span class="close-btn" onclick="removeItem('{{ item.Name }}')">×</span> <!-- Close symbol -->
136
  <img src="{{ item.Image1__c }}" alt="{{ item.Name }}" onerror="this.src='/static/placeholder.jpg';">
137
  <div class="cart-item-details">
138
  <div class="cart-item-title">{{ item.Name }}</div>
139
- <div class="cart-item-addons">
140
  <small class="text-muted">Add-ons: {{ item.Add_Ons__c }}</small>
141
  </div>
142
  <div class="cart-item-instructions">
@@ -152,6 +134,7 @@
152
  <div class="text-primary">
153
  $<span class="item-price">{{ item.Price__c }}</span>
154
  </div>
 
155
  </div>
156
  </div>
157
  {% endfor %}
@@ -175,101 +158,184 @@
175
  <p id="total-text" class="fw-bold">Total: ${{ subtotal }}</p> <!-- The final total after discount -->
176
  <button class="checkout-button" onclick="proceedToOrder()">Proceed to Order</button>
177
  </div>
178
- </div>
179
- </div>
180
-
181
  <script>
182
- // Remove item from cart
183
- function removeItem(itemName) {
184
- fetch(`/remove_item/${itemName}`, {
185
- method: 'POST',
186
- headers: {
187
- 'Content-Type': 'application/json'
188
- },
189
- body: JSON.stringify({ item_name: itemName })
190
- })
191
- .then(response => response.json())
192
- .then(data => {
193
- if (data.success) {
194
- alert(data.message);
195
- location.reload(); // Reload the page to update the cart
196
- } else {
197
- alert(data.message);
198
- }
199
- })
200
- .catch(err => console.error('Error removing item:', err));
 
 
 
 
 
201
  }
 
 
202
 
203
  // Update item quantity
204
- function updateQuantity(action, itemName) {
205
- let quantityInput = document.querySelector(`#item-${itemName} input`);
206
  let quantity = parseInt(quantityInput.value);
 
207
  if (action === 'increase') {
208
  quantity++;
209
  } else if (action === 'decrease' && quantity > 1) {
210
  quantity--;
211
  }
212
- if (quantity < 1) {
213
- quantity = 1;
 
 
 
214
  }
215
- quantityInput.value = quantity;
216
- // You can make an API call here to update the cart in your backend (e.g., Salesforce)
217
- }
218
 
219
- // Apply coupon and discount
220
- function applyCoupon() {
221
- const couponCode = document.getElementById('coupon-code').value;
222
- const subtotal = parseFloat(document.getElementById('subtotal-text').innerText.replace('Subtotal: $', ''));
223
- const couponMessage = document.getElementById('coupon-message');
 
 
 
 
 
 
 
 
 
 
224
 
225
- if (couponCode) {
226
- fetch('/apply_coupon', {
227
- method: 'POST',
228
- headers: {
229
- 'Content-Type': 'application/json'
230
- },
231
- body: JSON.stringify({ coupon_code: couponCode, subtotal: subtotal })
232
- })
233
- .then(response => response.json())
234
- .then(data => {
235
- if (data.success) {
236
- const discount = data.discount;
237
- const totalPrice = subtotal - discount;
238
- document.getElementById('discount-text').innerText = `Discount: $${discount.toFixed(2)}`;
239
- document.getElementById('total-text').innerText = `Total: $${totalPrice.toFixed(2)}`;
240
- couponMessage.innerText = `Coupon applied! You saved $${discount.toFixed(2)}`;
241
- couponMessage.style.color = 'green';
242
  } else {
243
- couponMessage.innerText = data.message;
244
- couponMessage.style.color = 'red';
245
  }
246
- })
247
- .catch(error => {
248
- console.error('Error applying coupon:', error);
249
- couponMessage.innerText = 'Error applying coupon.';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  couponMessage.style.color = 'red';
251
- });
252
- } else {
253
- couponMessage.innerText = 'Please select a coupon code.';
 
 
254
  couponMessage.style.color = 'red';
255
- }
 
 
 
256
  }
 
257
 
258
- function proceedToOrder() {
259
- fetch('/checkout', {
260
  method: 'POST',
 
 
 
261
  })
262
  .then(response => response.json())
263
  .then(data => {
264
  if (data.success) {
265
  alert(data.message);
266
- window.location.href = '/order'; // Redirect to order summary page
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
  } else {
268
- alert(data.error || data.message);
269
  }
270
  })
271
- .catch(err => console.error('Error during checkout:', err));
272
  }
273
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
  </body>
275
- </html>
 
9
  body {
10
  font-family: Arial, sans-serif;
11
  background-color: #f8f9fa;
12
+ }
13
  .cart-container {
14
  max-width: 800px;
15
  margin: 20px auto;
 
24
  align-items: center;
25
  border-bottom: 1px solid #dee2e6;
26
  padding: 10px 0;
 
27
  }
28
  .cart-item img {
29
  width: 70px;
 
105
  color: red;
106
  margin-top: 10px;
107
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  </style>
109
  </head>
110
  <body>
 
114
 
115
  <!-- Cart Items -->
116
  {% for item in cart_items %}
117
+ <div class="cart-item">
 
118
  <img src="{{ item.Image1__c }}" alt="{{ item.Name }}" onerror="this.src='/static/placeholder.jpg';">
119
  <div class="cart-item-details">
120
  <div class="cart-item-title">{{ item.Name }}</div>
121
+ <div class="cart-item-addons">
122
  <small class="text-muted">Add-ons: {{ item.Add_Ons__c }}</small>
123
  </div>
124
  <div class="cart-item-instructions">
 
134
  <div class="text-primary">
135
  $<span class="item-price">{{ item.Price__c }}</span>
136
  </div>
137
+ <button class="btn btn-danger btn-sm" onclick="removeItem('{{ item.Name }}')">Remove</button>
138
  </div>
139
  </div>
140
  {% endfor %}
 
158
  <p id="total-text" class="fw-bold">Total: ${{ subtotal }}</p> <!-- The final total after discount -->
159
  <button class="checkout-button" onclick="proceedToOrder()">Proceed to Order</button>
160
  </div>
161
+
 
 
162
  <script>
163
+ // Fetch coupon codes automatically when the page loads
164
+ document.addEventListener("DOMContentLoaded", function() {
165
+ const customerEmail = "{{ customer_email }}"; // The email stored in the session
166
+
167
+ if (customerEmail) {
168
+ fetch(/get_coupon_codes?email=${customerEmail})
169
+ .then(response => response.json())
170
+ .then(data => {
171
+ if (data.success && data.coupon_codes) {
172
+ const couponSelect = document.getElementById('coupon-code');
173
+ data.coupon_codes.forEach(coupon => {
174
+ const option = document.createElement('option');
175
+ option.value = coupon.Coupon_Code__c;
176
+ option.textContent = coupon.Coupon_Code__c;
177
+ couponSelect.appendChild(option);
178
+ });
179
+ } else {
180
+ document.getElementById('coupon-message').innerText = "No active coupons found for your account.";
181
+ document.getElementById('coupon-message').style.color = "red";
182
+ }
183
+ })
184
+ .catch(error => {
185
+ console.error('Error fetching coupon codes:', error);
186
+ });
187
  }
188
+ });
189
+
190
 
191
  // Update item quantity
192
+ function updateQuantity(action, itemName, customerEmail) {
193
+ let quantityInput = document.querySelector(input[data-item-name="${itemName}"]);
194
  let quantity = parseInt(quantityInput.value);
195
+ // Update quantity based on action
196
  if (action === 'increase') {
197
  quantity++;
198
  } else if (action === 'decrease' && quantity > 1) {
199
  quantity--;
200
  }
201
+
202
+ // Validate quantity
203
+ if (isNaN(quantity) || quantity < 1) {
204
+ alert("Invalid quantity!");
205
+ return;
206
  }
 
 
 
207
 
208
+ // Send updated quantity to the server
209
+ fetch('/cart/update_quantity', {
210
+ method: 'POST',
211
+ headers: { 'Content-Type': 'application/json' },
212
+ body: JSON.stringify({ email: customerEmail, item_name: itemName, quantity: quantity })
213
+ })
214
+ .then(response => response.json())
215
+ .then(data => {
216
+ if (data.success) {
217
+ // Update the item price and quantity in the UI
218
+ quantityInput.value = quantity;
219
+ let itemElement = quantityInput.closest(".cart-item"); // Locate the parent cart item
220
+ if (itemElement) {
221
+ let basePriceElement = itemElement.querySelector(".base-price");
222
+ let addonsPriceElement = itemElement.querySelector(".addons-price");
223
 
224
+ // Update the base price
225
+ if (basePriceElement) {
226
+ basePriceElement.innerText = data.new_item_price.toFixed(2); // Assuming backend sends this
227
+ }
228
+
229
+ // Update add-ons price if needed (optional)
230
+ if (addonsPriceElement && data.addons_price !== undefined) {
231
+ addonsPriceElement.innerText = data.addons_price.toFixed(2);
232
+ }
 
 
 
 
 
 
 
 
233
  } else {
234
+ console.error(Parent cart item element not found for item: ${itemName});
 
235
  }
236
+ location.reload();
237
+
238
+ // Recalculate subtotal dynamically
239
+
240
+ } else {
241
+ alert("Error updating quantity: " + data.error);
242
+ }
243
+ })
244
+ .catch(err => console.error("Error:", err));
245
+ }
246
+
247
+
248
+ // Apply coupon and discount
249
+ function applyCoupon() {
250
+ const couponCode = document.getElementById('coupon-code').value;
251
+ const subtotal = parseFloat(document.getElementById('subtotal-text').innerText.replace('Subtotal: $', ''));
252
+ const couponMessage = document.getElementById('coupon-message');
253
+
254
+ if (couponCode) {
255
+ fetch('/apply_coupon', {
256
+ method: 'POST',
257
+ headers: {
258
+ 'Content-Type': 'application/json'
259
+ },
260
+ body: JSON.stringify({ coupon_code: couponCode, subtotal: subtotal })
261
+ })
262
+ .then(response => response.json())
263
+ .then(data => {
264
+ if (data.success) {
265
+ const discount = data.discount;
266
+ const totalPrice = subtotal - discount;
267
+ document.getElementById('discount-text').innerText = Discount: $${discount.toFixed(2)};
268
+ document.getElementById('total-text').innerText = Total: $${totalPrice.toFixed(2)};
269
+ couponMessage.innerText = Coupon applied! You saved $${discount.toFixed(2)};
270
+ couponMessage.style.color = 'green';
271
+ } else {
272
+ couponMessage.innerText = data.message;
273
  couponMessage.style.color = 'red';
274
+ }
275
+ })
276
+ .catch(error => {
277
+ console.error('Error applying coupon:', error);
278
+ couponMessage.innerText = 'Error applying coupon.';
279
  couponMessage.style.color = 'red';
280
+ });
281
+ } else {
282
+ couponMessage.innerText = 'Please select a coupon code.';
283
+ couponMessage.style.color = 'red';
284
  }
285
+ }
286
 
287
+ function removeItemFromCart(itemName) {
288
+ fetch(/cart/remove/${encodeURIComponent(itemName)}, {
289
  method: 'POST',
290
+ headers: {
291
+ 'Content-Type': 'application/json'
292
+ }
293
  })
294
  .then(response => response.json())
295
  .then(data => {
296
  if (data.success) {
297
  alert(data.message);
298
+ location.reload(); // Reload the page to update the cart
299
+ } else {
300
+ alert(data.message);
301
+ }
302
+ })
303
+ .catch(err => console.error('Error removing item:', err));
304
+ }
305
+
306
+ function addSuggestion(itemId) {
307
+ fetch(/cart/add_suggestion/${itemId}, {
308
+ method: 'POST',
309
+ headers: { 'Content-Type': 'application/json' },
310
+ body: JSON.stringify({})
311
+ })
312
+ .then(response => response.json())
313
+ .then(data => {
314
+ if (data.success) {
315
+ alert('Item added to cart!');
316
+ location.reload();
317
  } else {
318
+ alert(data.error);
319
  }
320
  })
321
+ .catch(err => console.error('Error adding suggestion:', err));
322
  }
323
+
324
+ function proceedToOrder() {
325
+ fetch('/checkout', {
326
+ method: 'POST',
327
+ })
328
+ .then(response => response.json())
329
+ .then(data => {
330
+ if (data.success) {
331
+ alert(data.message);
332
+ window.location.href = '/order'; // Redirect to menu or order confirmation page
333
+ } else {
334
+ alert(data.error || data.message);
335
+ }
336
+ })
337
+ .catch(err => console.error('Error during checkout:', err));
338
+ }
339
+ </script>
340
  </body>
341
+ </html>