Spaces:
Runtime error
Runtime error
Update templates/menu.html
Browse files- templates/menu.html +70 -83
templates/menu.html
CHANGED
|
@@ -1144,7 +1144,9 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
| 1144 |
});
|
| 1145 |
});
|
| 1146 |
|
| 1147 |
-
|
|
|
|
|
|
|
| 1148 |
// function addSoftDrinkToCart(name, price, image, section, category, index) {
|
| 1149 |
// const itemPrice = parseFloat(price);
|
| 1150 |
// const quantity = 1; // Default quantity
|
|
@@ -1173,7 +1175,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
| 1173 |
// // Hide the ADD button and show quantity controls
|
| 1174 |
// document.getElementById(`add-btn-${index}`).style.display = 'none';
|
| 1175 |
// document.getElementById(`quantity-control-${index}`).style.display = 'flex';
|
| 1176 |
-
//
|
| 1177 |
// alert('Soft Drink added to cart successfully!');
|
| 1178 |
// } else {
|
| 1179 |
// alert(data.error || 'Failed to add Soft Drink to cart.');
|
|
@@ -1184,7 +1186,8 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
| 1184 |
// alert('An error occurred while adding the Soft Drink to the cart.');
|
| 1185 |
// });
|
| 1186 |
// }
|
| 1187 |
-
|
|
|
|
| 1188 |
function addSoftDrinkToCart(name, price, image, section, category, index) {
|
| 1189 |
const itemPrice = parseFloat(price);
|
| 1190 |
const quantity = 1; // Default quantity
|
|
@@ -1200,6 +1203,7 @@ function addSoftDrinkToCart(name, price, image, section, category, index) {
|
|
| 1200 |
quantity: quantity
|
| 1201 |
};
|
| 1202 |
|
|
|
|
| 1203 |
fetch('/cart/add', {
|
| 1204 |
method: 'POST',
|
| 1205 |
headers: {
|
|
@@ -1213,7 +1217,7 @@ function addSoftDrinkToCart(name, price, image, section, category, index) {
|
|
| 1213 |
// Hide the ADD button and show quantity controls
|
| 1214 |
document.getElementById(`add-btn-${index}`).style.display = 'none';
|
| 1215 |
document.getElementById(`quantity-control-${index}`).style.display = 'flex';
|
| 1216 |
-
updateCartQuantity(data.cart);
|
| 1217 |
alert('Soft Drink added to cart successfully!');
|
| 1218 |
} else {
|
| 1219 |
alert(data.error || 'Failed to add Soft Drink to cart.');
|
|
@@ -1224,50 +1228,50 @@ function addSoftDrinkToCart(name, price, image, section, category, index) {
|
|
| 1224 |
alert('An error occurred while adding the Soft Drink to the cart.');
|
| 1225 |
});
|
| 1226 |
}
|
| 1227 |
-
|
| 1228 |
-
|
| 1229 |
// subbu###################
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
-
|
| 1235 |
// function increaseQuantity(name, index) {
|
| 1236 |
// let quantityElement = document.getElementById(`quantity-${index}`);
|
| 1237 |
// let currentQuantity = parseInt(quantityElement.innerText);
|
| 1238 |
-
// currentQuantity++;
|
| 1239 |
// quantityElement.innerText = currentQuantity;
|
| 1240 |
|
|
|
|
| 1241 |
// updateCartQuantity(name, currentQuantity);
|
| 1242 |
// }
|
| 1243 |
-
function increaseQuantity(name, index) {
|
| 1244 |
let quantityElement = document.getElementById(`quantity-${index}`);
|
| 1245 |
let currentQuantity = parseInt(quantityElement.innerText);
|
| 1246 |
currentQuantity++; // Increase the quantity by 1
|
| 1247 |
quantityElement.innerText = currentQuantity;
|
| 1248 |
|
| 1249 |
-
// Update cart immediately
|
| 1250 |
updateCartQuantity(name, currentQuantity);
|
| 1251 |
}
|
| 1252 |
|
| 1253 |
-
|
| 1254 |
|
| 1255 |
-
|
| 1256 |
// function decreaseQuantity(name, index) {
|
| 1257 |
// let quantityElement = document.getElementById(`quantity-${index}`);
|
| 1258 |
// let currentQuantity = parseInt(quantityElement.innerText);
|
|
|
|
| 1259 |
// if (currentQuantity > 1) {
|
| 1260 |
// currentQuantity--;
|
| 1261 |
// quantityElement.innerText = currentQuantity;
|
|
|
|
| 1262 |
// updateCartQuantity(name, currentQuantity);
|
| 1263 |
// } else {
|
| 1264 |
-
// // Remove from cart and
|
| 1265 |
// removeFromCart(name, index);
|
|
|
|
|
|
|
| 1266 |
// }
|
| 1267 |
// }
|
| 1268 |
-
|
| 1269 |
-
|
| 1270 |
-
function decreaseQuantity(name, index) {
|
| 1271 |
let quantityElement = document.getElementById(`quantity-${index}`);
|
| 1272 |
let currentQuantity = parseInt(quantityElement.innerText);
|
| 1273 |
|
|
@@ -1277,7 +1281,7 @@ function decreaseQuantity(name, index) {
|
|
| 1277 |
// Update cart immediately
|
| 1278 |
updateCartQuantity(name, currentQuantity);
|
| 1279 |
} else {
|
| 1280 |
-
// Remove from cart if quantity is 1
|
| 1281 |
removeFromCart(name, index);
|
| 1282 |
document.getElementById(`add-btn-${index}`).style.display = 'block'; // Show ADD button
|
| 1283 |
document.getElementById(`quantity-control-${index}`).style.display = 'none'; // Hide quantity controls
|
|
@@ -1286,85 +1290,68 @@ function decreaseQuantity(name, index) {
|
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
-
|
| 1290 |
-
|
| 1291 |
-
|
| 1292 |
-
// Existing updateCartUI function (unchanged, but included for reference)
|
| 1293 |
-
// function updateCartUI(cart) {
|
| 1294 |
// if (!Array.isArray(cart)) {
|
| 1295 |
// console.error('Invalid cart data:', cart);
|
| 1296 |
// return;
|
| 1297 |
// }
|
| 1298 |
// const cartIcon = document.getElementById('cart-icon');
|
| 1299 |
-
//
|
| 1300 |
-
// cartIcon.innerText = cart.length; // Update cart icon if it exists
|
| 1301 |
-
// }
|
| 1302 |
// }
|
| 1303 |
-
|
| 1304 |
-
|
| 1305 |
-
|
| 1306 |
-
|
| 1307 |
-
|
| 1308 |
-
|
| 1309 |
-
|
| 1310 |
-
|
| 1311 |
-
|
| 1312 |
-
|
| 1313 |
-
|
| 1314 |
-
|
| 1315 |
-
|
| 1316 |
-
|
| 1317 |
-
|
| 1318 |
-
|
| 1319 |
-
|
| 1320 |
-
|
| 1321 |
-
|
| 1322 |
-
|
| 1323 |
-
|
| 1324 |
-
|
| 1325 |
-
|
| 1326 |
-
//
|
| 1327 |
-
//
|
| 1328 |
-
|
| 1329 |
-
//
|
| 1330 |
-
|
| 1331 |
-
//
|
| 1332 |
-
//
|
| 1333 |
-
//
|
| 1334 |
-
|
| 1335 |
-
|
| 1336 |
-
//
|
| 1337 |
-
//
|
| 1338 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1339 |
// }
|
| 1340 |
-
|
| 1341 |
-
|
| 1342 |
-
// Function to update cart quantity
|
| 1343 |
-
function updateCartQuantity(cart) {
|
| 1344 |
if (!Array.isArray(cart)) {
|
| 1345 |
console.error('Invalid cart data:', cart);
|
| 1346 |
return;
|
| 1347 |
}
|
|
|
|
| 1348 |
const cartIcon = document.getElementById('cart-icon');
|
| 1349 |
-
cartIcon.innerText = cart.length;
|
| 1350 |
-
|
| 1351 |
-
|
| 1352 |
-
if (!Array.isArray(cart)) {
|
| 1353 |
-
console.error('Invalid cart data:', cart);
|
| 1354 |
-
return;
|
| 1355 |
-
}
|
| 1356 |
-
// Optional: Update quantity on the cart page
|
| 1357 |
const cartCountElement = document.getElementById('cart-count');
|
| 1358 |
-
cartCountElement.innerText = cart.reduce((total, item)=> total+item.quantity,0);
|
| 1359 |
-
|
| 1360 |
-
// Optionally, show a small success notification that the item was added
|
| 1361 |
-
const successNotification = document.createElement('div');
|
| 1362 |
-
successNotification.classList.add('success-notification');
|
| 1363 |
-
successNotification.innerText = 'Item added to cart!';
|
| 1364 |
-
document.body.appendChild(successNotification);
|
| 1365 |
-
setTimeout(() => {
|
| 1366 |
-
successNotification.remove(); // Remove success notification after a few seconds
|
| 1367 |
-
}, 2000);
|
| 1368 |
}
|
| 1369 |
|
| 1370 |
|
|
|
|
| 1144 |
});
|
| 1145 |
});
|
| 1146 |
|
| 1147 |
+
ft Drink added to cart successfully!');
|
| 1148 |
+
|
| 1149 |
+
// subbu###################
|
| 1150 |
// function addSoftDrinkToCart(name, price, image, section, category, index) {
|
| 1151 |
// const itemPrice = parseFloat(price);
|
| 1152 |
// const quantity = 1; // Default quantity
|
|
|
|
| 1175 |
// // Hide the ADD button and show quantity controls
|
| 1176 |
// document.getElementById(`add-btn-${index}`).style.display = 'none';
|
| 1177 |
// document.getElementById(`quantity-control-${index}`).style.display = 'flex';
|
| 1178 |
+
// updateCartQuantity(data.cart);
|
| 1179 |
// alert('Soft Drink added to cart successfully!');
|
| 1180 |
// } else {
|
| 1181 |
// alert(data.error || 'Failed to add Soft Drink to cart.');
|
|
|
|
| 1186 |
// alert('An error occurred while adding the Soft Drink to the cart.');
|
| 1187 |
// });
|
| 1188 |
// }
|
| 1189 |
+
|
| 1190 |
+
// Add Soft Drink to Cart with a default quantity of 1
|
| 1191 |
function addSoftDrinkToCart(name, price, image, section, category, index) {
|
| 1192 |
const itemPrice = parseFloat(price);
|
| 1193 |
const quantity = 1; // Default quantity
|
|
|
|
| 1203 |
quantity: quantity
|
| 1204 |
};
|
| 1205 |
|
| 1206 |
+
// Add item to cart for the first time
|
| 1207 |
fetch('/cart/add', {
|
| 1208 |
method: 'POST',
|
| 1209 |
headers: {
|
|
|
|
| 1217 |
// Hide the ADD button and show quantity controls
|
| 1218 |
document.getElementById(`add-btn-${index}`).style.display = 'none';
|
| 1219 |
document.getElementById(`quantity-control-${index}`).style.display = 'flex';
|
| 1220 |
+
updateCartQuantity(data.cart); // Update cart display
|
| 1221 |
alert('Soft Drink added to cart successfully!');
|
| 1222 |
} else {
|
| 1223 |
alert(data.error || 'Failed to add Soft Drink to cart.');
|
|
|
|
| 1228 |
alert('An error occurred while adding the Soft Drink to the cart.');
|
| 1229 |
});
|
| 1230 |
}
|
|
|
|
|
|
|
| 1231 |
// subbu###################
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
+
|
| 1237 |
// function increaseQuantity(name, index) {
|
| 1238 |
// let quantityElement = document.getElementById(`quantity-${index}`);
|
| 1239 |
// let currentQuantity = parseInt(quantityElement.innerText);
|
| 1240 |
+
// currentQuantity++; // Increase the quantity by 1
|
| 1241 |
// quantityElement.innerText = currentQuantity;
|
| 1242 |
|
| 1243 |
+
// // Update cart immediately
|
| 1244 |
// updateCartQuantity(name, currentQuantity);
|
| 1245 |
// }
|
| 1246 |
+
function increaseQuantity(name, index) {
|
| 1247 |
let quantityElement = document.getElementById(`quantity-${index}`);
|
| 1248 |
let currentQuantity = parseInt(quantityElement.innerText);
|
| 1249 |
currentQuantity++; // Increase the quantity by 1
|
| 1250 |
quantityElement.innerText = currentQuantity;
|
| 1251 |
|
| 1252 |
+
// Update cart immediately when quantity is increased
|
| 1253 |
updateCartQuantity(name, currentQuantity);
|
| 1254 |
}
|
| 1255 |
|
|
|
|
| 1256 |
|
| 1257 |
+
|
| 1258 |
// function decreaseQuantity(name, index) {
|
| 1259 |
// let quantityElement = document.getElementById(`quantity-${index}`);
|
| 1260 |
// let currentQuantity = parseInt(quantityElement.innerText);
|
| 1261 |
+
|
| 1262 |
// if (currentQuantity > 1) {
|
| 1263 |
// currentQuantity--;
|
| 1264 |
// quantityElement.innerText = currentQuantity;
|
| 1265 |
+
// // Update cart immediately
|
| 1266 |
// updateCartQuantity(name, currentQuantity);
|
| 1267 |
// } else {
|
| 1268 |
+
// // Remove from cart if quantity is 1 and show the "Add" button
|
| 1269 |
// removeFromCart(name, index);
|
| 1270 |
+
// document.getElementById(`add-btn-${index}`).style.display = 'block'; // Show ADD button
|
| 1271 |
+
// document.getElementById(`quantity-control-${index}`).style.display = 'none'; // Hide quantity controls
|
| 1272 |
// }
|
| 1273 |
// }
|
| 1274 |
+
function decreaseQuantity(name, index) {
|
|
|
|
|
|
|
| 1275 |
let quantityElement = document.getElementById(`quantity-${index}`);
|
| 1276 |
let currentQuantity = parseInt(quantityElement.innerText);
|
| 1277 |
|
|
|
|
| 1281 |
// Update cart immediately
|
| 1282 |
updateCartQuantity(name, currentQuantity);
|
| 1283 |
} else {
|
| 1284 |
+
// Remove from cart if quantity is 1
|
| 1285 |
removeFromCart(name, index);
|
| 1286 |
document.getElementById(`add-btn-${index}`).style.display = 'block'; // Show ADD button
|
| 1287 |
document.getElementById(`quantity-control-${index}`).style.display = 'none'; // Hide quantity controls
|
|
|
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
+
// Function to update cart quantity
|
| 1294 |
+
// function updateCartQuantity(cart) {
|
|
|
|
|
|
|
|
|
|
| 1295 |
// if (!Array.isArray(cart)) {
|
| 1296 |
// console.error('Invalid cart data:', cart);
|
| 1297 |
// return;
|
| 1298 |
// }
|
| 1299 |
// const cartIcon = document.getElementById('cart-icon');
|
| 1300 |
+
// cartIcon.innerText = cart.length; // Assuming cart is an array of items
|
|
|
|
|
|
|
| 1301 |
// }
|
| 1302 |
+
function updateCartQuantity(name, newQuantity) {
|
| 1303 |
+
const customerEmail = "user@example.com"; // Get the logged-in user's email
|
| 1304 |
+
fetch(`/cart/update_quantity`, {
|
| 1305 |
+
method: 'POST',
|
| 1306 |
+
headers: { 'Content-Type': 'application/json' },
|
| 1307 |
+
body: JSON.stringify({
|
| 1308 |
+
email: customerEmail,
|
| 1309 |
+
item_name: name.trim(),
|
| 1310 |
+
quantity: newQuantity
|
| 1311 |
+
})
|
| 1312 |
+
})
|
| 1313 |
+
.then(response => response.json())
|
| 1314 |
+
.then(data => {
|
| 1315 |
+
if (data.success) {
|
| 1316 |
+
alert("Item quantity updated successfully.");
|
| 1317 |
+
updateCartDisplay(data.cart); // Update cart UI
|
| 1318 |
+
} else {
|
| 1319 |
+
alert(data.error || "Failed to update item quantity.");
|
| 1320 |
+
}
|
| 1321 |
+
})
|
| 1322 |
+
.catch(err => console.error("Error updating item quantity:", err));
|
| 1323 |
+
}
|
| 1324 |
+
|
| 1325 |
+
// function updateCartDisplay(cart) {
|
| 1326 |
+
// if (!Array.isArray(cart)) {
|
| 1327 |
+
// console.error('Invalid cart data:', cart);
|
| 1328 |
+
// return;
|
| 1329 |
+
// }
|
| 1330 |
+
// // Optional: Update quantity on the cart page
|
| 1331 |
+
// const cartCountElement = document.getElementById('cart-count');
|
| 1332 |
+
// cartCountElement.innerText = cart.reduce((total, item)=> total+item.quantity,0); // Update cart item count //Sum of all quantities
|
| 1333 |
+
|
| 1334 |
+
// // Optionally, show a small success notification that the item was added
|
| 1335 |
+
// const successNotification = document.createElement('div');
|
| 1336 |
+
// successNotification.classList.add('success-notification');
|
| 1337 |
+
// successNotification.innerText = 'Item added to cart!';
|
| 1338 |
+
// document.body.appendChild(successNotification);
|
| 1339 |
+
// setTimeout(() => {
|
| 1340 |
+
// successNotification.remove(); // Remove success notification after a few seconds
|
| 1341 |
+
// }, 2000);
|
| 1342 |
// }
|
| 1343 |
+
function updateCartDisplay(cart) {
|
|
|
|
|
|
|
|
|
|
| 1344 |
if (!Array.isArray(cart)) {
|
| 1345 |
console.error('Invalid cart data:', cart);
|
| 1346 |
return;
|
| 1347 |
}
|
| 1348 |
+
|
| 1349 |
const cartIcon = document.getElementById('cart-icon');
|
| 1350 |
+
cartIcon.innerText = cart.length; // Assuming cart is an array of items
|
| 1351 |
+
|
| 1352 |
+
// Optional: Update quantity on the cart page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1353 |
const cartCountElement = document.getElementById('cart-count');
|
| 1354 |
+
cartCountElement.innerText = cart.reduce((total, item) => total + item.quantity, 0); // Update cart item count (sum of all quantities)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1355 |
}
|
| 1356 |
|
| 1357 |
|