Rammohan0504 commited on
Commit
a51f61c
·
verified ·
1 Parent(s): 991c8b8

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +68 -67
templates/cart.html CHANGED
@@ -143,7 +143,6 @@
143
  <p class="text-center">Your cart is empty.</p>
144
  {% endfor %}
145
 
146
- <!-- Apply Coupon Section -->
147
  <div class="apply-coupon">
148
  <label for="coupon-code">Coupon Code:</label>
149
  <select id="coupon-code" name="coupon_code">
@@ -158,95 +157,97 @@
158
 
159
  <!-- Subtotal -->
160
  <div class="cart-summary">
161
- <p class="fw-bold">Subtotal: ${{ subtotal }}</p>
 
 
162
  <button class="checkout-button" onclick="proceedToOrder()">Proceed to Order</button>
163
  </div>
164
  </div>
165
  </div>
166
 
167
- <script>
168
- // Fetch coupon codes automatically when the page loads
169
- document.addEventListener("DOMContentLoaded", function() {
170
- const customerEmail = "{{ customer_email }}"; // The email stored in the session
171
 
172
- if (customerEmail) {
173
- fetch(`/get_coupon_codes?email=${customerEmail}`)
174
- .then(response => response.json())
175
- .then(data => {
176
- if (data.success && data.coupon_codes) {
177
- const couponSelect = document.getElementById('coupon-code');
178
- data.coupon_codes.forEach(coupon => {
179
- const option = document.createElement('option');
180
- option.value = coupon.Coupon_Code__c;
181
- option.textContent = coupon.Coupon_Code__c;
182
- couponSelect.appendChild(option);
183
- });
184
- } else {
185
- document.getElementById('coupon-message').innerText = "No active coupons found for your account.";
186
- document.getElementById('coupon-message').style.color = "red";
187
- }
188
- })
189
- .catch(error => {
190
- console.error('Error fetching coupon codes:', error);
191
- });
192
- }
193
- });
194
-
195
- // Apply coupon and discount
196
- function applyCoupon() {
197
- const couponCode = document.getElementById('coupon-code').value;
198
- const subtotal = parseFloat(document.getElementById('subtotal').innerText.replace('$', ''));
199
- const couponMessage = document.getElementById('coupon-message');
200
-
201
- if (couponCode) {
202
- fetch('/apply_coupon', {
203
- method: 'POST',
204
- headers: {
205
- 'Content-Type': 'application/json'
206
- },
207
- body: JSON.stringify({ coupon_code: couponCode, subtotal: subtotal })
208
- })
209
  .then(response => response.json())
210
  .then(data => {
211
- if (data.success) {
212
- const discount = data.discount;
213
- const totalPrice = subtotal - discount;
214
- document.getElementById('discount').innerText = discount.toFixed(2);
215
- document.getElementById('total-price').innerText = totalPrice.toFixed(2);
216
- couponMessage.innerText = `Coupon applied! You saved $${discount.toFixed(2)}`;
217
- couponMessage.style.color = 'green';
 
218
  } else {
219
- couponMessage.innerText = data.message;
220
- couponMessage.style.color = 'red';
221
  }
222
  })
223
  .catch(error => {
224
- console.error('Error applying coupon:', error);
225
- couponMessage.innerText = 'Error applying coupon.';
226
- couponMessage.style.color = 'red';
227
  });
228
- } else {
229
- couponMessage.innerText = 'Please select a coupon code.';
230
- couponMessage.style.color = 'red';
231
- }
232
  }
233
-
234
- function proceedToOrder() {
235
- fetch('/checkout', {
 
 
 
 
 
 
 
236
  method: 'POST',
 
 
 
 
237
  })
238
  .then(response => response.json())
239
  .then(data => {
240
  if (data.success) {
241
- alert(data.message);
242
- window.location.href = '/order'; // Redirect to menu or order confirmation page
 
 
 
 
243
  } else {
244
- alert(data.error || data.message);
 
245
  }
246
  })
247
- .catch(err => console.error('Error during checkout:', err));
 
 
 
 
 
 
 
248
  }
249
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
 
251
  </body>
252
  </html>
 
143
  <p class="text-center">Your cart is empty.</p>
