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

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +30 -5
templates/cart.html CHANGED
@@ -336,14 +336,14 @@ function updateQuantity(action, itemName, customerEmail) {
336
  // Update quantity based on action
337
  if (action === 'increase') {
338
  quantity += 1; // Simply increase the quantity by 1
339
- } else if (action === 'decrease' && quantity > 1) {
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
@@ -383,6 +383,31 @@ function updateQuantity(action, itemName, customerEmail) {
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}"]`);
 
336
  // Update quantity based on action
337
  if (action === 'increase') {
338
  quantity += 1; // Simply increase the quantity by 1
339
+ } else if (action === 'decrease' && quantity > 0) {
340
+ quantity -= 1; // Decrease the quantity by 1, ensuring it can reach 0
341
  }
342
 
343
+ // If quantity reaches 0, remove the item
344
  if (quantity === 0) {
345
+ removeItemFromCart(itemName);
346
+ return; // Don't continue with quantity update request if item is removed
347
  }
348
 
349
  // Update the input field with the new integer quantity
 
383
  .catch(err => console.error("Error:", err));
384
  }
385
 
386
+ function removeItemFromCart(itemName) {
387
+ // Find and remove the item element from the DOM
388
+ let itemElement = document.querySelector(`.cart-item[data-item-name="${itemName}"]`);
389
+ if (itemElement) {
390
+ itemElement.remove(); // Remove the item from the cart UI
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
  function removeItemFromCart(itemName) {
412
  // Hide the item or remove it from the cart UI
413
  let itemElement = document.querySelector(`.cart-item[data-item-name="${itemName}"]`);