Rammohan0504 commited on
Commit
750c1a8
·
verified ·
1 Parent(s): 4d19b04

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +74 -101
templates/cart.html CHANGED
@@ -75,22 +75,12 @@
75
  color: #fff;
76
  padding: 5px 20px;
77
  font-size: 1rem;
78
- font-weight: bold;
79
  cursor: pointer;
80
  border-radius: 5px;
81
- border: none;
82
- transition: background-color 0.3s ease, transform 0.2s ease;
83
  }
84
  .apply-button:disabled {
85
  background-color: #6c757d;
86
- cursor: not-allowed;
87
- }
88
-
89
- .apply-button:hover {
90
- background-color: #218838; /* Darker green on hover */
91
- transform: scale(1.05); /* Slightly enlarge the button when hovered */
92
  }
93
-
94
  .checkout-button {
95
  background-color: #007bff;
96
  color: #fff;
@@ -118,12 +108,6 @@
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">
125
- <small class="text-muted">Instructions: {{ item.Instructions__c or "None" }}</small>
126
- </div>
127
  <div class="cart-item-quantity">
128
  <button onclick="updateQuantity('decrease', '{{ item.Name }}')">-</button>
129
  <input type="text" value="{{ item.Quantity__c }}" readonly>
@@ -139,58 +123,52 @@
139
  </div>
140
  {% endfor %}
141
 
142
- <div class="apply-coupon">
143
- <label for="coupon-code">Coupon Code:</label>
144
- <select id="coupon-code" name="coupon_code">
145
- <option value="">Select a coupon code</option>
146
- {% for coupon in coupon_codes %}
147
- <option value="{{ coupon.Coupon_Code__c }}">{{ coupon.Coupon_Code__c }}</option>
148
- {% endfor %}
149
- </select>
150
- <button type="button" class="apply-button" onclick="applyCoupon()">Apply Coupon</button>
151
- <p id="coupon-message" class="coupon-message"></p>
152
- </div>
153
-
154
- <!-- Subtotal -->
155
- <div class="cart-summary">
156
- <p id="subtotal-text" class="fw-bold">Subtotal: ${{ subtotal }}</p>
157
- <p id="discount-text" class="fw-bold">Discount: $0.00</p>
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) {
193
- let quantityInput = document.querySelector(input[data-item-name="${itemName}"]);
194
  let quantity = parseInt(quantityInput.value);
195
  if (action === 'increase') {
196
  quantity++;
@@ -205,60 +183,55 @@
205
  updateCart(itemName, quantity);
206
  }
207
 
208
- // Apply coupon and discount
209
- function applyCoupon() {
210
- const couponCode = document.getElementById('coupon-code').value;
211
- const subtotal = parseFloat(document.getElementById('subtotal-text').innerText.replace('Subtotal: $', ''));
212
- const couponMessage = document.getElementById('coupon-message');
 
 
213
 
214
- if (couponCode) {
215
- fetch('/apply_coupon', {
216
  method: 'POST',
217
- headers: {
218
- 'Content-Type': 'application/json'
219
- },
220
- body: JSON.stringify({ coupon_code: couponCode, subtotal: subtotal })
221
  })
222
  .then(response => response.json())
223
  .then(data => {
224
  if (data.success) {
225
- const discount = data.discount;
226
- const totalPrice = subtotal - discount;
227
- document.getElementById('discount-text').innerText = Discount: $${discount.toFixed(2)};
228
- document.getElementById('total-text').innerText = Total: $${totalPrice.toFixed(2)};
229
- couponMessage.innerText = Coupon applied! You saved $${discount.toFixed(2)};
230
- couponMessage.style.color = 'green';
231
  } else {
232
- couponMessage.innerText = data.message;
233
- couponMessage.style.color = 'red';
234
  }
235
  })
236
- .catch(error => {
237
- console.error('Error applying coupon:', error);
238
- couponMessage.innerText = 'Error applying coupon.';
239
- couponMessage.style.color = 'red';
 
 
 
 
 
 
 
 
240
  });
241
- } else {
242
- couponMessage.innerText = 'Please select a coupon code.';
243
- couponMessage.style.color = 'red';
 
244
  }
245
- }
246
 
247
- function proceedToOrder() {
248
- fetch('/checkout', {
249
- method: 'POST',
250
- })
251
- .then(response => response.json())
252
- .then(data => {
253
- if (data.success) {
254
- alert(data.message);
255
- window.location.href = '/order'; // Redirect to menu or order confirmation page
256
- } else {
257
- alert(data.error || data.message);
258
- }
259
- })
260
- .catch(err => console.error('Error during checkout:', err));
261
- }
262
- </script>
263
  </body>
264
- </html>
 
75
  color: #fff;
76
  padding: 5px 20px;
77
  font-size: 1rem;
 
78
  cursor: pointer;
79
  border-radius: 5px;
 
 
80
  }
81
  .apply-button:disabled {
82
  background-color: #6c757d;
 
 
 
 
 
 
83
  }
 
