Update app.py
Browse files
app.py
CHANGED
|
@@ -164,9 +164,10 @@ def add_to_cart():
|
|
| 164 |
return jsonify({"success": False, "error": "Item name and price are required."})
|
| 165 |
|
| 166 |
try:
|
| 167 |
-
# Query the cart to check if the item already exists
|
| 168 |
query = f"""
|
| 169 |
-
SELECT Id, Quantity__c, Add_Ons__c,
|
|
|
|
| 170 |
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
| 171 |
"""
|
| 172 |
result = sf.query(query)
|
|
@@ -176,18 +177,23 @@ def add_to_cart():
|
|
| 176 |
addons_price = sum(addon['price'] for addon in addons) # New add-ons price
|
| 177 |
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons]) # Format new add-ons
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
sf.Cart_Item__c.update(cart_item_id, {
|
| 186 |
-
"Quantity__c": existing_quantity + 1
|
| 187 |
-
"Instructions__c": combined_instructions, # Update instructions
|
| 188 |
})
|
| 189 |
else:
|
| 190 |
-
# If
|
| 191 |
sf.Cart_Item__c.create({
|
| 192 |
"Name": item_name,
|
| 193 |
"Price__c": item_price + addons_price,
|
|
@@ -197,7 +203,7 @@ def add_to_cart():
|
|
| 197 |
"Add_Ons_Price__c": addons_price,
|
| 198 |
"Image1__c": item_image,
|
| 199 |
"Customer_Email__c": customer_email,
|
| 200 |
-
"Instructions__c": instructions # Save instructions
|
| 201 |
})
|
| 202 |
|
| 203 |
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
|
@@ -375,6 +381,7 @@ def checkout():
|
|
| 375 |
print(f"Error during checkout: {str(e)}")
|
| 376 |
return jsonify({"success": False, "error": str(e)})
|
| 377 |
|
|
|
|
| 378 |
@app.route("/order", methods=["GET"])
|
| 379 |
def order_summary():
|
| 380 |
email = session.get('user_email') # Fetch logged-in user's email
|
|
|
|
| 164 |
return jsonify({"success": False, "error": "Item name and price are required."})
|
| 165 |
|
| 166 |
try:
|
| 167 |
+
# Query the cart to check if the item already exists with the same instructions
|
| 168 |
query = f"""
|
| 169 |
+
SELECT Id, Quantity__c, Add_Ons__c, Instructions__c
|
| 170 |
+
FROM Cart_Item__c
|
| 171 |
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
| 172 |
"""
|
| 173 |
result = sf.query(query)
|
|
|
|
| 177 |
addons_price = sum(addon['price'] for addon in addons) # New add-ons price
|
| 178 |
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons]) # Format new add-ons
|
| 179 |
|
| 180 |
+
# Check for the same instructions
|
| 181 |
+
matched_item = None
|
| 182 |
+
for item in cart_items:
|
| 183 |
+
if item.get('Instructions__c', '') == instructions:
|
| 184 |
+
matched_item = item
|
| 185 |
+
break
|
| 186 |
+
|
| 187 |
+
if matched_item:
|
| 188 |
+
# If the item already exists with the same instructions, update it
|
| 189 |
+
cart_item_id = matched_item['Id']
|
| 190 |
+
existing_quantity = matched_item['Quantity__c']
|
| 191 |
|
| 192 |
sf.Cart_Item__c.update(cart_item_id, {
|
| 193 |
+
"Quantity__c": existing_quantity + 1 # Increase quantity by 1
|
|
|
|
| 194 |
})
|
| 195 |
else:
|
| 196 |
+
# If instructions are different or item doesn't exist, create a new cart item
|
| 197 |
sf.Cart_Item__c.create({
|
| 198 |
"Name": item_name,
|
| 199 |
"Price__c": item_price + addons_price,
|
|
|
|
| 203 |
"Add_Ons_Price__c": addons_price,
|
| 204 |
"Image1__c": item_image,
|
| 205 |
"Customer_Email__c": customer_email,
|
| 206 |
+
"Instructions__c": instructions # Save the new instructions
|
| 207 |
})
|
| 208 |
|
| 209 |
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
|
|
|
| 381 |
print(f"Error during checkout: {str(e)}")
|
| 382 |
return jsonify({"success": False, "error": str(e)})
|
| 383 |
|
| 384 |
+
|
| 385 |
@app.route("/order", methods=["GET"])
|
| 386 |
def order_summary():
|
| 387 |
email = session.get('user_email') # Fetch logged-in user's email
|