Spaces:
Sleeping
Sleeping
Update Menu_page.py
Browse files- Menu_page.py +152 -0
Menu_page.py
CHANGED
@@ -132,5 +132,157 @@ def menu():
|
|
132 |
first_letter=first_letter # Pass first letter to the template
|
133 |
)
|
134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
|
|
|
132 |
first_letter=first_letter # Pass first letter to the template
|
133 |
)
|
134 |
|
135 |
+
@menu_page.route('/api/addons', methods=['GET'])
|
136 |
+
def get_addons():
|
137 |
+
item_name = request.args.get('item_name')
|
138 |
+
item_section = request.args.get('item_section')
|
139 |
+
|
140 |
+
# Check if both item_name and item_section are provided
|
141 |
+
if not item_name or not item_section:
|
142 |
+
return jsonify({"success": False, "error": "Item name and section are required."}), 400
|
143 |
+
|
144 |
+
try:
|
145 |
+
# Fetch customization options from Salesforce based on the section
|
146 |
+
query = f"""
|
147 |
+
SELECT Name, Customization_Type__c, Options__c, Max_Selections__c, Extra_Charge__c, Extra_Charge_Amount__c
|
148 |
+
FROM Customization_Options__c
|
149 |
+
WHERE Section__c = '{item_section}'
|
150 |
+
"""
|
151 |
+
result = sf.query(query)
|
152 |
+
addons = result.get('records', [])
|
153 |
+
|
154 |
+
# Check if we found any addons
|
155 |
+
if not addons:
|
156 |
+
return jsonify({"success": False, "error": "No customization options found for the given section."}), 404
|
157 |
+
|
158 |
+
# Format data for frontend
|
159 |
+
formatted_addons = []
|
160 |
+
for addon in addons:
|
161 |
+
# Ensure 'Options__c' exists and is not None
|
162 |
+
options = addon.get("Options__c", "")
|
163 |
+
if options: # If options are available, split them
|
164 |
+
options = options.split(", ") # Convert comma-separated options into a list
|
165 |
+
else:
|
166 |
+
options = [] # If no options, default to an empty list
|
167 |
+
|
168 |
+
formatted_addons.append({
|
169 |
+
"name": addon["Name"],
|
170 |
+
"type": addon["Customization_Type__c"],
|
171 |
+
"options": options,
|
172 |
+
"max_selections": addon.get("Max_Selections__c", 1),
|
173 |
+
"extra_charge": addon.get("Extra_Charge__c", False),
|
174 |
+
"extra_charge_amount": addon.get("Extra_Charge_Amount__c", 0)
|
175 |
+
})
|
176 |
+
|
177 |
+
return jsonify({"success": True, "addons": formatted_addons})
|
178 |
+
|
179 |
+
except Exception as e:
|
180 |
+
# Log the exception for debugging
|
181 |
+
app.logger.error(f"Error fetching addons: {str(e)}")
|
182 |
+
return jsonify({"success": False, "error": "An error occurred while fetching customization options."}), 500
|
183 |
+
|
184 |
+
@menu_page.route('/cart/add', methods=['POST'])
|
185 |
+
def add_to_cart():
|
186 |
+
try:
|
187 |
+
# Get data from request
|
188 |
+
data = request.json
|
189 |
+
item_name = data.get('itemName', '').strip()
|
190 |
+
item_price = data.get('itemPrice')
|
191 |
+
item_image = data.get('itemImage')
|
192 |
+
addons = data.get('addons', [])
|
193 |
+
instructions = data.get('instructions', '')
|
194 |
+
category = data.get('category')
|
195 |
+
section = data.get('section')
|
196 |
+
quantity = data.get('quantity', 1) # Get the quantity field from the request
|
197 |
+
customer_email = session.get('user_email')
|
198 |
+
|
199 |
+
# Basic validation for required fields
|
200 |
+
if not item_name or not item_price:
|
201 |
+
return jsonify({"success": False, "error": "Item name and price are required."}), 400
|
202 |
+
|
203 |
+
if not customer_email:
|
204 |
+
return jsonify({"success": False, "error": "User email is required."}), 400
|
205 |
+
|
206 |
+
# Query to check if the item is already in the cart
|
207 |
+
query = f"""
|
208 |
+
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
209 |
+
FROM Cart_Item__c
|
210 |
+
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
211 |
+
"""
|
212 |
+
result = sf.query(query)
|
213 |
+
cart_items = result.get("records", [])
|
214 |
+
|
215 |
+
# Calculate the total price for the addons
|
216 |
+
addons_price = sum(addon['price'] for addon in addons)
|
217 |
+
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
218 |
+
|
219 |
+
# If the item is already in the cart, update it
|
220 |
+
if cart_items:
|
221 |
+
cart_item_id = cart_items[0]['Id']
|
222 |
+
existing_quantity = cart_items[0]['Quantity__c']
|
223 |
+
existing_addons = cart_items[0].get('Add_Ons__c', "None")
|
224 |
+
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
225 |
+
existing_instructions = cart_items[0].get('Instructions__c', "")
|
226 |
+
|
227 |
+
# Combine the new addons with the existing ones
|
228 |
+
combined_addons = existing_addons if existing_addons != "None" else ""
|
229 |
+
if new_addons:
|
230 |
+
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
231 |
+
|
232 |
+
# Combine existing instructions with new instructions
|
233 |
+
combined_instructions = existing_instructions
|
234 |
+
if instructions:
|
235 |
+
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
236 |
+
|
237 |
+
# Calculate total addons price
|
238 |
+
combined_addons_list = combined_addons.split("; ")
|
239 |
+
combined_addons_price = sum(
|
240 |
+
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
241 |
+
)
|
242 |
+
|
243 |
+
# Update the cart item in Salesforce (updating quantity)
|
244 |
+
sf.Cart_Item__c.update(cart_item_id, {
|
245 |
+
"Quantity__c": existing_quantity + quantity, # Add the selected quantity
|
246 |
+
"Add_Ons__c": combined_addons,
|
247 |
+
"Add_Ons_Price__c": combined_addons_price,
|
248 |
+
"Instructions__c": combined_instructions,
|
249 |
+
"Price__c": (existing_quantity + quantity) * item_price + combined_addons_price,
|
250 |
+
"Category__c": category,
|
251 |
+
"Section__c": section
|
252 |
+
})
|
253 |
+
else:
|
254 |
+
# If the item is not already in the cart, create a new entry
|
255 |
+
addons_string = "None"
|
256 |
+
if addons:
|
257 |
+
addons_string = new_addons
|
258 |
+
|
259 |
+
total_price = item_price * quantity + addons_price # Multiply by the quantity
|
260 |
+
|
261 |
+
# Create new cart item in Salesforce
|
262 |
+
sf.Cart_Item__c.create({
|
263 |
+
"Name": item_name,
|
264 |
+
"Price__c": total_price,
|
265 |
+
"Base_Price__c": item_price,
|
266 |
+
"Quantity__c": quantity, # Use the selected quantity
|
267 |
+
"Add_Ons_Price__c": addons_price,
|
268 |
+
"Add_Ons__c": addons_string,
|
269 |
+
"Image1__c": item_image,
|
270 |
+
"Customer_Email__c": customer_email,
|
271 |
+
"Instructions__c": instructions,
|
272 |
+
"Category__c": category,
|
273 |
+
"Section__c": section
|
274 |
+
})
|
275 |
+
|
276 |
+
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
277 |
+
|
278 |
+
except KeyError as e:
|
279 |
+
# Handle missing expected keys in request data
|
280 |
+
return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400
|
281 |
+
|
282 |
+
except Exception as e:
|
283 |
+
# Log the error for debugging and return a general error message
|
284 |
+
print(f"Error adding item to cart: {str(e)}")
|
285 |
+
return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
|
286 |
+
|
287 |
|
288 |
|