84
  .checkout-button {
85
  background-color: #007bff;
86
  color: #fff;
 
108
  <img src="{{ item.Image1__c }}" alt="{{ item.Name }}" onerror="this.src='/static/placeholder.jpg';">
109
  <div class="cart-item-details">
110
  <div class="cart-item-title">{{ item.Name }}</div>
 
 
 
 
 
 
111
  <div class="cart-item-quantity">
112
  <button onclick="updateQuantity('decrease', '{{ item.Name }}')">-</button>
113
  <input type="text" value="{{ item.Quantity__c }}" readonly>
 
123
  </div>
124
  {% endfor %}
125
 
126
+ <!-- Coupon Code Section -->
127
+ <div class="coupon-section">
128
+ <select id="couponCode" class="form-control" style="width: 70%;"></select>
129
+ <button class="apply-button" onclick="applyCoupon()">Apply Coupon</button>
130
+ </div>
131
+
132
+ <div class="error-message" id="couponError"></div>
133
+
134
+ <!-- Cart Summary -->
135
+ <div class="cart-summary">
136
+ <p><strong>Subtotal: </strong><span id="subtotal">${{ subtotal }}</span></p>
137
+ <p><strong>Discount: </strong><span id="discount">0.00</span></p>
138
+ <p><strong>Total: </strong><span id="total">$<span id="total-price">{{ total_price }}</span></span></p>
139
+ </div>
 
 
 
 
 
 
 
 
 
 
140
 
141
+ <!-- Checkout Button -->
142
+ <button class="checkout-button" onclick="proceedToOrder()">Proceed to Order</button>
143
+ </div>
144
+ </div>
145
+
146
+ <script>
147
+ // Fetch the coupon codes related to the logged-in user and populate the dropdown
148
+ document.addEventListener("DOMContentLoaded", function() {
149
+ const customerEmail = "{{ customer_email }}"; // Assuming email is passed to the template
150
+
151
+ fetch(`/get_coupons/${customerEmail}`)
152
  .then(response => response.json())
153
  .then(data => {
154
+ if (data.success) {
155
+ let couponDropdown = document.getElementById("couponCode");
156
+ data.coupons.forEach(coupon => {
157
+ let option = document.createElement("option");
158
  option.value = coupon.Coupon_Code__c;
159
+ option.text = coupon.Coupon_Code__c;
160
+ couponDropdown.appendChild(option);
161
  });
162
  } else {
163
+ document.getElementById('couponError').innerText = "No coupons available.";
 
164
  }
165
  })
166
+ .catch(error => console.error("Error fetching coupons:", error));
167
+ });
 
 
 
 
168
 
169
  // Update item quantity
170
  function updateQuantity(action, itemName) {
171
+ let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
172
  let quantity = parseInt(quantityInput.value);
173
  if (action === 'increase') {
174
  quantity++;
 
183
  updateCart(itemName, quantity);
184
  }
185
 
186
+ // Apply coupon
187
+ function applyCoupon() {
188
+ let couponCode = document.getElementById('couponCode').value;
189
+ if (!couponCode) {
190
+ document.getElementById('couponError').innerText = "Please select a coupon.";
191
+ return;
192
+ }
193
 
194
+ fetch(`/apply_coupon`, {
 
195
  method: 'POST',
196
+ headers: { 'Content-Type': 'application/json' },
197
+ body: JSON.stringify({ coupon_code: couponCode })
 
 
198
  })
199
  .then(response => response.json())
200
  .then(data => {
201
  if (data.success) {
202
+ let discount = data.discount;
203
+ document.getElementById('discount').innerText = discount.toFixed(2);
204
+ let subtotal = parseFloat(document.getElementById('subtotal').innerText.replace('$', ''));
205
+ let total = subtotal - discount;
206
+ document.getElementById('total-price').innerText = total.toFixed(2);
207
+ document.getElementById('couponError').innerText = `Coupon applied! You saved $${discount.toFixed(2)}`;
208
  } else {
209
+ document.getElementById('couponError').innerText = "Invalid coupon code.";
 
210
  }
211
  })
212
+ .catch(error => console.error("Error applying coupon:", error));
213
+ }
214
+
215
+ // Update the cart prices based on the quantity changes
216
+ function updateCart(itemName, quantity) {
217
+ let price = parseFloat(document.querySelector(`.item[data-item-name="${itemName}"] .item-price`).innerText.replace('$', ''));
218
+ let newPrice = price * quantity;
219
+ document.querySelector(`.item[data-item-name="${itemName}"] .item-price`).innerText = newPrice.toFixed(2);
220
+ // Update subtotal and total
221
+ let subtotal = 0;
222
+ document.querySelectorAll('.item').forEach(item => {
223
+ subtotal += parseFloat(item.querySelector('.item-price').innerText.replace('$', ''));
224
  });
225
+ document.getElementById('subtotal').innerText = "$" + subtotal.toFixed(2);
226
+ let discount = parseFloat(document.getElementById('discount').innerText);
227
+ let total = subtotal - discount;
228
+ document.getElementById('total-price').innerText = total.toFixed(2);
229
  }
 
230
 
231
+ // Proceed to the order summary page
232
+ function proceedToOrder() {
233
+ window.location.href = '/order_summary'; // Redirect to order summary page
234
+ }
235
+ </script>
 
 
 
 
 
 
 
 
 
 
 
236
  </body>
237
+ </html>