Spaces:
Runtime error
Runtime error
Update Cart_Page.py
Browse files- Cart_Page.py +57 -0
Cart_Page.py
CHANGED
@@ -243,3 +243,60 @@ def remove_cart_item(item_name):
|
|
243 |
return jsonify({'success': False, 'message': f"An error occurred: {str(e)}"}), 500
|
244 |
|
245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
return jsonify({'success': False, 'message': f"An error occurred: {str(e)}"}), 500
|
244 |
|
245 |
|
246 |
+
cart_page.route("/cart/update_quantity", methods=["POST"])
|
247 |
+
def update_quantity():
|
248 |
+
data = request.json # Extract JSON data from the request
|
249 |
+
email = data.get('email')
|
250 |
+
item_name = data.get('item_name')
|
251 |
+
try:
|
252 |
+
# Convert quantity to an integer
|
253 |
+
quantity = int(data.get('quantity'))
|
254 |
+
except (ValueError, TypeError):
|
255 |
+
return jsonify({"success": False, "error": "Invalid quantity provided."}), 400
|
256 |
+
|
257 |
+
# Validate inputs
|
258 |
+
if not email or not item_name or quantity is None:
|
259 |
+
return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400
|
260 |
+
|
261 |
+
try:
|
262 |
+
# Query the cart item in Salesforce
|
263 |
+
cart_items = sf.query(
|
264 |
+
f"SELECT Id, Quantity__c, Price__c, Base_Price__c, Add_Ons_Price__c FROM Cart_Item__c "
|
265 |
+
f"WHERE Customer_Email__c = '{email}' AND Name = '{item_name}'"
|
266 |
+
)['records']
|
267 |
+
|
268 |
+
if not cart_items:
|
269 |
+
return jsonify({"success": False, "error": "Cart item not found."}), 404
|
270 |
+
|
271 |
+
# Retrieve the first matching record
|
272 |
+
cart_item_id = cart_items[0]['Id']
|
273 |
+
base_price = cart_items[0]['Base_Price__c']
|
274 |
+
addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
275 |
+
|
276 |
+
# Calculate the new item price
|
277 |
+
new_item_price = (base_price * quantity) + addons_price
|
278 |
+
|
279 |
+
# Update the record in Salesforce
|
280 |
+
sf.Cart_Item__c.update(cart_item_id, {
|
281 |
+
"Quantity__c": quantity,
|
282 |
+
"Price__c": new_item_price, # Update base price
|
283 |
+
})
|
284 |
+
|
285 |
+
# Recalculate the subtotal for all items in the cart
|
286 |
+
cart_items = sf.query(f"""
|
287 |
+
SELECT Price__c, Add_Ons_Price__c
|
288 |
+
FROM Cart_Item__c
|
289 |
+
WHERE Customer_Email__c = '{email}'
|
290 |
+
""")['records']
|
291 |
+
new_subtotal = sum(item['Price__c'] for item in cart_items)
|
292 |
+
|
293 |
+
# Return updated item price and subtotal
|
294 |
+
return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
|
295 |
+
print(f"New item price: {new_item_price}, New subtotal: {new_subtotal}")
|
296 |
+
return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
|
297 |
+
|
298 |
+
except Exception as e:
|
299 |
+
print(f"Error updating quantity: {str(e)}")
|
300 |
+
return jsonify({"success": False, "error": str(e)}), 500
|
301 |
+
|
302 |
+
|