Spaces:
Sleeping
Sleeping
Update templates/cart.html
Browse files- templates/cart.html +32 -45
templates/cart.html
CHANGED
@@ -170,54 +170,41 @@
|
|
170 |
</div>
|
171 |
|
172 |
<script>
|
173 |
-
function
|
174 |
-
|
175 |
-
const
|
176 |
-
let
|
177 |
-
// Adjust quantity based on the action
|
178 |
if (action === 'increase') {
|
179 |
-
|
180 |
-
} else if (action === 'decrease' &&
|
181 |
-
|
182 |
-
}else{
|
183 |
-
alert("Quantity cannot be less than 1!");
|
184 |
-
return;
|
185 |
}
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
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 |
-
|
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() {
|