nagasurendra commited on
Commit
e93043a
·
verified ·
1 Parent(s): 53ab93b

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +30 -15
templates/cart.html CHANGED
@@ -387,26 +387,41 @@
387
 
388
  function calculateDiscount() {
389
  let couponDropdown = document.getElementById('couponDropdown');
390
- let subtotal = parseFloat("{{ subtotal }}");
391
-
392
- // Check if a coupon is selected
393
  let selectedCoupon = couponDropdown.value.trim();
394
-
395
- if (!selectedCoupon || selectedCoupon === "") {
396
- // No coupon selected, reset the discount and total to the original values
397
- document.getElementById("discountText").innerText = `Discount: $0`;
398
- document.getElementById("totalBillText").innerText = `Total Bill: $${subtotal.toFixed(2)}`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  } else {
400
- // Apply a 10% discount (or your desired logic here)
401
- let discount = subtotal * 0.10;
402
- let total = subtotal - discount;
403
-
404
- // Update the UI with discount and new total
405
- document.getElementById("discountText").innerText = `$${discount.toFixed(2)}`;
406
- document.getElementById("totalBillText").innerText = `$${total.toFixed(2)}`;
407
  }
408
  }
409
 
 
410
  function proceedToOrder() {
411
  let couponCheckbox = document.getElementById('couponCheckbox');
412
  let couponDropdown = document.getElementById('couponDropdown');
 
387
 
388
  function calculateDiscount() {
389
  let couponDropdown = document.getElementById('couponDropdown');
 
 
 
390
  let selectedCoupon = couponDropdown.value.trim();
391
+
392
+ // Send the selected coupon to the backend for processing
393
+ if (selectedCoupon) {
394
+ // Perform AJAX request to send the selected coupon to the backend
395
+ fetch('/checkout', {
396
+ method: 'POST',
397
+ headers: {
398
+ 'Content-Type': 'application/json',
399
+ },
400
+ body: JSON.stringify({
401
+ selectedCoupon: selectedCoupon
402
+ })
403
+ })
404
+ .then(response => response.json())
405
+ .then(data => {
406
+ // Update the UI with the response (success or error)
407
+ if (data.success) {
408
+ document.getElementById("discountText").innerText = `$${data.discount.toFixed(2)}`;
409
+ document.getElementById("totalBillText").innerText = `$${data.totalBill.toFixed(2)}`;
410
+ } else {
411
+ alert(data.message);
412
+ }
413
+ })
414
+ .catch(error => {
415
+ console.error('Error:', error);
416
+ alert('There was an error applying the coupon.');
417
+ });
418
  } else {
419
+ // No coupon selected, handle reward points or reset discount
420
+ alert('Please select a valid coupon or apply rewards points.');
 
 
 
 
 
421
  }
422
  }
423
 
424
+
425
  function proceedToOrder() {
426
  let couponCheckbox = document.getElementById('couponCheckbox');
427
  let couponDropdown = document.getElementById('couponDropdown');