Update menu.py
Browse files
menu.py
CHANGED
@@ -1,45 +1,27 @@
|
|
1 |
-
from flask import
|
2 |
from salesforce import get_salesforce_connection
|
3 |
import os
|
4 |
|
5 |
-
app = Flask(__name__)
|
6 |
-
app.secret_key = 'your-secret-key-here' # Change for production
|
7 |
-
|
8 |
-
# Initialize Blueprint
|
9 |
menu_blueprint = Blueprint('menu', __name__)
|
10 |
|
11 |
# Initialize Salesforce connection
|
12 |
sf = get_salesforce_connection()
|
13 |
|
14 |
-
#
|
15 |
-
STATIC_DIR = os.path.join(
|
16 |
-
|
17 |
-
# Video configuration
|
18 |
PLACEHOLDER_VIDEO = 'placeholder.mp4'
|
19 |
PLACEHOLDER_PATH = os.path.join(STATIC_DIR, PLACEHOLDER_VIDEO)
|
|
|
20 |
|
21 |
# Create placeholder video at startup if it doesn't exist
|
22 |
if not os.path.exists(PLACEHOLDER_PATH):
|
23 |
open(PLACEHOLDER_PATH, 'wb').close()
|
24 |
print(f"Created placeholder video at {PLACEHOLDER_PATH}")
|
25 |
|
26 |
-
# Menu configuration
|
27 |
-
MENU_CONFIG = {
|
28 |
-
"section_order": ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads",
|
29 |
-
"Customized dish", "Apetizer", "Desserts", "Soft Drinks"],
|
30 |
-
"section_mapping": {
|
31 |
-
"Main Course": "Curries",
|
32 |
-
"Appetizers": "Starters",
|
33 |
-
"Breads": "Breads",
|
34 |
-
"Soft Drinks": "Soft Drinks"
|
35 |
-
},
|
36 |
-
"categories": ["All", "Veg", "Non veg"]
|
37 |
-
}
|
38 |
-
|
39 |
def get_valid_video_path(item_name, video_url=None):
|
40 |
"""
|
41 |
Get valid video path for item with placeholder fallback
|
42 |
-
Priority: 1. Video1__c from Salesforce 2.
|
43 |
"""
|
44 |
# First try: Video1__c from Salesforce if provided
|
45 |
if video_url:
|
@@ -53,11 +35,6 @@ def get_valid_video_path(item_name, video_url=None):
|
|
53 |
elif video_url.startswith('069'):
|
54 |
return f"https://yourdomain.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}"
|
55 |
|
56 |
-
# Second try: Generated filename based on item name
|
57 |
-
video_file = f"{item_name.lower().replace(' ', '_')}.mp4"
|
58 |
-
if os.path.isfile(os.path.join(STATIC_DIR, video_file)):
|
59 |
-
return f"/static/{video_file}"
|
60 |
-
|
61 |
# Final fallback: placeholder.mp4
|
62 |
if not os.path.exists(PLACEHOLDER_PATH):
|
63 |
open(PLACEHOLDER_PATH, 'wb').close()
|
@@ -65,112 +42,113 @@ def get_valid_video_path(item_name, video_url=None):
|
|
65 |
|
66 |
return f"/static/{PLACEHOLDER_VIDEO}"
|
67 |
|
68 |
-
def organize_menu_items(items, selected_category="All"):
|
69 |
-
"""Organize items into the requested section structure with category filtering"""
|
70 |
-
ordered_menu = {section: [] for section in MENU_CONFIG["section_order"]}
|
71 |
-
|
72 |
-
# Filter by category if needed
|
73 |
-
filtered_items = []
|
74 |
-
for item in items:
|
75 |
-
if selected_category == "Veg" and item.get("Veg_NonVeg__c") not in ["Veg", "both"]:
|
76 |
-
continue
|
77 |
-
if selected_category == "Non veg" and item.get("Veg_NonVeg__c") not in ["Non veg", "both"]:
|
78 |
-
continue
|
79 |
-
filtered_items.append(item)
|
80 |
-
|
81 |
-
# Get top 4 best sellers
|
82 |
-
best_sellers = sorted(filtered_items, key=lambda x: x.get("Total_Ordered__c", 0), reverse=True)[:4]
|
83 |
-
if best_sellers:
|
84 |
-
ordered_menu["Best Sellers"] = best_sellers
|
85 |
-
|
86 |
-
# Organize remaining items
|
87 |
-
for item in filtered_items:
|
88 |
-
if item in best_sellers:
|
89 |
-
continue
|
90 |
-
|
91 |
-
section = MENU_CONFIG["section_mapping"].get(
|
92 |
-
item.get("Section__c", "Others"),
|
93 |
-
item.get("Section__c", "Others")
|
94 |
-
)
|
95 |
-
|
96 |
-
if section in ordered_menu:
|
97 |
-
ordered_menu[section].append(item)
|
98 |
-
else:
|
99 |
-
if "Others" not in ordered_menu:
|
100 |
-
ordered_menu["Others"] = []
|
101 |
-
ordered_menu["Others"].append(item)
|
102 |
-
|
103 |
-
# Remove empty sections
|
104 |
-
return {section: items for section, items in ordered_menu.items() if items}
|
105 |
-
|
106 |
@menu_blueprint.route("/menu", methods=["GET", "POST"])
|
107 |
def menu():
|
108 |
-
# User session handling
|
109 |
-
user_email = session.get('user_email') or request.args.get("email")
|
110 |
-
user_name = session.get('user_name') or request.args.get("name")
|
111 |
selected_category = request.args.get("category", "All")
|
112 |
-
|
|
|
113 |
if not user_email:
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
first_letter = user_name[0].upper() if user_name else "A"
|
119 |
|
120 |
try:
|
121 |
-
#
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
WHERE Email__c = '{user_email}'
|
126 |
-
""")
|
127 |
if not user_result['records']:
|
128 |
return redirect(url_for('login'))
|
129 |
-
|
130 |
referral_code = user_result['records'][0].get('Referral__c', 'N/A')
|
131 |
reward_points = user_result['records'][0].get('Reward_Points__c', 0)
|
132 |
-
|
133 |
# Get cart item count
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
# Get menu items including video field
|
141 |
-
food_items = sf.query("""
|
142 |
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
143 |
Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c
|
144 |
FROM Menu_Item__c
|
145 |
-
"""
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
150 |
Veg_NonVeg__c, Section__c, Total_Ordered__c
|
151 |
FROM Custom_Dish__c
|
152 |
WHERE CreatedDate >= LAST_N_DAYS:7
|
153 |
-
"""
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
item
|
161 |
-
)
|
162 |
-
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
except Exception as e:
|
166 |
-
print(f"Error: {str(e)}")
|
167 |
-
# Fallback data
|
168 |
-
ordered_menu = {section: [] for section in
|
169 |
-
referral_code = 'N/A'
|
170 |
-
reward_points = 0
|
171 |
-
cart_count = 0
|
172 |
-
|
173 |
-
# Sample best sellers with video support
|
174 |
best_sellers = ["Chicken Biryani", "Paneer Butter Masala", "Veg Manchurian", "Prawn Fry"]
|
175 |
ordered_menu["Best Sellers"] = [{
|
176 |
"Name": name,
|
@@ -181,105 +159,153 @@ def menu():
|
|
181 |
"Total_Ordered__c": 100,
|
182 |
"Veg_NonVeg__c": "Veg" if "Paneer" in name or "Veg" in name else "Non veg"
|
183 |
} for name in best_sellers]
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
return render_template(
|
186 |
"menu.html",
|
187 |
ordered_menu=ordered_menu,
|
188 |
-
categories=
|
189 |
selected_category=selected_category,
|
190 |
referral_code=referral_code,
|
191 |
reward_points=reward_points,
|
192 |
user_name=user_name,
|
193 |
first_letter=first_letter,
|
194 |
-
cart_item_count=
|
195 |
)
|
196 |
|
197 |
-
# [Rest of your routes remain unchanged...]
|
198 |
@menu_blueprint.route('/api/addons', methods=['GET'])
|
199 |
def get_addons():
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
try:
|
201 |
-
|
202 |
-
|
203 |
-
return jsonify({"success": False, "error": "Section required"}), 400
|
204 |
-
|
205 |
-
addons = sf.query(f"""
|
206 |
-
SELECT Name, Customization_Type__c, Options__c, Max_Selections__c,
|
207 |
-
Extra_Charge__c, Extra_Charge_Amount__c
|
208 |
FROM Customization_Options__c
|
209 |
WHERE Section__c = '{item_section}'
|
210 |
-
"""
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
except Exception as e:
|
225 |
-
|
|
|
226 |
|
227 |
@menu_blueprint.route('/cart/add', methods=['POST'])
|
228 |
def add_to_cart():
|
229 |
try:
|
230 |
data = request.json
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
|
|
|
|
|
|
|
|
235 |
customer_email = session.get('user_email')
|
|
|
|
|
|
|
|
|
236 |
if not customer_email:
|
237 |
-
return jsonify({"success": False, "error": "
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
257 |
)
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
sf.Cart_Item__c.update(existing[0]['Id'], item_data)
|
269 |
else:
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
return jsonify({"success": False, "error": str(e)}), 500
|
276 |
|
277 |
-
|
278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
|
280 |
-
|
281 |
-
def home():
|
282 |
-
return redirect(url_for('menu.menu'))
|
283 |
|
284 |
-
|
285 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for
|
2 |
from salesforce import get_salesforce_connection
|
3 |
import os
|
4 |
|
|
|
|
|
|
|
|
|
5 |
menu_blueprint = Blueprint('menu', __name__)
|
6 |
|
7 |
# Initialize Salesforce connection
|
8 |
sf = get_salesforce_connection()
|
9 |
|
10 |
+
# Constants for video handling
|
11 |
+
STATIC_DIR = os.path.join(os.path.dirname(__file__), 'static')
|
|
|
|
|
12 |
PLACEHOLDER_VIDEO = 'placeholder.mp4'
|
13 |
PLACEHOLDER_PATH = os.path.join(STATIC_DIR, PLACEHOLDER_VIDEO)
|
14 |
+
SECTION_ORDER = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "Customized dish", "Apetizer", "Desserts", "Soft Drinks"]
|
15 |
|
16 |
# Create placeholder video at startup if it doesn't exist
|
17 |
if not os.path.exists(PLACEHOLDER_PATH):
|
18 |
open(PLACEHOLDER_PATH, 'wb').close()
|
19 |
print(f"Created placeholder video at {PLACEHOLDER_PATH}")
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def get_valid_video_path(item_name, video_url=None):
|
22 |
"""
|
23 |
Get valid video path for item with placeholder fallback
|
24 |
+
Priority: 1. Video1__c from Salesforce 2. placeholder.mp4
|
25 |
"""
|
26 |
# First try: Video1__c from Salesforce if provided
|
27 |
if video_url:
|
|
|
35 |
elif video_url.startswith('069'):
|
36 |
return f"https://yourdomain.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}"
|
37 |
|
|
|
|
|
|
|
|
|
|
|
38 |
# Final fallback: placeholder.mp4
|
39 |
if not os.path.exists(PLACEHOLDER_PATH):
|
40 |
open(PLACEHOLDER_PATH, 'wb').close()
|
|
|
42 |
|
43 |
return f"/static/{PLACEHOLDER_VIDEO}"
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
@menu_blueprint.route("/menu", methods=["GET", "POST"])
|
46 |
def menu():
|
|
|
|
|
|
|
47 |
selected_category = request.args.get("category", "All")
|
48 |
+
user_email = session.get('user_email')
|
49 |
+
|
50 |
if not user_email:
|
51 |
+
user_email = request.args.get("email")
|
52 |
+
user_name = request.args.get("name")
|
53 |
+
|
54 |
+
if user_email:
|
55 |
+
session['user_email'] = user_email
|
56 |
+
session['user_name'] = user_name
|
57 |
+
else:
|
58 |
+
return redirect(url_for("login"))
|
59 |
+
else:
|
60 |
+
user_name = session.get('user_name')
|
61 |
+
|
62 |
first_letter = user_name[0].upper() if user_name else "A"
|
63 |
|
64 |
try:
|
65 |
+
# Fetch user referral and reward points
|
66 |
+
user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
67 |
+
user_result = sf.query(user_query)
|
68 |
+
|
|
|
|
|
69 |
if not user_result['records']:
|
70 |
return redirect(url_for('login'))
|
71 |
+
|
72 |
referral_code = user_result['records'][0].get('Referral__c', 'N/A')
|
73 |
reward_points = user_result['records'][0].get('Reward_Points__c', 0)
|
74 |
+
|
75 |
# Get cart item count
|
76 |
+
cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
|
77 |
+
cart_count_result = sf.query(cart_query)
|
78 |
+
cart_item_count = cart_count_result['totalSize']
|
79 |
+
|
80 |
+
# Query to fetch Menu_Item__c records including Video1__c
|
81 |
+
menu_query = """
|
|
|
|
|
82 |
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
83 |
Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c
|
84 |
FROM Menu_Item__c
|
85 |
+
"""
|
86 |
+
result = sf.query(menu_query)
|
87 |
+
food_items = result['records'] if 'records' in result else []
|
88 |
+
|
89 |
+
# Process items and add video paths
|
90 |
+
for item in food_items:
|
91 |
+
if 'Total_Ordered__c' not in item or item['Total_Ordered__c'] is None:
|
92 |
+
item['Total_Ordered__c'] = 0
|
93 |
+
item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
|
94 |
+
|
95 |
+
# Query to fetch Custom_Dish__c records
|
96 |
+
custom_dish_query = """
|
97 |
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
98 |
Veg_NonVeg__c, Section__c, Total_Ordered__c
|
99 |
FROM Custom_Dish__c
|
100 |
WHERE CreatedDate >= LAST_N_DAYS:7
|
101 |
+
"""
|
102 |
+
custom_dish_result = sf.query(custom_dish_query)
|
103 |
+
custom_dishes = custom_dish_result.get('records', [])
|
104 |
+
|
105 |
+
# Process custom dishes and add video paths
|
106 |
+
for item in custom_dishes:
|
107 |
+
if 'Total_Ordered__c' not in item or item['Total_Ordered__c'] is None:
|
108 |
+
item['Total_Ordered__c'] = 0
|
109 |
+
item['Video1__c'] = get_valid_video_path(item['Name'])
|
110 |
+
|
111 |
+
# Merge both Menu_Item__c and Custom_Dish__c records
|
112 |
+
all_items = food_items + custom_dishes
|
113 |
+
ordered_menu = {section: [] for section in SECTION_ORDER}
|
114 |
+
|
115 |
+
# Process best sellers
|
116 |
+
best_sellers = sorted(all_items, key=lambda x: x.get("Total_Ordered__c", 0), reverse=True)
|
117 |
|
118 |
+
if selected_category == "Veg":
|
119 |
+
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
|
120 |
+
elif selected_category == "Non veg":
|
121 |
+
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]
|
122 |
+
|
123 |
+
best_sellers = best_sellers[:4]
|
124 |
+
if best_sellers:
|
125 |
+
ordered_menu["Best Sellers"] = best_sellers
|
126 |
+
|
127 |
+
# Organize other sections
|
128 |
+
added_item_names = set()
|
129 |
+
for item in all_items:
|
130 |
+
section = item.get("Section__c", "Others")
|
131 |
+
if section not in ordered_menu:
|
132 |
+
ordered_menu[section] = []
|
133 |
+
|
134 |
+
if item['Name'] in added_item_names:
|
135 |
+
continue
|
136 |
+
|
137 |
+
if selected_category == "Veg" and item.get("Veg_NonVeg__c") not in ["Veg", "both"]:
|
138 |
+
continue
|
139 |
+
if selected_category == "Non veg" and item.get("Veg_NonVeg__c") not in ["Non veg", "both"]:
|
140 |
+
continue
|
141 |
+
|
142 |
+
ordered_menu[section].append(item)
|
143 |
+
added_item_names.add(item['Name'])
|
144 |
+
|
145 |
+
ordered_menu = {section: items for section, items in ordered_menu.items() if items}
|
146 |
+
categories = ["All", "Veg", "Non veg"]
|
147 |
+
|
148 |
except Exception as e:
|
149 |
+
print(f"Error fetching menu data: {str(e)}")
|
150 |
+
# Fallback data with video support
|
151 |
+
ordered_menu = {section: [] for section in SECTION_ORDER}
|
|
|
|
|
|
|
|
|
|
|
152 |
best_sellers = ["Chicken Biryani", "Paneer Butter Masala", "Veg Manchurian", "Prawn Fry"]
|
153 |
ordered_menu["Best Sellers"] = [{
|
154 |
"Name": name,
|
|
|
159 |
"Total_Ordered__c": 100,
|
160 |
"Veg_NonVeg__c": "Veg" if "Paneer" in name or "Veg" in name else "Non veg"
|
161 |
} for name in best_sellers]
|
162 |
+
|
163 |
+
categories = ["All", "Veg", "Non veg"]
|
164 |
+
referral_code = 'N/A'
|
165 |
+
reward_points = 0
|
166 |
+
cart_item_count = 0
|
167 |
|
168 |
return render_template(
|
169 |
"menu.html",
|
170 |
ordered_menu=ordered_menu,
|
171 |
+
categories=categories,
|
172 |
selected_category=selected_category,
|
173 |
referral_code=referral_code,
|
174 |
reward_points=reward_points,
|
175 |
user_name=user_name,
|
176 |
first_letter=first_letter,
|
177 |
+
cart_item_count=cart_item_count
|
178 |
)
|
179 |
|
|
|
180 |
@menu_blueprint.route('/api/addons', methods=['GET'])
|
181 |
def get_addons():
|
182 |
+
item_name = request.args.get('item_name')
|
183 |
+
item_section = request.args.get('item_section')
|
184 |
+
|
185 |
+
if not item_name or not item_section:
|
186 |
+
return jsonify({"success": False, "error": "Item name and section are required."}), 400
|
187 |
+
|
188 |
try:
|
189 |
+
query = f"""
|
190 |
+
SELECT Name, Customization_Type__c, Options__c, Max_Selections__c, Extra_Charge__c, Extra_Charge_Amount__c
|
|
|
|
|
|
|
|
|
|
|
191 |
FROM Customization_Options__c
|
192 |
WHERE Section__c = '{item_section}'
|
193 |
+
"""
|
194 |
+
result = sf.query(query)
|
195 |
+
addons = result.get('records', [])
|
196 |
+
|
197 |
+
if not addons:
|
198 |
+
return jsonify({"success": False, "error": "No customization options found for the given section."}), 404
|
199 |
+
|
200 |
+
formatted_addons = []
|
201 |
+
for addon in addons:
|
202 |
+
options = addon.get("Options__c", "")
|
203 |
+
if options:
|
204 |
+
options = options.split(", ")
|
205 |
+
else:
|
206 |
+
options = []
|
207 |
+
|
208 |
+
formatted_addons.append({
|
209 |
+
"name": addon["Name"],
|
210 |
+
"type": addon["Customization_Type__c"],
|
211 |
+
"options": options,
|
212 |
+
"max_selections": addon.get("Max_Selections__c", 1),
|
213 |
+
"extra_charge": addon.get("Extra_Charge__c", False),
|
214 |
+
"extra_charge_amount": addon.get("Extra_Charge_Amount__c", 0)
|
215 |
+
})
|
216 |
+
|
217 |
+
return jsonify({"success": True, "addons": formatted_addons})
|
218 |
+
|
219 |
except Exception as e:
|
220 |
+
print(f"Error fetching addons: {str(e)}")
|
221 |
+
return jsonify({"success": False, "error": "An error occurred while fetching customization options."}), 500
|
222 |
|
223 |
@menu_blueprint.route('/cart/add', methods=['POST'])
|
224 |
def add_to_cart():
|
225 |
try:
|
226 |
data = request.json
|
227 |
+
item_name = data.get('itemName', '').strip()
|
228 |
+
item_price = data.get('itemPrice')
|
229 |
+
item_image = data.get('itemImage')
|
230 |
+
addons = data.get('addons', [])
|
231 |
+
instructions = data.get('instructions', '')
|
232 |
+
category = data.get('category')
|
233 |
+
section = data.get('section')
|
234 |
+
quantity = data.get('quantity', 1)
|
235 |
customer_email = session.get('user_email')
|
236 |
+
|
237 |
+
if not item_name or not item_price:
|
238 |
+
return jsonify({"success": False, "error": "Item name and price are required."}), 400
|
239 |
+
|
240 |
if not customer_email:
|
241 |
+
return jsonify({"success": False, "error": "User email is required."}), 400
|
242 |
+
|
243 |
+
query = f"""
|
244 |
+
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
245 |
+
FROM Cart_Item__c
|
246 |
+
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
247 |
+
"""
|
248 |
+
result = sf.query(query)
|
249 |
+
cart_items = result.get("records", [])
|
250 |
+
|
251 |
+
addons_price = sum(addon['price'] for addon in addons)
|
252 |
+
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
253 |
+
|
254 |
+
if cart_items:
|
255 |
+
cart_item_id = cart_items[0]['Id']
|
256 |
+
existing_quantity = cart_items[0]['Quantity__c']
|
257 |
+
existing_addons = cart_items[0].get('Add_Ons__c', "None")
|
258 |
+
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
259 |
+
existing_instructions = cart_items[0].get('Instructions__c', "")
|
260 |
+
|
261 |
+
combined_addons = existing_addons if existing_addons != "None" else ""
|
262 |
+
if new_addons:
|
263 |
+
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
264 |
+
|
265 |
+
combined_instructions = existing_instructions
|
266 |
+
if instructions:
|
267 |
+
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
268 |
+
|
269 |
+
combined_addons_list = combined_addons.split("; ")
|
270 |
+
combined_addons_price = sum(
|
271 |
+
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
272 |
)
|
273 |
+
|
274 |
+
sf.Cart_Item__c.update(cart_item_id, {
|
275 |
+
"Quantity__c": existing_quantity + quantity,
|
276 |
+
"Add_Ons__c": combined_addons,
|
277 |
+
"Add_Ons_Price__c": combined_addons_price,
|
278 |
+
"Instructions__c": combined_instructions,
|
279 |
+
"Price__c": (existing_quantity + quantity) * item_price + combined_addons_price,
|
280 |
+
"Category__c": category,
|
281 |
+
"Section__c": section
|
282 |
+
})
|
|
|
283 |
else:
|
284 |
+
addons_string = "None"
|
285 |
+
if addons:
|
286 |
+
addons_string = new_addons
|
287 |
+
|
288 |
+
total_price = item_price * quantity + addons_price
|
|
|
289 |
|
290 |
+
sf.Cart_Item__c.create({
|
291 |
+
"Name": item_name,
|
292 |
+
"Price__c": total_price,
|
293 |
+
"Base_Price__c": item_price,
|
294 |
+
"Quantity__c": quantity,
|
295 |
+
"Add_Ons_Price__c": addons_price,
|
296 |
+
"Add_Ons__c": addons_string,
|
297 |
+
"Image1__c": item_image,
|
298 |
+
"Customer_Email__c": customer_email,
|
299 |
+
"Instructions__c": instructions,
|
300 |
+
"Category__c": category,
|
301 |
+
"Section__c": section
|
302 |
+
})
|
303 |
|
304 |
+
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
|
|
|
|
305 |
|
306 |
+
except KeyError as e:
|
307 |
+
return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400
|
308 |
+
|
309 |
+
except Exception as e:
|
310 |
+
print(f"Error adding item to cart: {str(e)}")
|
311 |
+
return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
|