144
  {% endfor %}
145
 
 
146
  <div class="apply-coupon">
147
  <label for="coupon-code">Coupon Code:</label>
148
  <select id="coupon-code" name="coupon_code">
 
157
 
158
  <!-- Subtotal -->
159
  <div class="cart-summary">
160
+ <p id="subtotal-text" class="fw-bold">Subtotal: ${{ subtotal }}</p>
161
+ <p id="discount-text" class="fw-bold">Discount: $0.00</p>
162
+ <p id="total-text" class="fw-bold">Total: ${{ subtotal }}</p> <!-- The final total after discount -->
163
  <button class="checkout-button" onclick="proceedToOrder()">Proceed to Order</button>
164
  </div>
165
  </div>
166
  </div>
167
 
168
+ <script>
169
+ // Fetch coupon codes automatically when the page loads
170
+ document.addEventListener("DOMContentLoaded", function() {
171
+ const customerEmail = "{{ customer_email }}"; // The email stored in the session
172
 
173
+ if (customerEmail) {
174
+ fetch(`/get_coupon_codes?email=${customerEmail}`)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  .then(response => response.json())
176
  .then(data => {
177
+ if (data.success && data.coupon_codes) {
178
+ const couponSelect = document.getElementById('coupon-code');
179
+ data.coupon_codes.forEach(coupon => {
180
+ const option = document.createElement('option');
181
+ option.value = coupon.Coupon_Code__c;
182
+ option.textContent = coupon.Coupon_Code__c;
183
+ couponSelect.appendChild(option);
184
+ });
185
  } else {
186
+ document.getElementById('coupon-message').innerText = "No active coupons found for your account.";
187
+ document.getElementById('coupon-message').style.color = "red";
188
  }
189
  })
190
  .catch(error => {
191
+ console.error('Error fetching coupon codes:', error);
 
 
192
  });
 
 
 
 
193
  }
194
+ });
195
+
196
+ // Apply coupon and discount
197
+ function applyCoupon() {
198
+ const couponCode = document.getElementById('coupon-code').value;
199
+ const subtotal = parseFloat(document.getElementById('subtotal-text').innerText.replace('Subtotal: $', ''));
200
+ const couponMessage = document.getElementById('coupon-message');
201
+
202
+ if (couponCode) {
203
+ fetch('/apply_coupon', {
204
  method: 'POST',
205
+ headers: {
206
+ 'Content-Type': 'application/json'
207
+ },
208
+ body: JSON.stringify({ coupon_code: couponCode, subtotal: subtotal })
209
  })
210
  .then(response => response.json())
211
  .then(data => {
212
  if (data.success) {
213
+ const discount = data.discount;
214
+ const totalPrice = subtotal - discount;
215
+ document.getElementById('discount-text').innerText = `Discount: $${discount.toFixed(2)}`;
216
+ document.getElementById('total-text').innerText = `Total: $${totalPrice.toFixed(2)}`;
217
+ couponMessage.innerText = `Coupon applied! You saved $${discount.toFixed(2)}`;
218
+ couponMessage.style.color = 'green';
219
  } else {
220
+ couponMessage.innerText = data.message;
221
+ couponMessage.style.color = 'red';
222
  }
223
  })
224
+ .catch(error => {
225
+ console.error('Error applying coupon:', error);
226
+ couponMessage.innerText = 'Error applying coupon.';
227
+ couponMessage.style.color = 'red';
228
+ });
229
+ } else {
230
+ couponMessage.innerText = 'Please select a coupon code.';
231
+ couponMessage.style.color = 'red';
232
  }
233
+ }
234
+
235
+ function proceedToOrder() {
236
+ fetch('/checkout', {
237
+ method: 'POST',
238
+ })
239
+ .then(response => response.json())
240
+ .then(data => {
241
+ if (data.success) {
242
+ alert(data.message);
243
+ window.location.href = '/order'; // Redirect to menu or order confirmation page
244
+ } else {
245
+ alert(data.error || data.message);
246
+ }
247
+ })
248
+ .catch(err => console.error('Error during checkout:', err));
249
+ }
250
+ </script>
251
 
252
  </body>
253
  </html>