Spaces:
Sleeping
Sleeping
File size: 4,780 Bytes
56256a7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | function updateCartDisplay() {
let totalBill = 0;
let cartHTML = "<h3>Your Cart:</h3><ul style='list-style: none; padding: 0;'>";
cart.forEach((item, index) => {
totalBill += item.itemTotal;
const extras = item.extras.map((extra, i) => {
const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
const extraTotal = extrasPrices[extra] * extraQuantity;
totalBill += extraTotal;
return `<li>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<span>${extra}</span>
<span>Price: $${extrasPrices[extra].toFixed(2)}</span>
<div>
<label for='extra-quantity-${index}-${i}'>Quantity:</label>
<input type='number' id='extra-quantity-${index}-${i}' value='${extraQuantity}' min='1' style='width: 50px;' onchange='updateExtraQuantity(${index}, ${i}, this.value)'>
</div>
<span>Total: $${extraTotal.toFixed(2)}</span>
<input type='checkbox' id='extra-remove-${index}-${i}' onclick='removeExtra(${index}, ${i})'> Remove
</div>
</li>`;
}).join('');
cartHTML += `<li style='border-bottom: 1px solid #ddd; margin-bottom: 10px;'>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<span>${item.name}</span>
<span>Item Price: $${item.price.toFixed(2)}</span>
<div>
<label for='item-quantity-${index}'>Quantity:</label>
<input type='number' id='item-quantity-${index}' value='${item.quantity}' min='1' style='width: 50px;' onchange='updateItemQuantity(${index}, this.value)'>
</div>
<span>Total: $${(item.price * item.quantity).toFixed(2)}</span>
<input type='checkbox' id='item-remove-${index}' onclick='removeItem(${index})'> Remove
</div>
<div style='margin-left: 20px;'>
<h4>Extras:</h4>
<ul style='list-style: none; padding: 0;'>${extras}</ul>
</div>
<div style='margin-left: 20px;'>
<strong>Instructions:</strong> ${item.instructions || "None"}
</div>
</li>`;
});
cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
cartHTML += `<button style='margin-top: 10px; background-color: #007bff; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer;' onclick='submitCart()'>Submit</button>`;
document.getElementById('floating-cart').innerHTML = cartHTML;
}
function updateItemQuantity(index, newQuantity) {
const quantity = parseInt(newQuantity) || 1;
cart[index].quantity = quantity;
cart[index].itemTotal = cart[index].price * quantity;
updateCartDisplay();
}
function updateExtraQuantity(cartIndex, extraIndex, newQuantity) {
const quantity = parseInt(newQuantity) || 1;
cart[cartIndex].extrasQuantities = cart[cartIndex].extrasQuantities || [];
cart[cartIndex].extrasQuantities[extraIndex] = quantity;
updateCartDisplay();
}
function removeExtra(cartIndex, extraIndex) {
cart[cartIndex].extras.splice(extraIndex, 1);
if (cart[cartIndex].extrasQuantities) {
cart[cartIndex].extrasQuantities.splice(extraIndex, 1);
}
updateCartDisplay();
}
function submitCart() {
let finalOrderHTML = "<h3>Final Order:</h3><ul>";
let totalBill = 0;
cart.forEach(item => {
totalBill += item.itemTotal;
const extras = item.extras.map((extra, i) => {
const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
const extraTotal = extrasPrices[extra] * extraQuantity;
totalBill += extraTotal;
return `${extra} (x${extraQuantity}) - $${extraTotal.toFixed(2)}`;
}).join(', ');
finalOrderHTML += `<li>
${item.name} (x${item.quantity}) - $${item.itemTotal.toFixed(2)}
<br>Extras: ${extras}
<br>Instructions: ${item.instructions || "None"}
</li>`;
});
finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
document.getElementById('final-order').innerHTML = finalOrderHTML;
alert("Your final order has been submitted!");
}
|