Rammohan0504 commited on
Commit
27cccb5
·
verified ·
1 Parent(s): 61c3999

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +67 -129
templates/cart.html CHANGED
@@ -77,6 +77,31 @@
77
  cursor: pointer;
78
  margin-top: 10px;
79
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  .suggestion-section {
81
  margin-top: 20px;
82
  padding: 15px;
@@ -143,7 +168,6 @@
143
  <div class="cart-item-actions">
144
  <div class="text-primary">
145
  $<span class="base-price">{{ item.Price__c }}</span>
146
-
147
  </div>
148
 
149
  <button class="btn btn-danger btn-sm" onclick="removeItemFromCart('{{ item.Name }}')">Remove</button>
@@ -153,6 +177,14 @@
153
  <p class="text-center">Your cart is empty.</p>
154
  {% endfor %}
155
 
 
 
 
 
 
 
 
 
156
  <!-- Subtotal -->
157
  <div class="cart-summary">
158
  <p class="fw-bold">Subtotal: ${{ subtotal }}</p>
@@ -162,7 +194,6 @@
162
 
163
  <!-- Suggestions Section -->
164
  <div class="suggestion-section">
165
-
166
  {% for suggestion in suggestions %}
167
  <div class="suggestion-item">
168
  <img src="{{ suggestion.Image1__c }}" alt="{{ suggestion.Name }}" onerror="this.src='/static/placeholder.jpg';">
@@ -177,78 +208,44 @@
177
  </div>
178
 
179
  <script>
180
- // Example function to handle the increase/decrease button clicks
181
- function updateQuantity(action, itemName, customerEmail) {
182
- let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
183
- let quantity = parseInt(quantityInput.value);
184
- // Update quantity based on action
185
- if (action === 'increase') {
186
- quantity++;
187
- } else if (action === 'decrease' && quantity > 1) {
188
- quantity--;
189
- }
190
 
191
- // Validate quantity
192
- if (isNaN(quantity) || quantity < 1) {
193
- alert("Invalid quantity!");
194
- return;
195
- }
196
-
197
- // Send updated quantity to the server
198
- fetch('/cart/update_quantity', {
199
- method: 'POST',
200
- headers: { 'Content-Type': 'application/json' },
201
- body: JSON.stringify({ email: customerEmail, item_name: itemName, quantity: quantity })
202
- })
203
- .then(response => response.json())
204
- .then(data => {
205
- if (data.success) {
206
- // Update the item price and quantity in the UI
207
- quantityInput.value = quantity;
208
- let itemElement = quantityInput.closest(".cart-item"); // Locate the parent cart item
209
- if (itemElement) {
210
- let basePriceElement = itemElement.querySelector(".base-price");
211
- let addonsPriceElement = itemElement.querySelector(".addons-price");
212
-
213
- // Update the base price
214
- if (basePriceElement) {
215
- basePriceElement.innerText = data.new_item_price.toFixed(2); // Assuming backend sends this
216
- }
217
-
218
- // Update add-ons price if needed (optional)
219
- if (addonsPriceElement && data.addons_price !== undefined) {
220
- addonsPriceElement.innerText = data.addons_price.toFixed(2);
221
- }
222
  } else {
223
- console.error(`Parent cart item element not found for item: ${itemName}`);
 
224
  }
225
- location.reload();
226
-
227
- // Recalculate subtotal dynamically
228
-
229
- } else {
230
- alert("Error updating quantity: " + data.error);
231
- }
232
- })
233
- .catch(err => console.error("Error:", err));
 
234
  }
235
-
236
 
237
- function calculateSubtotal() {
238
- let subtotal = 0;
239
- document.querySelectorAll('.cart-item').forEach(item => {
240
- const quantity = parseInt(item.querySelector('input').value);
241
- const basePrice = parseFloat(item.querySelector('.base-price').innerText.replace('$', '')); // Base Price
242
- const addonsPrice = parseFloat(item.querySelector('.addons-price').innerText.replace('$', '')) || 0; // Add-ons Price
243
- subtotal += (basePrice * quantity) + addonsPrice; // Include add-ons price in subtotal
244
- });
245
-
246
- // Update the subtotal in the HTML
247
- document.querySelector('.cart-summary p').innerText = `Subtotal: $${subtotal.toFixed(2)}`;
248
- return subtotal;
249
- }
250
-
251
-
252
  function proceedToOrder() {
253
  fetch('/checkout', {
254
  method: 'POST',
@@ -264,65 +261,6 @@
264
  })
265
  .catch(err => console.error('Error during checkout:', err));
266
  }
267
- function addSuggestion(itemId) {
268
- fetch(`/cart/add_suggestion/${itemId}`, {
269
- method: 'POST',
270
- headers: { 'Content-Type': 'application/json' },
271
- body: JSON.stringify({})
272
- })
273
- .then(response => response.json())
274
- .then(data => {
275
- if (data.success) {
276
- alert('Item added to cart!');
277
- location.reload();
278
- } else {
279
- alert(data.error);
280
- }
281
- })
282
- .catch(err => console.error('Error adding suggestion:', err));
283
- }
284
- function removeItemFromCart(itemName) {
285
- fetch(`/cart/remove/${encodeURIComponent(itemName)}`, {
286
- method: 'POST',
287
- headers: {
288
- 'Content-Type': 'application/json'
289
- }
290
- })
291
- .then(response => response.json())
292
- .then(data => {
293
- if (data.success) {
294
- alert(data.message);
295
- location.reload(); // Reload the page to update the cart
296
- } else {
297
- alert(data.message);
298
- }
299
- })
300
- .catch(err => console.error('Error removing item:', err));
301
- }
302
- function addToCart(itemName, customerEmail) {
303
- fetch(`/cart/add_item`, {
304
- method: "POST",
305
- headers: { "Content-Type": "application/json" },
306
- body: JSON.stringify({
307
- email: customerEmail,
308
- item_name: itemName.trim(), //Ensure the item name is trimmed
309
- quantity: 0 // DEFAULT QUANTITY PASSED HERE
310
- })
311
- })
312
- .then(response => response.json())
313
- .then(data => {
314
- if (data.success) {
315
- alert("Item added/updated successfully.");
316
- location.reload(); // Reload the page to update the cart
317
- } else {
318
- alert(data.error || "Failed to add item to cart.");
319
- }
320
- })
321
- .catch(err => console.error("Error adding item to cart:", err));
322
- }
323
-
324
-
325
  </script>
326
-
327
  </body>
328
- </html>
 
77
  cursor: pointer;
78
  margin-top: 10px;
79
  }
