Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -314,38 +314,60 @@ def update_quantity():
|
|
| 314 |
|
| 315 |
@app.route("/checkout", methods=["POST"])
|
| 316 |
def checkout():
|
|
|
|
| 317 |
email = session.get('user_email')
|
| 318 |
user_id = session.get('user_id')
|
|
|
|
|
|
|
| 319 |
if not email or not user_id:
|
| 320 |
return jsonify({"success": False, "message": "User not logged in"})
|
| 321 |
|
| 322 |
try:
|
| 323 |
-
# Fetch
|
| 324 |
result = sf.query(f"""
|
| 325 |
SELECT Id, Name, Price__c, Quantity__c, Add_Ons__c
|
| 326 |
FROM Cart_Item__c
|
| 327 |
WHERE Customer_Email__c = '{email}'
|
| 328 |
""")
|
| 329 |
cart_items = result.get("records", [])
|
|
|
|
|
|
|
| 330 |
if not cart_items:
|
| 331 |
return jsonify({"success": False, "message": "Cart is empty"})
|
| 332 |
|
| 333 |
-
#
|
| 334 |
-
total_price =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
order_data = {
|
| 338 |
"Customer_Name__c": user_id,
|
| 339 |
"Customer_Email__c": email,
|
| 340 |
"Total_Amount__c": total_price,
|
| 341 |
"Order_Status__c": "Pending",
|
| 342 |
-
"Order_Items__c": "\n".join(
|
| 343 |
-
[f"{item['Name']} (Qty: {item['Quantity__c']})" for item in cart_items]
|
| 344 |
-
),
|
| 345 |
-
"Add_Ons__c": "\n".join(
|
| 346 |
-
[item['Add_Ons__c'] if item.get('Add_Ons__c') else "None" for item in cart_items]
|
| 347 |
-
),
|
| 348 |
}
|
|
|
|
|
|
|
| 349 |
sf.Order__c.create(order_data)
|
| 350 |
|
| 351 |
# Clear the cart after placing the order
|
|
@@ -353,7 +375,10 @@ def checkout():
|
|
| 353 |
sf.Cart_Item__c.delete(item["Id"])
|
| 354 |
|
| 355 |
return jsonify({"success": True, "message": "Order placed successfully!"})
|
|
|
|
| 356 |
except Exception as e:
|
|
|
|
|
|
|
| 357 |
return jsonify({"success": False, "error": str(e)})
|
| 358 |
|
| 359 |
|
|
|
|
| 314 |
|
| 315 |
@app.route("/checkout", methods=["POST"])
|
| 316 |
def checkout():
|
| 317 |
+
# Fetch the logged-in user's email and ID
|
| 318 |
email = session.get('user_email')
|
| 319 |
user_id = session.get('user_id')
|
| 320 |
+
|
| 321 |
+
# Validate that the user is logged in
|
| 322 |
if not email or not user_id:
|
| 323 |
return jsonify({"success": False, "message": "User not logged in"})
|
| 324 |
|
| 325 |
try:
|
| 326 |
+
# Fetch all items from the cart for this user
|
| 327 |
result = sf.query(f"""
|
| 328 |
SELECT Id, Name, Price__c, Quantity__c, Add_Ons__c
|
| 329 |
FROM Cart_Item__c
|
| 330 |
WHERE Customer_Email__c = '{email}'
|
| 331 |
""")
|
| 332 |
cart_items = result.get("records", [])
|
| 333 |
+
|
| 334 |
+
# Check if the cart is empty
|
| 335 |
if not cart_items:
|
| 336 |
return jsonify({"success": False, "message": "Cart is empty"})
|
| 337 |
|
| 338 |
+
# Initialize total price and order items list
|
| 339 |
+
total_price = 0
|
| 340 |
+
order_items = []
|
| 341 |
+
|
| 342 |
+
# Iterate through each cart item to calculate total price and format order details
|
| 343 |
+
for item in cart_items:
|
| 344 |
+
item_name = item['Name']
|
| 345 |
+
item_price = item['Price__c']
|
| 346 |
+
item_quantity = item['Quantity__c']
|
| 347 |
+
addons_string = item.get('Add_Ons__c', "None")
|
| 348 |
+
|
| 349 |
+
# Calculate the total price for the item
|
| 350 |
+
total_price += item_price * item_quantity
|
| 351 |
|
| 352 |
+
# Add the main item to the order summary
|
| 353 |
+
order_items.append(f"{item_name} (Qty: {item_quantity})")
|
| 354 |
+
|
| 355 |
+
# Process addons if they exist
|
| 356 |
+
if addons_string and addons_string != "None":
|
| 357 |
+
addons = addons_string.split(";") # Assuming addons are stored in a semicolon-separated format
|
| 358 |
+
for addon in addons:
|
| 359 |
+
order_items.append(f" - {addon.strip()}") # Add each addon as part of the order summary
|
| 360 |
+
|
| 361 |
+
# Prepare the order data to be stored in Salesforce
|
| 362 |
order_data = {
|
| 363 |
"Customer_Name__c": user_id,
|
| 364 |
"Customer_Email__c": email,
|
| 365 |
"Total_Amount__c": total_price,
|
| 366 |
"Order_Status__c": "Pending",
|
| 367 |
+
"Order_Items__c": "\n".join(order_items), # Format order items into a single string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
}
|
| 369 |
+
|
| 370 |
+
# Create the order record in Salesforce
|
| 371 |
sf.Order__c.create(order_data)
|
| 372 |
|
| 373 |
# Clear the cart after placing the order
|
|
|
|
| 375 |
sf.Cart_Item__c.delete(item["Id"])
|
| 376 |
|
| 377 |
return jsonify({"success": True, "message": "Order placed successfully!"})
|
| 378 |
+
|
| 379 |
except Exception as e:
|
| 380 |
+
# Handle any exceptions during the process
|
| 381 |
+
print(f"Error during checkout: {str(e)}")
|
| 382 |
return jsonify({"success": False, "error": str(e)})
|
| 383 |
|
| 384 |
|