Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -387,51 +387,57 @@ def get_addons():
|
|
387 |
return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."})
|
388 |
|
389 |
|
|
|
390 |
@app.route("/cart/update_quantity", methods=["POST"])
|
391 |
def update_quantity():
|
392 |
-
data = request.json
|
393 |
-
item_name = data.get(
|
|
|
394 |
|
395 |
try:
|
396 |
-
|
397 |
-
quantity = int(data.get('quantity'))
|
398 |
except (ValueError, TypeError):
|
399 |
return jsonify({"success": False, "error": "Invalid quantity provided."}), 400
|
400 |
|
401 |
-
if not item_name or
|
402 |
-
return jsonify({"success": False, "error": "
|
403 |
|
404 |
try:
|
405 |
-
#
|
406 |
-
cart_items = sf.query(
|
407 |
-
f"SELECT Id, Quantity__c, Price__c, Base_Price__c, Add_Ons_Price__c FROM Cart_Item__c "
|
408 |
-
f"WHERE Name = '{item_name}'"
|
409 |
-
)['records']
|
410 |
|
411 |
if not cart_items:
|
412 |
return jsonify({"success": False, "error": "Cart item not found."}), 404
|
413 |
|
414 |
-
|
415 |
-
|
416 |
-
base_price = cart_items[0].get('Base_Price__c', 0)
|
417 |
-
addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
418 |
-
print(base_price)
|
419 |
|
420 |
if base_price is None:
|
421 |
base_price = 0
|
422 |
-
# Calculate new item price
|
423 |
-
new_item_price = (base_price * quantity) + addons_price
|
424 |
-
print(new_item_price)
|
425 |
|
426 |
-
|
|
|
|
|
427 |
sf.Cart_Item__c.update(cart_item_id, {
|
428 |
"Quantity__c": quantity,
|
429 |
-
"Price__c": new_item_price,
|
430 |
})
|
431 |
|
432 |
-
#
|
433 |
-
|
434 |
-
new_subtotal = sum(item[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
435 |
|
436 |
return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
|
437 |
|
@@ -439,7 +445,6 @@ def update_quantity():
|
|
439 |
print(f"Error updating quantity: {str(e)}")
|
440 |
return jsonify({"success": False, "error": str(e)}), 500
|
441 |
|
442 |
-
|
443 |
|
444 |
@app.route("/checkout", methods=["POST"])
|
445 |
def checkout():
|
|
|
387 |
return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."})
|
388 |
|
389 |
|
390 |
+
|
391 |
@app.route("/cart/update_quantity", methods=["POST"])
|
392 |
def update_quantity():
|
393 |
+
data = request.json
|
394 |
+
item_name = data.get("item_name")
|
395 |
+
email = data.get("email")
|
396 |
|
397 |
try:
|
398 |
+
quantity = int(data.get("quantity"))
|
|
|
399 |
except (ValueError, TypeError):
|
400 |
return jsonify({"success": False, "error": "Invalid quantity provided."}), 400
|
401 |
|
402 |
+
if not item_name or not email:
|
403 |
+
return jsonify({"success": False, "error": "Email and item name are required."}), 400
|
404 |
|
405 |
try:
|
406 |
+
# Fetch the cart item from Salesforce
|
407 |
+
cart_items = sf.query(f"SELECT Id, Base_Price__c FROM Cart_Item__c WHERE Name = '{item_name}'")["records"]
|
|
|
|
|
|
|
408 |
|
409 |
if not cart_items:
|
410 |
return jsonify({"success": False, "error": "Cart item not found."}), 404
|
411 |
|
412 |
+
cart_item_id = cart_items[0]["Id"]
|
413 |
+
base_price = cart_items[0].get("Base_Price__c", 0) # Default to 0 if None
|
|
|
|
|
|
|
414 |
|
415 |
if base_price is None:
|
416 |
base_price = 0
|
|
|
|
|
|
|
417 |
|
418 |
+
new_item_price = base_price * quantity
|
419 |
+
|
420 |
+
# Update Cart Item in Salesforce
|
421 |
sf.Cart_Item__c.update(cart_item_id, {
|
422 |
"Quantity__c": quantity,
|
423 |
+
"Price__c": new_item_price,
|
424 |
})
|
425 |
|
426 |
+
# Fetch all cart items for this user to calculate the new subtotal
|
427 |
+
cart_items = sf.query(f"SELECT Price__c FROM Cart_Item__c WHERE Customer_Email__c = '{email}'")["records"]
|
428 |
+
new_subtotal = sum(item["Price__c"] for item in cart_items)
|
429 |
+
|
430 |
+
# Fetch the latest Order for the user
|
431 |
+
order_result = sf.query(f"""
|
432 |
+
SELECT Id FROM Order__c
|
433 |
+
WHERE Customer_Email__c = '{email}'
|
434 |
+
ORDER BY CreatedDate DESC
|
435 |
+
LIMIT 1
|
436 |
+
""")
|
437 |
+
|
438 |
+
if order_result.get("records"):
|
439 |
+
order_id = order_result["records"][0]["Id"]
|
440 |
+
sf.Order__c.update(order_id, {"Total_Amount__c": new_subtotal})
|
441 |
|
442 |
return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
|
443 |
|
|
|
445 |
print(f"Error updating quantity: {str(e)}")
|
446 |
return jsonify({"success": False, "error": str(e)}), 500
|
447 |
|
|
|
448 |
|
449 |
@app.route("/checkout", methods=["POST"])
|
450 |
def checkout():
|