Update app.py
Browse files
app.py
CHANGED
|
@@ -586,9 +586,53 @@ def checkout():
|
|
| 586 |
|
| 587 |
total_price = sum(item['Price__c'] for item in cart_items)
|
| 588 |
discount = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 589 |
|
| 590 |
if selected_coupon:
|
| 591 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 592 |
|
| 593 |
total_bill = total_price - discount
|
| 594 |
|
|
|
|
| 586 |
|
| 587 |
total_price = sum(item['Price__c'] for item in cart_items)
|
| 588 |
discount = 0
|
| 589 |
+
# Fetch the user's existing coupons
|
| 590 |
+
coupon_query = sf.query(f"""
|
| 591 |
+
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
| 592 |
+
""")
|
| 593 |
+
|
| 594 |
+
has_coupons = bool(coupon_query["records"]) # Check if user has any coupons
|
| 595 |
|
| 596 |
if selected_coupon:
|
| 597 |
+
# Case 3: User selected a valid coupon → Apply discount & remove coupon
|
| 598 |
+
discount = total_price * 0.10 # 10% discount
|
| 599 |
+
referral_coupon_id = coupon_query["records"][0]["Id"]
|
| 600 |
+
existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")
|
| 601 |
+
|
| 602 |
+
# Remove only the selected coupon
|
| 603 |
+
updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
|
| 604 |
+
|
| 605 |
+
# Convert list back to a string with newlines
|
| 606 |
+
updated_coupons_str = "\n".join(updated_coupons).strip()
|
| 607 |
+
|
| 608 |
+
# Update the Referral_Coupon__c record with the remaining coupons
|
| 609 |
+
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
| 610 |
+
"Coupon_Code__c": updated_coupons_str
|
| 611 |
+
})
|
| 612 |
+
else:
|
| 613 |
+
# Case 1 & Case 2: User has no coupons or has coupons but didn’t select one → Add 10% to reward points
|
| 614 |
+
reward_points_to_add = total_price * 0.10
|
| 615 |
+
|
| 616 |
+
# Fetch current reward points
|
| 617 |
+
customer_record = sf.query(f"""
|
| 618 |
+
SELECT Id, Reward_Points__c FROM Customer_Login__c
|
| 619 |
+
WHERE Email__c = '{email}'
|
| 620 |
+
""")
|
| 621 |
+
customer = customer_record.get("records", [])[0] if customer_record else None
|
| 622 |
+
|
| 623 |
+
if customer:
|
| 624 |
+
current_reward_points = customer.get("Reward_Points__c") or 0
|
| 625 |
+
new_reward_points = current_reward_points + reward_points_to_add
|
| 626 |
+
|
| 627 |
+
print(f"Updating reward points: Current = {current_reward_points}, Adding = {reward_points_to_add}, New = {new_reward_points}")
|
| 628 |
+
|
| 629 |
+
# Update reward points in Salesforce
|
| 630 |
+
sf.Customer_Login__c.update(customer["Id"], {
|
| 631 |
+
"Reward_Points__c": new_reward_points
|
| 632 |
+
})
|
| 633 |
+
print(f"Successfully updated reward points for {email}")
|
| 634 |
+
|
| 635 |
+
|
| 636 |
|
| 637 |
total_bill = total_price - discount
|
| 638 |
|