Rammohan0504 commited on
Commit
727e00d
·
verified ·
1 Parent(s): 52bd2a7

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +37 -25
templates/cart.html CHANGED
@@ -185,31 +185,43 @@
185
 
186
  // Update quantity
187
  function updateQuantity(action, itemName) {
188
- const quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
189
- let quantity = parseInt(quantityInput.value);
190
-
191
- if (action === 'increase') {
192
- quantity++;
193
- } else if (action === 'decrease' && quantity > 1) {
194
- quantity--;
195
- }
196
-
197
- fetch('/cart/update_quantity', {
198
- method: 'POST',
199
- headers: { 'Content-Type': 'application/json' },
200
- body: JSON.stringify({ item_name: itemName, quantity: quantity })
201
- })
202
- .then(response => response.json())
203
- .then(data => {
204
- if (data.success) {
205
- quantityInput.value = quantity;
206
- location.reload();
207
- } else {
208
- alert('Error updating quantity: ' + data.message);
209
- }
210
- })
211
- .catch(err => console.error('Error:', err));
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) {