Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -534,7 +534,7 @@ def checkout():
|
|
534 |
|
535 |
try:
|
536 |
data = request.json
|
537 |
-
selected_coupon = data.get("selectedCoupon", "").strip()
|
538 |
|
539 |
# Fetch cart items
|
540 |
result = sf.query(f"""
|
@@ -548,53 +548,20 @@ def checkout():
|
|
548 |
return jsonify({"success": False, "message": "Cart is empty"})
|
549 |
|
550 |
total_price = sum(item['Price__c'] for item in cart_items)
|
551 |
-
discount = 0
|
552 |
-
|
553 |
-
# Fetch the user's existing coupons
|
554 |
-
coupon_query = sf.query(f"""
|
555 |
-
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
556 |
-
""")
|
557 |
-
|
558 |
-
has_coupons = bool(coupon_query["records"]) # Check if user has any coupons
|
559 |
|
560 |
if selected_coupon:
|
561 |
-
# Apply discount
|
562 |
-
discount = total_price * 0.10 # 10% discount
|
563 |
-
referral_coupon_id = coupon_query["records"][0]["Id"]
|
564 |
-
existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")
|
565 |
-
|
566 |
-
# Remove only the selected coupon
|
567 |
-
updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
|
568 |
-
updated_coupons_str = "\n".join(updated_coupons).strip()
|
569 |
-
|
570 |
-
# Update the Referral_Coupon__c record with the remaining coupons
|
571 |
-
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
572 |
-
"Coupon_Code__c": updated_coupons_str
|
573 |
-
})
|
574 |
-
else:
|
575 |
-
# Add 10% to reward points if no coupon is used
|
576 |
-
reward_points_to_add = total_price * 0.10
|
577 |
-
|
578 |
-
# Fetch current reward points
|
579 |
-
customer_record = sf.query(f"""
|
580 |
-
SELECT Id, Reward_Points__c FROM Customer_Login__c
|
581 |
-
WHERE Email__c = '{email}'
|
582 |
-
""")
|
583 |
-
customer = customer_record.get("records", [])[0] if customer_record else None
|
584 |
-
|
585 |
-
if customer:
|
586 |
-
current_reward_points = customer.get("Reward_Points__c") or 0
|
587 |
-
new_reward_points = current_reward_points + reward_points_to_add
|
588 |
-
|
589 |
-
# Update reward points in Salesforce
|
590 |
-
sf.Customer_Login__c.update(customer["Id"], {
|
591 |
-
"Reward_Points__c": new_reward_points
|
592 |
-
})
|
593 |
|
594 |
total_bill = total_price - discount
|
595 |
|
596 |
-
# ✅ Store
|
597 |
-
order_details = "\n".join(
|
|
|
|
|
|
|
|
|
|
|
598 |
|
599 |
# Store the order details in Order__c
|
600 |
order_data = {
|
@@ -604,7 +571,7 @@ def checkout():
|
|
604 |
"Discount__c": discount,
|
605 |
"Total_Bill__c": total_bill,
|
606 |
"Order_Status__c": "Pending",
|
607 |
-
"Order_Details__c": order_details # ✅
|
608 |
}
|
609 |
|
610 |
sf.Order__c.create(order_data)
|
@@ -619,7 +586,6 @@ def checkout():
|
|
619 |
print(f"Error during checkout: {str(e)}")
|
620 |
return jsonify({"success": False, "error": str(e)})
|
621 |
|
622 |
-
|
623 |
@app.route("/order", methods=["GET"])
|
624 |
def order_summary():
|
625 |
email = session.get('user_email') # Fetch logged-in user's email
|
|
|
534 |
|
535 |
try:
|
536 |
data = request.json
|
537 |
+
selected_coupon = data.get("selectedCoupon", "").strip()
|
538 |
|
539 |
# Fetch cart items
|
540 |
result = sf.query(f"""
|
|
|
548 |
return jsonify({"success": False, "message": "Cart is empty"})
|
549 |
|
550 |
total_price = sum(item['Price__c'] for item in cart_items)
|
551 |
+
discount = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
552 |
|
553 |
if selected_coupon:
|
554 |
+
discount = total_price * 0.10 # Apply 10% discount
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
555 |
|
556 |
total_bill = total_price - discount
|
557 |
|
558 |
+
# ✅ Store **all details** including Add-Ons, Instructions, Price, and Image
|
559 |
+
order_details = "\n".join(
|
560 |
+
f"{item['Name']} x{item['Quantity__c']} | Add-Ons: {item.get('Add_Ons__c', 'None')} | "
|
561 |
+
f"Instructions: {item.get('Instructions__c', 'None')} | "
|
562 |
+
f"Price: ${item['Price__c']} | Image: {item['Image1__c']}"
|
563 |
+
for item in cart_items
|
564 |
+
)
|
565 |
|
566 |
# Store the order details in Order__c
|
567 |
order_data = {
|
|
|
571 |
"Discount__c": discount,
|
572 |
"Total_Bill__c": total_bill,
|
573 |
"Order_Status__c": "Pending",
|
574 |
+
"Order_Details__c": order_details # ✅ Now includes **all details**
|
575 |
}
|
576 |
|
577 |
sf.Order__c.create(order_data)
|
|
|
586 |
print(f"Error during checkout: {str(e)}")
|
587 |
return jsonify({"success": False, "error": str(e)})
|
588 |
|
|
|
589 |
@app.route("/order", methods=["GET"])
|
590 |
def order_summary():
|
591 |
email = session.get('user_email') # Fetch logged-in user's email
|