Update templates/menu.html
Browse files- templates/menu.html +46 -28
templates/menu.html
CHANGED
@@ -898,41 +898,59 @@ if (!localStorage.getItem('cart')) {
|
|
898 |
localStorage.setItem('cart', JSON.stringify([]));
|
899 |
}
|
900 |
|
901 |
-
|
902 |
-
|
903 |
-
|
904 |
-
let
|
905 |
-
|
906 |
-
// Create the item object to add to the cart
|
907 |
-
let item = {
|
908 |
-
name: itemName,
|
909 |
-
price: itemPrice,
|
910 |
-
image: itemImage,
|
911 |
-
quantity: 1 // Initial quantity is 1, you can adjust if needed
|
912 |
-
};
|
913 |
|
914 |
-
//
|
915 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
916 |
|
917 |
-
if (
|
918 |
-
|
919 |
-
|
920 |
-
} else {
|
921 |
-
// If the item doesn't exist, add it to the cart
|
922 |
-
cart.push(item);
|
923 |
}
|
924 |
|
925 |
-
//
|
926 |
-
|
|
|
|
|
|
|
|
|
|
|
927 |
|
928 |
-
//
|
929 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
930 |
}
|
931 |
|
932 |
-
// Function to get the cart (optional for showing cart info)
|
933 |
-
function getCart() {
|
934 |
-
return JSON.parse(localStorage.getItem('cart'));
|
935 |
-
}
|
936 |
|
937 |
|
938 |
|
|
|
898 |
localStorage.setItem('cart', JSON.stringify([]));
|
899 |
}
|
900 |
|
901 |
+
function addToCartDirectly() {
|
902 |
+
// Retrieve item details directly from the modal
|
903 |
+
const itemName = document.getElementById('modal-name').innerText;
|
904 |
+
let itemPrice = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
905 |
|
906 |
+
// Validate item price
|
907 |
+
if (isNaN(itemPrice)) {
|
908 |
+
alert('Invalid price for the item. Please check the item details.');
|
909 |
+
return;
|
910 |
+
}
|
911 |
+
|
912 |
+
const itemImage = document.getElementById('modal-img').src;
|
913 |
+
const quantity = parseInt(document.getElementById('quantityInput').value) || 1; // Default to 1 if invalid
|
914 |
|
915 |
+
if (!itemName || !itemPrice || !itemImage) {
|
916 |
+
console.error('Missing data for cart item:', { itemName, itemPrice, itemImage});
|
917 |
+
return;
|
|
|
|
|
|
|
918 |
}
|
919 |
|
920 |
+
// Prepare the cart data (no add-ons or section)
|
921 |
+
const cartPayload = {
|
922 |
+
itemName: itemName,
|
923 |
+
itemPrice: itemPrice,
|
924 |
+
itemImage: itemImage,
|
925 |
+
quantity: quantity // Include quantity
|
926 |
+
};
|
927 |
|
928 |
+
// Send the cart data to the server
|
929 |
+
fetch('/cart/add', {
|
930 |
+
method: 'POST',
|
931 |
+
headers: {
|
932 |
+
'Content-Type': 'application/json',
|
933 |
+
},
|
934 |
+
body: JSON.stringify(cartPayload)
|
935 |
+
})
|
936 |
+
.then(response => response.json())
|
937 |
+
.then(data => {
|
938 |
+
if (data.success) {
|
939 |
+
alert('Item added to cart successfully!');
|
940 |
+
updateCartUI(data.cart); // Update cart UI after adding an item
|
941 |
+
const modal = document.getElementById('itemModal');
|
942 |
+
const modalInstance = bootstrap.Modal.getInstance(modal);
|
943 |
+
modalInstance.hide();
|
944 |
+
} else {
|
945 |
+
alert(data.error || 'Failed to add item to cart.');
|
946 |
+
}
|
947 |
+
})
|
948 |
+
.catch(err => {
|
949 |
+
console.error('Error adding item to cart:', err);
|
950 |
+
alert('An error occurred while adding the item to the cart.');
|
951 |
+
});
|
952 |
}
|
953 |
|
|
|
|
|
|
|
|
|
954 |
|
955 |
|
956 |
|