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