Spaces:
Sleeping
Sleeping
Update templates/cart.html
Browse files- templates/cart.html +37 -25
templates/cart.html
CHANGED
@@ -185,31 +185,43 @@
|
|
185 |
|
186 |
// Update quantity
|
187 |
function updateQuantity(action, itemName) {
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
|
214 |
// Remove item
|
215 |
function removeItemFromCart(itemName) {
|
|
|
185 |
|
186 |
// Update quantity
|
187 |
function updateQuantity(action, itemName) {
|
188 |
+
const quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
|
189 |
+
const priceElement = document.querySelector(`.base-price[data-item-name="${itemName}"]`);
|
190 |
+
const subtotalElement = document.getElementById("subtotal-text");
|
191 |
+
|
192 |
+
let quantity = parseInt(quantityInput.value);
|
193 |
+
|
194 |
+
if (action === 'increase') {
|
195 |
+
quantity++;
|
196 |
+
} else if (action === 'decrease' && quantity > 1) {
|
197 |
+
quantity--;
|
198 |
+
}
|
199 |
+
|
200 |
+
fetch('/cart/update_quantity', {
|
201 |
+
method: 'POST',
|
202 |
+
headers: { 'Content-Type': 'application/json' },
|
203 |
+
body: JSON.stringify({
|
204 |
+
item_name: itemName,
|
205 |
+
quantity: quantity
|
206 |
+
})
|
207 |
+
})
|
208 |
+
.then(response => response.json())
|
209 |
+
.then(data => {
|
210 |
+
if (data.success) {
|
211 |
+
// Update quantity in UI without reloading
|
212 |
+
quantityInput.value = quantity;
|
213 |
+
|
214 |
+
// Update price in UI
|
215 |
+
priceElement.innerText = `$${data.new_item_price.toFixed(2)}`;
|
216 |
+
|
217 |
+
// Update subtotal dynamically
|
218 |
+
subtotalElement.innerText = `Subtotal: $${data.subtotal.toFixed(2)}`;
|
219 |
+
} else {
|
220 |
+
alert('Error updating quantity: ' + data.error);
|
221 |
+
}
|
222 |
+
})
|
223 |
+
.catch(err => console.error('Error:', err));
|
224 |
+
}
|
225 |
|
226 |
// Remove item
|
227 |
function removeItemFromCart(itemName) {
|