Update templates/cart.html
Browse files- templates/cart.html +59 -66
templates/cart.html
CHANGED
@@ -480,60 +480,54 @@
|
|
480 |
|
481 |
<script>
|
482 |
// Example function to handle the increase/decrease button clicks
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
// Validate quantity
|
494 |
-
if (isNaN(quantity) || quantity < 1) {
|
495 |
-
alert("Invalid quantity!");
|
496 |
-
return;
|
497 |
-
}
|
498 |
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
})
|
505 |
-
.then(response => response.json())
|
506 |
-
.then(data => {
|
507 |
-
if (data.success) {
|
508 |
-
// Update the item price and quantity in the UI
|
509 |
-
quantityInput.value = quantity;
|
510 |
-
let itemElement = quantityInput.closest(".cart-item"); // Locate the parent cart item
|
511 |
-
if (itemElement) {
|
512 |
-
let basePriceElement = itemElement.querySelector(".base-price");
|
513 |
-
let addonsPriceElement = itemElement.querySelector(".addons-price");
|
514 |
-
|
515 |
-
// Update the base price
|
516 |
-
if (basePriceElement) {
|
517 |
-
basePriceElement.innerText = data.new_item_price.toFixed(2); // Assuming backend sends this
|
518 |
-
}
|
519 |
-
|
520 |
-
// Update add-ons price if needed (optional)
|
521 |
-
if (addonsPriceElement && data.addons_price !== undefined) {
|
522 |
-
addonsPriceElement.innerText = data.addons_price.toFixed(2);
|
523 |
-
}
|
524 |
-
} else {
|
525 |
-
console.error(`Parent cart item element not found for item: ${itemName}`);
|
526 |
-
}
|
527 |
-
location.reload();
|
528 |
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
533 |
}
|
534 |
-
|
535 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
536 |
}
|
|
|
|
|
|
|
|
|
537 |
function toggleCouponDropdown() {
|
538 |
let couponDropdown = document.getElementById('couponDropdown');
|
539 |
|
@@ -642,23 +636,22 @@
|
|
642 |
.catch(err => console.error('Error adding suggestion:', err));
|
643 |
}
|
644 |
function removeItemFromCart(itemName) {
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
} else {
|
657 |
-
alert(data.message);
|
658 |
-
}
|
659 |
-
})
|
660 |
-
.catch(err => console.error('Error removing item:', err));
|
661 |
}
|
|
|
|
|
|
|
|
|
662 |
function addToCart(itemName, customerEmail) {
|
663 |
fetch(`/cart/add_item`, {
|
664 |
method: "POST",
|
|
|
480 |
|
481 |
<script>
|
482 |
// Example function to handle the increase/decrease button clicks
|
483 |
+
function updateQuantity(action, itemName, customerEmail) {
|
484 |
+
let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
|
485 |
+
let quantity = parseInt(quantityInput.value);
|
486 |
+
|
487 |
+
// Update quantity based on action
|
488 |
+
if (action === 'increase') {
|
489 |
+
quantity++;
|
490 |
+
} else if (action === 'decrease' && quantity > 1) {
|
491 |
+
quantity--;
|
492 |
+
}
|
|
|
|
|
|
|
|
|
|
|
493 |
|
494 |
+
// Validate quantity
|
495 |
+
if (isNaN(quantity) || quantity < 1) {
|
496 |
+
alert("Invalid quantity!");
|
497 |
+
return;
|
498 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
499 |
|
500 |
+
// Send updated quantity to the server
|
501 |
+
fetch('/cart/update_quantity', { // Correct URL (prefixed by '/cart')
|
502 |
+
method: 'POST',
|
503 |
+
headers: { 'Content-Type': 'application/json' },
|
504 |
+
body: JSON.stringify({ email: customerEmail, item_name: itemName, quantity: quantity })
|
505 |
+
})
|
506 |
+
.then(response => response.json())
|
507 |
+
.then(data => {
|
508 |
+
if (data.success) {
|
509 |
+
quantityInput.value = quantity;
|
510 |
+
let itemElement = quantityInput.closest(".cart-item"); // Locate the parent cart item
|
511 |
+
if (itemElement) {
|
512 |
+
let basePriceElement = itemElement.querySelector(".base-price");
|
513 |
+
let addonsPriceElement = itemElement.querySelector(".addons-price");
|
514 |
+
// Update the base price
|
515 |
+
if (basePriceElement) {
|
516 |
+
basePriceElement.innerText = data.new_item_price.toFixed(2);
|
517 |
}
|
518 |
+
// Update add-ons price if needed (optional)
|
519 |
+
if (addonsPriceElement && data.addons_price !== undefined) {
|
520 |
+
addonsPriceElement.innerText = data.addons_price.toFixed(2);
|
521 |
+
}
|
522 |
+
}
|
523 |
+
location.reload(); // Recalculate subtotal dynamically
|
524 |
+
} else {
|
525 |
+
alert("Error updating quantity: " + data.error);
|
526 |
}
|
527 |
+
})
|
528 |
+
.catch(err => console.error("Error:", err));
|
529 |
+
}
|
530 |
+
|
531 |
function toggleCouponDropdown() {
|
532 |
let couponDropdown = document.getElementById('couponDropdown');
|
533 |
|
|
|
636 |
.catch(err => console.error('Error adding suggestion:', err));
|
637 |
}
|
638 |
function removeItemFromCart(itemName) {
|
639 |
+
fetch(`/cart/remove/${encodeURIComponent(itemName)}`, { // Correct URL (prefixed by '/cart')
|
640 |
+
method: 'POST',
|
641 |
+
headers: { 'Content-Type': 'application/json' }
|
642 |
+
})
|
643 |
+
.then(response => response.json())
|
644 |
+
.then(data => {
|
645 |
+
if (data.success) {
|
646 |
+
alert(data.message);
|
647 |
+
location.reload(); // Reload the page to update the cart
|
648 |
+
} else {
|
649 |
+
alert(data.message);
|
|
|
|
|
|
|
|
|
|
|
650 |
}
|
651 |
+
})
|
652 |
+
.catch(err => console.error('Error removing item:', err));
|
653 |
+
}
|
654 |
+
|
655 |
function addToCart(itemName, customerEmail) {
|
656 |
fetch(`/cart/add_item`, {
|
657 |
method: "POST",
|