Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1107,48 +1107,83 @@ def checkout():
|
|
1107 |
@app.route('/cart/add', methods=['POST'], endpoint='add_to_cart_v2')
|
1108 |
def add_to_cart():
|
1109 |
data = request.json
|
1110 |
-
item_name = data.get('itemName')
|
1111 |
-
item_price = data.get('itemPrice')
|
|
|
|
|
|
|
|
|
|
|
|
|
1112 |
|
1113 |
if not item_name or not item_price:
|
1114 |
-
return jsonify({"success": False, "error": "Item name and price are required."})
|
1115 |
|
1116 |
try:
|
1117 |
-
|
1118 |
-
|
1119 |
-
|
1120 |
-
|
1121 |
-
|
1122 |
-
|
1123 |
-
|
1124 |
-
|
1125 |
-
|
1126 |
-
result = sf.query(item_query)
|
1127 |
-
|
1128 |
-
if not result.get('records'):
|
1129 |
-
return jsonify({"success": False, "error": "Item not found."}), 404
|
1130 |
-
|
1131 |
-
item = result['records'][0]
|
1132 |
-
item_image = item.get('Image1__c', '/static/placeholder.jpg')
|
1133 |
-
|
1134 |
-
# Add the item to the user's cart
|
1135 |
-
cart_item = {
|
1136 |
-
'Name': item_name,
|
1137 |
-
'Price__c': item_price, # Ensure the price is passed as a string
|
1138 |
-
'Base_Price__c': item_price,
|
1139 |
-
'Quantity__c': 1,
|
1140 |
-
'Image1__c': item_image,
|
1141 |
-
'Customer_Email__c': email,
|
1142 |
-
'Instructions__c': '', # Optionally handle user instructions
|
1143 |
-
}
|
1144 |
|
1145 |
-
|
1146 |
-
|
|
|
|
|
|
|
|
|
1147 |
|
1148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1150 |
except Exception as e:
|
1151 |
-
|
|
|
1152 |
|
1153 |
@app.route("/order", methods=["GET"])
|
1154 |
def order_summary():
|
|
|
1107 |
@app.route('/cart/add', methods=['POST'], endpoint='add_to_cart_v2')
|
1108 |
def add_to_cart():
|
1109 |
data = request.json
|
1110 |
+
item_name = data.get('itemName').strip()
|
1111 |
+
item_price = data.get('itemPrice')
|
1112 |
+
item_image = data.get('itemImage')
|
1113 |
+
addons = data.get('addons', [])
|
1114 |
+
instructions = data.get('instructions', '')
|
1115 |
+
category = data.get('category')
|
1116 |
+
section = data.get('section')
|
1117 |
+
customer_email = session.get('user_email')
|
1118 |
|
1119 |
if not item_name or not item_price:
|
1120 |
+
return jsonify({"success": False, "error": "Item name and price are required."})
|
1121 |
|
1122 |
try:
|
1123 |
+
query = f"""
|
1124 |
+
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c FROM Cart_Item__c
|
1125 |
+
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
1126 |
+
"""
|
1127 |
+
result = sf.query(query)
|
1128 |
+
cart_items = result.get("records", [])
|
1129 |
+
|
1130 |
+
addons_price = sum(addon['price'] for addon in addons)
|
1131 |
+
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1132 |
|
1133 |
+
if cart_items:
|
1134 |
+
cart_item_id = cart_items[0]['Id']
|
1135 |
+
existing_quantity = cart_items[0]['Quantity__c']
|
1136 |
+
existing_addons = cart_items[0].get('Add_Ons__c', "None")
|
1137 |
+
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
1138 |
+
existing_instructions = cart_items[0].get('Instructions__c', "")
|
1139 |
|
1140 |
+
combined_addons = existing_addons if existing_addons != "None" else ""
|
1141 |
+
if new_addons:
|
1142 |
+
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
1143 |
+
|
1144 |
+
combined_instructions = existing_instructions
|
1145 |
+
if instructions:
|
1146 |
+
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
1147 |
+
|
1148 |
+
combined_addons_list = combined_addons.split("; ")
|
1149 |
+
combined_addons_price = sum(
|
1150 |
+
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
1151 |
+
)
|
1152 |
+
|
1153 |
+
sf.Cart_Item__c.update(cart_item_id, {
|
1154 |
+
"Quantity__c": existing_quantity + 1,
|
1155 |
+
"Add_Ons__c": combined_addons,
|
1156 |
+
"Add_Ons_Price__c": combined_addons_price,
|
1157 |
+
"Instructions__c": combined_instructions,
|
1158 |
+
"Price__c": (existing_quantity + 1) * item_price + combined_addons_price,
|
1159 |
+
"Category__c": category,
|
1160 |
+
"Section__c": section
|
1161 |
+
})
|
1162 |
+
else:
|
1163 |
+
addons_string = "None"
|
1164 |
+
if addons:
|
1165 |
+
addons_string = new_addons
|
1166 |
+
|
1167 |
+
total_price = item_price + addons_price
|
1168 |
|
1169 |
+
sf.Cart_Item__c.create({
|
1170 |
+
"Name": item_name,
|
1171 |
+
"Price__c": total_price,
|
1172 |
+
"Base_Price__c": item_price,
|
1173 |
+
"Quantity__c": 1,
|
1174 |
+
"Add_Ons_Price__c": addons_price,
|
1175 |
+
"Add_Ons__c": addons_string,
|
1176 |
+
"Image1__c": item_image,
|
1177 |
+
"Customer_Email__c": customer_email,
|
1178 |
+
"Instructions__c": instructions,
|
1179 |
+
"Category__c": category,
|
1180 |
+
"Section__c": section
|
1181 |
+
})
|
1182 |
+
|
1183 |
+
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
1184 |
except Exception as e:
|
1185 |
+
print(f"Error adding item to cart: {str(e)}")
|
1186 |
+
return jsonify({"success": False, "error": str(e)})
|
1187 |
|
1188 |
@app.route("/order", methods=["GET"])
|
1189 |
def order_summary():
|