nagasurendra commited on
Commit
423a4e7
·
verified ·
1 Parent(s): 18dc44c

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +32 -45
templates/cart.html CHANGED
@@ -170,54 +170,41 @@
170
  </div>
171
 
172
  <script>
173
- function updateQuantity(action, itemName, price, customerEmail) {
174
- const inputField = document.querySelector(`input[data-item-name="${itemName}"]`);
175
- const subtotalField = document.querySelector('.cart-summary p');
176
- let currentQuantity = parseInt(inputField.value);
177
- // Adjust quantity based on the action
178
  if (action === 'increase') {
179
- currentQuantity += 1;
180
- } else if (action === 'decrease' && currentQuantity > 1) {
181
- currentQuantity -= 1;
182
- }else{
183
- alert("Quantity cannot be less than 1!");
184
- return;
185
  }
186
-
187
- // Update the input field value locally
188
- inputField.value = currentQuantity;
189
-
190
- // Debugging: Log API request payload
191
- console.log("Updating Quantity:", {
192
- email: customerEmail,
193
- item_name: itemName,
194
- action: action,
195
- price:price,
196
- quantity: currentQuantity
197
- });
198
- // Calculate new subtotal
199
- let newSubtotal = calculateSubtotal(); // Correctly declare the newSubtotal variable
200
- subtotalField.innerText = `Subtotal: $${newSubtotal.toFixed(2)}`;
201
- // Correctly append the itemId to the URL
202
- // Make the server request to update the quantity
203
- // Make API call to update the quantity on the backend
204
- fetch(`/cart/update_quantity`, {
205
- method: "POST",
206
- headers: { "Content-Type": "application/json" },
207
- body: JSON.stringify({ email: customerEmail,
208
- item_name: itemName, action: action, quantity:currentQuantity }) //send the current quantity
209
- })
210
-
211
- .then(response => response.json())
212
- .then(data => {
213
- if (!data.success) {
214
- alert(data.error || "Failed to update quantity.");
215
- } else {
216
- console.log(`Quantity updated for ${itemName}: ${currentQuantity}`);
217
- }
218
  })
219
- .catch(err => console.error("Error updating quantity:", err));
220
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
 
223
  function calculateSubtotal() {
 
170
  </div>
171
 
172
  <script>
173
+ // Example function to handle the increase/decrease button clicks
174
+ function updateCartQuantity(itemName, action) {
175
+ const email = sessionStorage.getItem("userEmail"); // or get from your session
176
+ let quantity = parseInt(document.getElementById(`${itemName}-quantity`).innerText); // Current quantity on the page
 
177
  if (action === 'increase') {
178
+ quantity++;
179
+ } else if (action === 'decrease' && quantity > 1) {
180
+ quantity--;
 
 
 
181
  }
182
+ // Send the updated data to the server
183
+ fetch('/cart/update_quantity', {
184
+ method: 'POST',
185
+ headers: {
186
+ 'Content-Type': 'application/json',
187
+ },
188
+ body: JSON.stringify({
189
+ email: email,
190
+ item_name: itemName,
191
+ quantity: quantity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  })
193
+ })
194
+ .then(response => response.json())
195
+ .then(data => {
196
+ if (data.success) {
197
+ // Update the UI to reflect the new quantity
198
+ document.getElementById(`${itemName}-quantity`).innerText = quantity;
199
+ } else {
200
+ alert("Error updating quantity: " + data.error);
201
+ }
202
+ })
203
+ .catch(error => {
204
+ console.error('Error:', error);
205
+ });
206
+ }
207
+
208
 
209
 
210
  function calculateSubtotal() {