80
+ .apply-coupon {
81
+ margin-top: 20px;
82
+ text-align: right;
83
+ }
84
+ .apply-coupon input {
85
+ width: 200px;
86
+ padding: 5px;
87
+ margin-right: 10px;
88
+ }
89
+ .apply-coupon button {
90
+ background-color: #28a745;
91
+ color: white;
92
+ padding: 5px 15px;
93
+ border: none;
94
+ cursor: pointer;
95
+ font-size: 1rem;
96
+ }
97
+ .apply-coupon button:hover {
98
+ background-color: #218838;
99
+ }
100
+ .coupon-message {
101
+ margin-top: 10px;
102
+ font-size: 1rem;
103
+ color: red;
104
+ }
105
  .suggestion-section {
106
  margin-top: 20px;
107
  padding: 15px;
 
168
  <div class="cart-item-actions">
169
  <div class="text-primary">
170
  $<span class="base-price">{{ item.Price__c }}</span>
 
171
  </div>
172
 
173
  <button class="btn btn-danger btn-sm" onclick="removeItemFromCart('{{ item.Name }}')">Remove</button>
 
177
  <p class="text-center">Your cart is empty.</p>
178
  {% endfor %}
179
 
180
+ <!-- Apply Coupon Section -->
181
+ <div class="apply-coupon">
182
+ <label for="coupon-code">Coupon Code:</label>
183
+ <input type="text" id="coupon-code" name="coupon_code" placeholder="Enter coupon code">
184
+ <button type="button" onclick="applyCoupon()">Apply Coupon</button>
185
+ <p id="coupon-message" class="coupon-message"></p>
186
+ </div>
187
+
188
  <!-- Subtotal -->
189
  <div class="cart-summary">
190
  <p class="fw-bold">Subtotal: ${{ subtotal }}</p>
 
194
 
195
  <!-- Suggestions Section -->
196
  <div class="suggestion-section">
 
197
  {% for suggestion in suggestions %}
198
  <div class="suggestion-item">
199
  <img src="{{ suggestion.Image1__c }}" alt="{{ suggestion.Name }}" onerror="this.src='/static/placeholder.jpg';">
 
208
  </div>
209
 
210
  <script>
211
+ function applyCoupon() {
212
+ const couponCode = document.getElementById('coupon-code').value;
213
+ const subtotal = parseFloat(document.getElementById('subtotal').innerText);
214
+ const couponMessage = document.getElementById('coupon-message');
 
 
 
 
 
 
215
 
216
+ if (couponCode) {
217
+ fetch('/apply_coupon', {
218
+ method: 'POST',
219
+ headers: {
220
+ 'Content-Type': 'application/json'
221
+ },
222
+ body: JSON.stringify({ coupon_code: couponCode, subtotal: subtotal })
223
+ })
224
+ .then(response => response.json())
225
+ .then(data => {
226
+ if (data.success) {
227
+ const discount = data.discount;
228
+ const totalPrice = subtotal - discount;
229
+ document.getElementById('discount').innerText = discount;
230
+ document.getElementById('total-price').innerText = totalPrice;
231
+ couponMessage.innerText = `Coupon applied! You saved $${discount}`;
232
+ couponMessage.style.color = 'green';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  } else {
234
+ couponMessage.innerText = data.message;
235
+ couponMessage.style.color = 'red';
236
  }
237
+ })
238
+ .catch(error => {
239
+ console.error('Error applying coupon:', error);
240
+ couponMessage.innerText = 'Error applying coupon.';
241
+ couponMessage.style.color = 'red';
242
+ });
243
+ } else {
244
+ couponMessage.innerText = 'Please enter a coupon code.';
245
+ couponMessage.style.color = 'red';
246
+ }
247
  }
 
248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  function proceedToOrder() {
250
  fetch('/checkout', {
251
  method: 'POST',
 
261
  })
262
  .catch(err => console.error('Error during checkout:', err));
263
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  </script>
 
265
  </body>
266
+ </html>