Subbu1304 commited on
Commit
8f3f1b7
·
verified ·
1 Parent(s): e04e176

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +84 -1
templates/cart.html CHANGED
@@ -270,7 +270,60 @@
270
  // .catch(err => console.error("Error:", err));
271
  // }
272
 
273
- function updateQuantity(action, itemName, customerEmail) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
  let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
275
  let quantity = parseInt(quantityInput.value, 10); // Ensure we are working with an integer value
276
 
@@ -287,6 +340,12 @@
287
  quantity -= 1; // Decrease the quantity by 1, ensuring it stays at least 1
288
  }
289
 
 
 
 
 
 
 
290
  // Update the input field with the new integer quantity
291
  quantityInput.value = quantity;
292
 
@@ -324,6 +383,30 @@
324
  .catch(err => console.error("Error:", err));
325
  }
326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
 
328
 
329
  function toggleCouponDropdown() {
 
270
  // .catch(err => console.error("Error:", err));
271
  // }
272
 
273
+ // function updateQuantity(action, itemName, customerEmail) {
274
+ // let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
275
+ // let quantity = parseInt(quantityInput.value, 10); // Ensure we are working with an integer value
276
+
277
+ // // Ensure that the parsed quantity is a valid integer
278
+ // if (isNaN(quantity) || quantity < 1) {
279
+ // alert("Invalid quantity! Quantity should be a whole number.");
280
+ // return;
281
+ // }
282
+
283
+ // // Update quantity based on action
284
+ // if (action === 'increase') {
285
+ // quantity += 1; // Simply increase the quantity by 1
286
+ // } else if (action === 'decrease' && quantity > 1) {
287
+ // quantity -= 1; // Decrease the quantity by 1, ensuring it stays at least 1
288
+ // }
289
+
290
+ // // Update the input field with the new integer quantity
291
+ // quantityInput.value = quantity;
292
+
293
+ // // Send updated quantity to the server
294
+ // fetch('/cart/update_quantity', {
295
+ // method: 'POST',
296
+ // headers: { 'Content-Type': 'application/json' },
297
+ // body: JSON.stringify({ email: customerEmail, item_name: itemName, quantity: quantity })
298
+ // })
299
+ // .then(response => response.json())
300
+ // .then(data => {
301
+ // if (data.success) {
302
+ // // Update the base price based on the new quantity
303
+ // let itemElement = quantityInput.closest(".cart-item"); // Locate the parent cart item
304
+ // if (itemElement) {
305
+ // let basePriceElement = itemElement.querySelector(".base-price");
306
+ // let addonsPriceElement = itemElement.querySelector(".addons-price");
307
+
308
+ // // Update the base price
309
+ // if (basePriceElement) {
310
+ // basePriceElement.innerText = data.new_item_price.toFixed(2); // Assuming backend returns price in two decimal format
311
+ // }
312
+
313
+ // // Update add-ons price if needed (optional)
314
+ // if (addonsPriceElement && data.addons_price !== undefined) {
315
+ // addonsPriceElement.innerText = data.addons_price.toFixed(2);
316
+ // }
317
+ // } else {
318
+ // console.error(`Parent cart item element not found for item: ${itemName}`);
319
+ // }
320
+ // } else {
321
+ // alert("Error updating quantity: " + data.error);
322
+ // }
323
+ // })
324
+ // .catch(err => console.error("Error:", err));
325
+ // }
326
+ function updateQuantity(action, itemName, customerEmail) {
327
  let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
328
  let quantity = parseInt(quantityInput.value, 10); // Ensure we are working with an integer value
329
 
 
340
  quantity -= 1; // Decrease the quantity by 1, ensuring it stays at least 1
341
  }
342
 
343
+ // Check if quantity is zero
344
+ if (quantity === 0) {
345
+ removeItemFromCart(itemName); // Call the remove function if quantity is 0
346
+ return; // Don't proceed with updating quantity or sending request
347
+ }
348
+
349
  // Update the input field with the new integer quantity
350
  quantityInput.value = quantity;
351
 
 
383
  .catch(err => console.error("Error:", err));
384
  }
385
 
386
+ function removeItemFromCart(itemName) {
387
+ // Hide the item or remove it from the cart UI
388
+ let itemElement = document.querySelector(`.cart-item[data-item-name="${itemName}"]`);
389
+ if (itemElement) {
390
+ itemElement.remove(); // Remove the item element from the DOM
391
+ }
392
+
393
+ // Optionally, also send a request to the server to remove the item
394
+ fetch('/cart/remove_item', {
395
+ method: 'POST',
396
+ headers: { 'Content-Type': 'application/json' },
397
+ body: JSON.stringify({ item_name: itemName })
398
+ })
399
+ .then(response => response.json())
400
+ .then(data => {
401
+ if (data.success) {
402
+ console.log("Item removed from cart successfully");
403
+ } else {
404
+ alert("Error removing item from cart: " + data.error);
405
+ }
406
+ })
407
+ .catch(err => console.error("Error:", err));
408
+ }
409
+
410
 
411
 
412
  function toggleCouponDropdown() {