Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -768,30 +768,40 @@ def add_suggestion_to_cart():
|
|
| 768 |
|
| 769 |
@app.route('/cart/add', methods=['POST'])
|
| 770 |
def add_to_cart():
|
| 771 |
-
data = request.json
|
| 772 |
-
item_name = data.get('itemName').strip()
|
| 773 |
-
item_price = data.get('itemPrice')
|
| 774 |
-
item_image = data.get('itemImage')
|
| 775 |
-
addons = data.get('addons', [])
|
| 776 |
-
instructions = data.get('instructions', '')
|
| 777 |
-
category = data.get('category')
|
| 778 |
-
section = data.get('section')
|
| 779 |
-
customer_email = session.get('user_email')
|
| 780 |
-
|
| 781 |
-
if not item_name or not item_price:
|
| 782 |
-
return jsonify({"success": False, "error": "Item name and price are required."})
|
| 783 |
-
|
| 784 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 785 |
query = f"""
|
| 786 |
-
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
|
|
|
| 787 |
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
| 788 |
"""
|
| 789 |
result = sf.query(query)
|
| 790 |
cart_items = result.get("records", [])
|
| 791 |
|
|
|
|
| 792 |
addons_price = sum(addon['price'] for addon in addons)
|
| 793 |
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
| 794 |
|
|
|
|
| 795 |
if cart_items:
|
| 796 |
cart_item_id = cart_items[0]['Id']
|
| 797 |
existing_quantity = cart_items[0]['Quantity__c']
|
|
@@ -799,40 +809,46 @@ def add_to_cart():
|
|
| 799 |
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
| 800 |
existing_instructions = cart_items[0].get('Instructions__c', "")
|
| 801 |
|
|
|
|
| 802 |
combined_addons = existing_addons if existing_addons != "None" else ""
|
| 803 |
if new_addons:
|
| 804 |
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
| 805 |
|
|
|
|
| 806 |
combined_instructions = existing_instructions
|
| 807 |
if instructions:
|
| 808 |
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
| 809 |
|
|
|
|
| 810 |
combined_addons_list = combined_addons.split("; ")
|
| 811 |
combined_addons_price = sum(
|
| 812 |
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
| 813 |
)
|
| 814 |
|
|
|
|
| 815 |
sf.Cart_Item__c.update(cart_item_id, {
|
| 816 |
-
"Quantity__c": existing_quantity +
|
| 817 |
"Add_Ons__c": combined_addons,
|
| 818 |
"Add_Ons_Price__c": combined_addons_price,
|
| 819 |
"Instructions__c": combined_instructions,
|
| 820 |
-
"Price__c": (existing_quantity +
|
| 821 |
"Category__c": category,
|
| 822 |
"Section__c": section
|
| 823 |
})
|
| 824 |
else:
|
|
|
|
| 825 |
addons_string = "None"
|
| 826 |
if addons:
|
| 827 |
addons_string = new_addons
|
| 828 |
|
| 829 |
-
total_price = item_price + addons_price
|
| 830 |
|
|
|
|
| 831 |
sf.Cart_Item__c.create({
|
| 832 |
"Name": item_name,
|
| 833 |
"Price__c": total_price,
|
| 834 |
"Base_Price__c": item_price,
|
| 835 |
-
"Quantity__c":
|
| 836 |
"Add_Ons_Price__c": addons_price,
|
| 837 |
"Add_Ons__c": addons_string,
|
| 838 |
"Image1__c": item_image,
|
|
@@ -843,13 +859,15 @@ def add_to_cart():
|
|
| 843 |
})
|
| 844 |
|
| 845 |
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
| 846 |
-
except Exception as e:
|
| 847 |
-
print(f"Error adding item to cart: {str(e)}")
|
| 848 |
-
return jsonify({"success": False, "error": str(e)})
|
| 849 |
-
|
| 850 |
-
|
| 851 |
|
|
|
|
|
|
|
|
|
|
| 852 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 853 |
|
| 854 |
|
| 855 |
@app.route("/cart/add_item", methods=["POST"])
|
|
|
|
| 768 |
|
| 769 |
@app.route('/cart/add', methods=['POST'])
|
| 770 |
def add_to_cart():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
try:
|
| 772 |
+
# Get data from request
|
| 773 |
+
data = request.json
|
| 774 |
+
item_name = data.get('itemName', '').strip()
|
| 775 |
+
item_price = data.get('itemPrice')
|
| 776 |
+
item_image = data.get('itemImage')
|
| 777 |
+
addons = data.get('addons', [])
|
| 778 |
+
instructions = data.get('instructions', '')
|
| 779 |
+
category = data.get('category')
|
| 780 |
+
section = data.get('section')
|
| 781 |
+
quantity = data.get('quantity', 1) # Get the quantity field from the request
|
| 782 |
+
customer_email = session.get('user_email')
|
| 783 |
+
|
| 784 |
+
# Basic validation for required fields
|
| 785 |
+
if not item_name or not item_price:
|
| 786 |
+
return jsonify({"success": False, "error": "Item name and price are required."}), 400
|
| 787 |
+
|
| 788 |
+
if not customer_email:
|
| 789 |
+
return jsonify({"success": False, "error": "User email is required."}), 400
|
| 790 |
+
|
| 791 |
+
# Query to check if the item is already in the cart
|
| 792 |
query = f"""
|
| 793 |
+
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
| 794 |
+
FROM Cart_Item__c
|
| 795 |
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
| 796 |
"""
|
| 797 |
result = sf.query(query)
|
| 798 |
cart_items = result.get("records", [])
|
| 799 |
|
| 800 |
+
# Calculate the total price for the addons
|
| 801 |
addons_price = sum(addon['price'] for addon in addons)
|
| 802 |
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
| 803 |
|
| 804 |
+
# If the item is already in the cart, update it
|
| 805 |
if cart_items:
|
| 806 |
cart_item_id = cart_items[0]['Id']
|
| 807 |
existing_quantity = cart_items[0]['Quantity__c']
|
|
|
|
| 809 |
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
| 810 |
existing_instructions = cart_items[0].get('Instructions__c', "")
|
| 811 |
|
| 812 |
+
# Combine the new addons with the existing ones
|
| 813 |
combined_addons = existing_addons if existing_addons != "None" else ""
|
| 814 |
if new_addons:
|
| 815 |
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
| 816 |
|
| 817 |
+
# Combine existing instructions with new instructions
|
| 818 |
combined_instructions = existing_instructions
|
| 819 |
if instructions:
|
| 820 |
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
| 821 |
|
| 822 |
+
# Calculate total addons price
|
| 823 |
combined_addons_list = combined_addons.split("; ")
|
| 824 |
combined_addons_price = sum(
|
| 825 |
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
| 826 |
)
|
| 827 |
|
| 828 |
+
# Update the cart item in Salesforce (updating quantity)
|
| 829 |
sf.Cart_Item__c.update(cart_item_id, {
|
| 830 |
+
"Quantity__c": existing_quantity + quantity, # Add the selected quantity
|
| 831 |
"Add_Ons__c": combined_addons,
|
| 832 |
"Add_Ons_Price__c": combined_addons_price,
|
| 833 |
"Instructions__c": combined_instructions,
|
| 834 |
+
"Price__c": (existing_quantity + quantity) * item_price + combined_addons_price,
|
| 835 |
"Category__c": category,
|
| 836 |
"Section__c": section
|
| 837 |
})
|
| 838 |
else:
|
| 839 |
+
# If the item is not already in the cart, create a new entry
|
| 840 |
addons_string = "None"
|
| 841 |
if addons:
|
| 842 |
addons_string = new_addons
|
| 843 |
|
| 844 |
+
total_price = item_price * quantity + addons_price # Multiply by the quantity
|
| 845 |
|
| 846 |
+
# Create new cart item in Salesforce
|
| 847 |
sf.Cart_Item__c.create({
|
| 848 |
"Name": item_name,
|
| 849 |
"Price__c": total_price,
|
| 850 |
"Base_Price__c": item_price,
|
| 851 |
+
"Quantity__c": quantity, # Use the selected quantity
|
| 852 |
"Add_Ons_Price__c": addons_price,
|
| 853 |
"Add_Ons__c": addons_string,
|
| 854 |
"Image1__c": item_image,
|
|
|
|
| 859 |
})
|
| 860 |
|
| 861 |
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 862 |
|
| 863 |
+
except KeyError as e:
|
| 864 |
+
# Handle missing expected keys in request data
|
| 865 |
+
return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400
|
| 866 |
|
| 867 |
+
except Exception as e:
|
| 868 |
+
# Log the error for debugging and return a general error message
|
| 869 |
+
print(f"Error adding item to cart: {str(e)}")
|
| 870 |
+
return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
|
| 871 |
|
| 872 |
|
| 873 |
@app.route("/cart/add_item", methods=["POST"])
|