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

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +59 -54
templates/cart.html CHANGED
@@ -258,60 +258,65 @@
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
 
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, 10); // Get the value as an integer
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' && 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) {
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; // Set the value as an integer (no decimals)
289
+
290
+ // Make sure the value is displayed as an integer (no decimals)
291
+ quantityInput.value = parseInt(quantityInput.value, 10);
292
+
293
+ let itemElement = quantityInput.closest(".cart-item"); // Locate the parent cart item
294
+ if (itemElement) {
295
+ let basePriceElement = itemElement.querySelector(".base-price");
296
+ let addonsPriceElement = itemElement.querySelector(".addons-price");
297
+
298
+ // Update the base price
299
+ if (basePriceElement) {
300
+ basePriceElement.innerText = data.new_item_price.toFixed(2); // Assuming backend sends this
301
+ }
302
+
303
+ // Update add-ons price if needed (optional)
304
+ if (addonsPriceElement && data.addons_price !== undefined) {
305
+ addonsPriceElement.innerText = data.addons_price.toFixed(2);
306
+ }
307
+ } else {
308
+ console.error(`Parent cart item element not found for item: ${itemName}`);
309
+ }
310
+
311
+ // Optionally, reload the page to reflect updated values (can be avoided)
312
+ location.reload();
313
+ } else {
314
+ alert("Error updating quantity: " + data.error);
315
+ }
316
+ })
317
+ .catch(err => console.error("Error:", err));
318
+ }
319
+
320
  function updateQuantity(action, itemName, customerEmail) {
321
  let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
322
  let quantity = parseInt(quantityInput.value); // Get the value as an integer