Update app.py
Browse files
app.py
CHANGED
|
@@ -767,24 +767,25 @@ def add_suggestion_to_cart():
|
|
| 767 |
|
| 768 |
@app.route('/cart/add', methods=['POST'])
|
| 769 |
def add_to_cart():
|
| 770 |
-
data = request.json
|
| 771 |
-
item_name = data.get('itemName', '').strip()
|
| 772 |
-
item_price = data.get('itemPrice')
|
| 773 |
-
item_image = data.get('itemImage')
|
| 774 |
-
addons = data.get('addons', [])
|
| 775 |
-
instructions = data.get('instructions', '')
|
| 776 |
-
category = data.get('category')
|
| 777 |
-
section = data.get('section')
|
| 778 |
-
customer_email = session.get('user_email')
|
| 779 |
-
|
| 780 |
-
# Basic validation for required fields
|
| 781 |
-
if not item_name or not item_price:
|
| 782 |
-
return jsonify({"success": False, "error": "Item name and price are required."}), 400
|
| 783 |
-
|
| 784 |
-
if not customer_email:
|
| 785 |
-
return jsonify({"success": False, "error": "User email is required."}), 400
|
| 786 |
-
|
| 787 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 788 |
# Query to check if the item is already in the cart
|
| 789 |
query = f"""
|
| 790 |
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
|
@@ -856,9 +857,13 @@ def add_to_cart():
|
|
| 856 |
})
|
| 857 |
|
| 858 |
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
| 859 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 860 |
except Exception as e:
|
| 861 |
-
# Log the error for debugging
|
| 862 |
print(f"Error adding item to cart: {str(e)}")
|
| 863 |
return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
|
| 864 |
|
|
|
|
| 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 |
+
customer_email = session.get('user_email')
|
| 781 |
+
|
| 782 |
+
# Basic validation for required fields
|
| 783 |
+
if not item_name or not item_price:
|
| 784 |
+
return jsonify({"success": False, "error": "Item name and price are required."}), 400
|
| 785 |
+
|
| 786 |
+
if not customer_email:
|
| 787 |
+
return jsonify({"success": False, "error": "User email is required."}), 400
|
| 788 |
+
|
| 789 |
# Query to check if the item is already in the cart
|
| 790 |
query = f"""
|
| 791 |
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
|
|
|
| 857 |
})
|
| 858 |
|
| 859 |
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
| 860 |
+
|
| 861 |
+
except KeyError as e:
|
| 862 |
+
# Handle missing expected keys in request data
|
| 863 |
+
return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400
|
| 864 |
+
|
| 865 |
except Exception as e:
|
| 866 |
+
# Log the error for debugging and return a general error message
|
| 867 |
print(f"Error adding item to cart: {str(e)}")
|
| 868 |
return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
|
| 869 |
|