Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -389,54 +389,46 @@ def get_addons():
|
|
389 |
|
390 |
@app.route("/cart/update_quantity", methods=["POST"])
|
391 |
def update_quantity():
|
392 |
-
data = request.json
|
393 |
-
email = data.get('email')
|
394 |
item_name = data.get('item_name')
|
|
|
395 |
try:
|
396 |
# Convert quantity to an integer
|
397 |
quantity = int(data.get('quantity'))
|
398 |
except (ValueError, TypeError):
|
399 |
return jsonify({"success": False, "error": "Invalid quantity provided."}), 400
|
400 |
|
401 |
-
|
402 |
-
|
403 |
-
return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400
|
404 |
|
405 |
try:
|
406 |
-
# Query
|
407 |
cart_items = sf.query(
|
408 |
f"SELECT Id, Quantity__c, Price__c, Base_Price__c, Add_Ons_Price__c FROM Cart_Item__c "
|
409 |
-
f"WHERE
|
410 |
)['records']
|
411 |
|
412 |
if not cart_items:
|
413 |
return jsonify({"success": False, "error": "Cart item not found."}), 404
|
414 |
|
415 |
-
# Retrieve
|
416 |
cart_item_id = cart_items[0]['Id']
|
417 |
base_price = cart_items[0]['Base_Price__c']
|
418 |
addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
419 |
|
420 |
-
# Calculate
|
421 |
new_item_price = (base_price * quantity) + addons_price
|
422 |
|
423 |
# Update the record in Salesforce
|
424 |
sf.Cart_Item__c.update(cart_item_id, {
|
425 |
"Quantity__c": quantity,
|
426 |
-
"Price__c": new_item_price, # Update
|
427 |
})
|
428 |
|
429 |
-
# Recalculate
|
430 |
-
|
431 |
-
|
432 |
-
FROM Cart_Item__c
|
433 |
-
WHERE Customer_Email__c = '{email}'
|
434 |
-
""")['records']
|
435 |
-
new_subtotal = sum(item['Price__c'] for item in cart_items)
|
436 |
|
437 |
-
# Return updated item price and subtotal
|
438 |
-
return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
|
439 |
-
print(f"New item price: {new_item_price}, New subtotal: {new_subtotal}")
|
440 |
return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
|
441 |
|
442 |
except Exception as e:
|
|
|
389 |
|
390 |
@app.route("/cart/update_quantity", methods=["POST"])
|
391 |
def update_quantity():
|
392 |
+
data = request.json
|
|
|
393 |
item_name = data.get('item_name')
|
394 |
+
|
395 |
try:
|
396 |
# Convert quantity to an integer
|
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 quantity is None:
|
402 |
+
return jsonify({"success": False, "error": "Item name and quantity are required."}), 400
|
|
|
403 |
|
404 |
try:
|
405 |
+
# Query cart item from Salesforce
|
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 |
+
# Retrieve first matching record
|
415 |
cart_item_id = cart_items[0]['Id']
|
416 |
base_price = cart_items[0]['Base_Price__c']
|
417 |
addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
418 |
|
419 |
+
# Calculate new item price
|
420 |
new_item_price = (base_price * quantity) + addons_price
|
421 |
|
422 |
# Update the record in Salesforce
|
423 |
sf.Cart_Item__c.update(cart_item_id, {
|
424 |
"Quantity__c": quantity,
|
425 |
+
"Price__c": new_item_price, # Update item price
|
426 |
})
|
427 |
|
428 |
+
# Recalculate subtotal for all items in cart
|
429 |
+
all_cart_items = sf.query("SELECT Price__c FROM Cart_Item__c")['records']
|
430 |
+
new_subtotal = sum(item['Price__c'] for item in all_cart_items)
|
|
|
|
|
|
|
|
|
431 |
|
|
|
|
|
|
|
432 |
return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
|
433 |
|
434 |
except Exception as e:
|