Subbu1304 commited on
Commit
a27b796
·
verified ·
1 Parent(s): 61857d1

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +70 -11
templates/cart.html CHANGED
@@ -258,21 +258,75 @@
258
  // })
259
  // .catch(err => console.error("Error:", err));
260
  // }
261
- function updateQuantity(action, itemName, customerEmail) {
262
- let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
263
- let quantity = parseInt(quantityInput.value);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
  // Update quantity based on action
266
- if (action === 'increase') {
267
- quantity = Math.floor(quantity) + 1; // Ensure quantity remains an integer
268
- } else if (action === 'decrease' && Math.floor(quantity) > 1) {
269
- quantity = Math.floor(quantity) - 1; // Ensure quantity remains an integer
270
  }
271
 
272
  // Ensure quantity is a valid whole number (integer) and greater than 0
273
- if (isNaN(quantity) || quantity < 1 || quantity !== Math.floor(quantity)) {
274
- alert("Invalid quantity! Quantity should be a whole number.");
275
- return;
276
  }
277
 
278
  // Send updated quantity to the server
@@ -285,7 +339,11 @@
285
  .then(data => {
286
  if (data.success) {
287
  // Update the item price and quantity in the UI
288
- quantityInput.value = quantity;
 
 
 
 
289
  let itemElement = quantityInput.closest(".cart-item"); // Locate the parent cart item
290
  if (itemElement) {
291
  let basePriceElement = itemElement.querySelector(".base-price");
@@ -313,6 +371,7 @@
313
  .catch(err => console.error("Error:", err));
314
  }
315
 
 
316
  function toggleCouponDropdown() {
317
  let couponCheckbox = document.getElementById('couponCheckbox');
318
  let couponDropdown = document.getElementById('couponDropdown');
 
258
  // })
259
  // .catch(err => console.error("Error:", err));
260
  // }
261
+ // function updateQuantity(action, itemName, customerEmail) {
262
+ // let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
263
+ // let quantity = parseInt(quantityInput.value);
264
+
265
+ // // Update quantity based on action
266
+ // if (action === 'increase') {
267
+ // quantity = Math.floor(quantity) + 1; // Ensure quantity remains an integer
268
+ // } else if (action === 'decrease' && Math.floor(quantity) > 1) {
269
+ // quantity = Math.floor(quantity) - 1; // Ensure quantity remains an integer
270
+ // }
271
+
272
+ // // Ensure quantity is a valid whole number (integer) and greater than 0
273
+ // if (isNaN(quantity) || quantity < 1 || quantity !== Math.floor(quantity)) {
274
+ // alert("Invalid quantity! Quantity should be a whole number.");
275
+ // return;
276
+ // }
277
+
278
+ // // Send updated quantity to the server
279
+ // fetch('/cart/update_quantity', {
280
+ // method: 'POST',
281
+ // headers: { 'Content-Type': 'application/json' },
282
+ // body: JSON.stringify({ email: customerEmail, item_name: itemName, quantity: quantity })
283
+ // })
284
+ // .then(response => response.json())
285
+ // .then(data => {
286
+ // if (data.success) {
287
+ // // Update the item price and quantity in the UI
288
+ // quantityInput.value = quantity;
289
+ // let itemElement = quantityInput.closest(".cart-item"); // Locate the parent cart item
290
+ // if (itemElement) {
291
+ // let basePriceElement = itemElement.querySelector(".base-price");
292
+ // let addonsPriceElement = itemElement.querySelector(".addons-price");
293
+
294
+ // // Update the base price
295
+ // if (basePriceElement) {
296
+ // basePriceElement.innerText = data.new_item_price.toFixed(2); // Assuming backend sends this
297
+ // }
298
+
299
+ // // Update add-ons price if needed (optional)
300
+ // if (addonsPriceElement && data.addons_price !== undefined) {
301
+ // addonsPriceElement.innerText = data.addons_price.toFixed(2);
302
+ // }
303
+ // } else {
304
+ // console.error(`Parent cart item element not found for item: ${itemName}`);
305
+ // }
306
+
307
+ // // Optionally, reload the page to reflect updated values (can be avoided)
308
+ // location.reload();
309
+ // } else {
310
+ // alert("Error updating quantity: " + data.error);
311
+ // }
312
+ // })
313
+ // .catch(err => console.error("Error:", err));
314
+ // }
315
+ function updateQuantity(action, itemName, customerEmail) {
316
+ let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
317
+ let quantity = parseInt(quantityInput.value); // Get the value as an integer
318
 
319
  // Update quantity based on action
320
+ if (action === 'increase') {
321
+ quantity = Math.floor(quantity) + 1; // Ensure quantity remains an integer
322
+ } else if (action === 'decrease' && Math.floor(quantity) > 1) {
323
+ quantity = Math.floor(quantity) - 1; // Ensure quantity remains an integer
324
  }
325
 
326
  // Ensure quantity is a valid whole number (integer) and greater than 0
327
+ if (isNaN(quantity) || quantity < 1 || quantity !== Math.floor(quantity)) {
328
+ alert("Invalid quantity! Quantity should be a whole number.");
329
+ return;
330
  }
331
 
332
  // Send updated quantity to the server
 
339
  .then(data => {
340
  if (data.success) {
341
  // Update the item price and quantity in the UI
342
+ quantityInput.value = quantity; // Set the value as an integer
343
+
344
+ // Remove any decimals (if present) in the display
345
+ quantityInput.value = parseInt(quantityInput.value, 10);
346
+
347
  let itemElement = quantityInput.closest(".cart-item"); // Locate the parent cart item
348
  if (itemElement) {
349
  let basePriceElement = itemElement.querySelector(".base-price");
 
371
  .catch(err => console.error("Error:", err));
372
  }
373
 
374
+
375
  function toggleCouponDropdown() {
376
  let couponCheckbox = document.getElementById('couponCheckbox');
377
  let couponDropdown = document.getElementById('couponDropdown');