Spaces:
Sleeping
Sleeping
Update templates/cart.html
Browse files- templates/cart.html +59 -54
templates/cart.html
CHANGED
@@ -258,60 +258,65 @@
|
|
258 |
// })
|
259 |
// .catch(err => console.error("Error:", err));
|
260 |
// }
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
//
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
//
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
//
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
//
|
288 |
-
|
289 |
-
|
290 |
-
//
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
//
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
//
|
312 |
-
|
313 |
-
|
314 |
-
|
|
|
|
|
|
|
|
|
|
|
315 |
function updateQuantity(action, itemName, customerEmail) {
|
316 |
let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
|
317 |
let quantity = parseInt(quantityInput.value); // Get the value as an integer
|
|
|
258 |
// })
|
259 |
// .catch(err => console.error("Error:", err));
|
260 |
// }
|
261 |
+
function updateQuantity(action, itemName, customerEmail) {
|
262 |
+
let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
|
263 |
+
let quantity = parseInt(quantityInput.value, 10); // Get the value as an integer
|
264 |
+
|
265 |
+
// Update quantity based on action
|
266 |
+
if (action === 'increase') {
|
267 |
+
quantity = Math.floor(quantity) + 1; // Ensure quantity remains an integer
|
268 |
+
} else if (action === 'decrease' && quantity > 1) {
|
269 |
+
quantity = Math.floor(quantity) - 1; // Ensure quantity remains an integer
|
270 |
+
}
|
271 |
+
|
272 |
+
// Ensure quantity is a valid whole number (integer) and greater than 0
|
273 |
+
if (isNaN(quantity) || quantity < 1) {
|
274 |
+
alert("Invalid quantity! Quantity should be a whole number.");
|
275 |
+
return;
|
276 |
+
}
|
277 |
+
|
278 |
+
// Send updated quantity to the server
|
279 |
+
fetch('/cart/update_quantity', {
|
280 |
+
method: 'POST',
|
281 |
+
headers: { 'Content-Type': 'application/json' },
|
282 |
+
body: JSON.stringify({ email: customerEmail, item_name: itemName, quantity: quantity })
|
283 |
+
})
|
284 |
+
.then(response => response.json())
|
285 |
+
.then(data => {
|
286 |
+
if (data.success) {
|
287 |
+
// Update the item price and quantity in the UI
|
288 |
+
quantityInput.value = quantity; // Set the value as an integer (no decimals)
|
289 |
+
|
290 |
+
// Make sure the value is displayed as an integer (no decimals)
|
291 |
+
quantityInput.value = parseInt(quantityInput.value, 10);
|
292 |
+
|
293 |
+
let itemElement = quantityInput.closest(".cart-item"); // Locate the parent cart item
|
294 |
+
if (itemElement) {
|
295 |
+
let basePriceElement = itemElement.querySelector(".base-price");
|
296 |
+
let addonsPriceElement = itemElement.querySelector(".addons-price");
|
297 |
+
|
298 |
+
// Update the base price
|
299 |
+
if (basePriceElement) {
|
300 |
+
basePriceElement.innerText = data.new_item_price.toFixed(2); // Assuming backend sends this
|
301 |
+
}
|
302 |
+
|
303 |
+
// Update add-ons price if needed (optional)
|
304 |
+
if (addonsPriceElement && data.addons_price !== undefined) {
|
305 |
+
addonsPriceElement.innerText = data.addons_price.toFixed(2);
|
306 |
+
}
|
307 |
+
} else {
|
308 |
+
console.error(`Parent cart item element not found for item: ${itemName}`);
|
309 |
+
}
|
310 |
+
|
311 |
+
// Optionally, reload the page to reflect updated values (can be avoided)
|
312 |
+
location.reload();
|
313 |
+
} else {
|
314 |
+
alert("Error updating quantity: " + data.error);
|
315 |
+
}
|
316 |
+
})
|
317 |
+
.catch(err => console.error("Error:", err));
|
318 |
+
}
|
319 |
+
|
320 |
function updateQuantity(action, itemName, customerEmail) {
|
321 |
let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
|
322 |
let quantity = parseInt(quantityInput.value); // Get the value as an integer
|