Spaces:
Runtime error
Runtime error
# views/cart_page.py | |
from flask import render_template, session, redirect, url_for | |
import salesforce # Make sure you have your Salesforce connection here | |
def cart_page(): | |
email = session.get('user_email') | |
if not email: | |
return redirect(url_for("login")) | |
try: | |
# Fetch cart items with Category and Section | |
result = salesforce.sf.query(f""" | |
SELECT Name, Price__c, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Image1__c, Instructions__c, Category__c, Section__c | |
FROM Cart_Item__c | |
WHERE Customer_Email__c = '{email}' | |
""") | |
cart_items = result.get("records", []) | |
subtotal = sum(item['Price__c'] for item in cart_items) | |
# Fetch reward points | |
customer_result = salesforce.sf.query(f""" | |
SELECT Reward_Points__c | |
FROM Customer_Login__c | |
WHERE Email__c = '{email}' | |
""") | |
reward_points = customer_result['records'][0].get('Reward_Points__c', 0) if customer_result['records'] else 0 | |
# Fetch coupons for the user | |
coupon_result = salesforce.sf.query(f""" | |
SELECT Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}' | |
""") | |
coupons = [] | |
if coupon_result["records"]: | |
raw_coupons = coupon_result["records"][0].get("Coupon_Code__c", "") | |
coupons = raw_coupons.split("\n") if raw_coupons else [] | |
# Initialize suggestions as an empty list | |
suggestions = [] | |
# If there are items in the cart, fetch suggestions | |
if cart_items: | |
first_item = cart_items[0] | |
item_category = first_item.get('Category__c', 'All') # Default to 'All' if not found | |
item_section = first_item.get('Section__c', 'Biryanis') # Default to 'Biryanis' if not found | |
# Define section-to-complementary section mapping | |
complementary_sections = { | |
'Breads': ['Curries', 'Biryanis', 'Starters'], | |
'Biryanis': ['Curries', 'Starters', 'Desserts'], | |
'Curries': ['Biryanis', 'Breads', 'Starters'], | |
'Starters': ['Biryanis', 'Curries', 'Desserts'], | |
'Desserts': ['Biryanis', 'Curries', 'Soft Drinks'], | |
'Soft Drinks': ['Starters', 'Biryanis', 'Curries'] | |
} | |
suggested_sections = complementary_sections.get(item_section, []) | |
try: | |
for suggested_section in suggested_sections: | |
if item_category == "All": | |
query = f""" | |
SELECT Name, Price__c, Image1__c | |
FROM Menu_Item__c | |
WHERE Section__c = '{suggested_section}' | |
AND (Veg_NonVeg__c = 'Veg' OR Veg_NonVeg__c = 'Non veg') | |
LIMIT 4 | |
""" | |
else: | |
query = f""" | |
SELECT Name, Price__c, Image1__c | |
FROM Menu_Item__c | |
WHERE Section__c = '{suggested_section}' | |
AND Veg_NonVeg__c = '{item_category}' | |
LIMIT 4 | |
""" | |
suggestion_result = salesforce.sf.query(query) | |
suggestions.extend(suggestion_result.get("records", [])) | |
if len(suggestions) > 4: | |
suggestions = suggestions[:4] | |
except Exception as e: | |
print(f"Error fetching suggestions: {e}") | |
return render_template( | |
"cart.html", | |
cart_items=cart_items, | |
subtotal=subtotal, | |
reward_points=reward_points, | |
customer_email=email, | |
coupons=coupons, | |
suggestions=suggestions | |
) | |
except Exception as e: | |
print(f"Error fetching cart items: {e}") | |
return render_template("cart.html", cart_items=[], subtotal=0, reward_points=0, coupons=[], suggestions=[]) | |