DSatishchandra commited on
Commit
0ab8ae7
·
verified ·
1 Parent(s): bfe9487

Update Menu_page.py

Browse files
Files changed (1) hide show
  1. Menu_page.py +0 -101
Menu_page.py CHANGED
@@ -132,106 +132,5 @@ def menu():
132
  first_letter=first_letter # Pass first letter to the template
133
  )
134
 
135
- @menu_page.route('/cart/add', methods=['POST'])
136
- def add_to_cart():
137
- try:
138
- # Get data from request
139
- data = request.json
140
- item_name = data.get('itemName', '').strip()
141
- item_price = data.get('itemPrice')
142
- item_image = data.get('itemImage')
143
- addons = data.get('addons', [])
144
- instructions = data.get('instructions', '')
145
- category = data.get('category')
146
- section = data.get('section')
147
- quantity = data.get('quantity', 1) # Get the quantity field from the request
148
- customer_email = session.get('user_email')
149
-
150
- # Basic validation for required fields
151
- if not item_name or not item_price:
152
- return jsonify({"success": False, "error": "Item name and price are required."}), 400
153
-
154
- if not customer_email:
155
- return jsonify({"success": False, "error": "User email is required."}), 400
156
-
157
- # Query to check if the item is already in the cart
158
- query = f"""
159
- SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
160
- FROM Cart_Item__c
161
- WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
162
- """
163
- result = sf.query(query)
164
- cart_items = result.get("records", [])
165
-
166
- # Calculate the total price for the addons
167
- addons_price = sum(addon['price'] for addon in addons)
168
- new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
169
-
170
- # If the item is already in the cart, update it
171
- if cart_items:
172
- cart_item_id = cart_items[0]['Id']
173
- existing_quantity = cart_items[0]['Quantity__c']
174
- existing_addons = cart_items[0].get('Add_Ons__c', "None")
175
- existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
176
- existing_instructions = cart_items[0].get('Instructions__c', "")
177
-
178
- # Combine the new addons with the existing ones
179
- combined_addons = existing_addons if existing_addons != "None" else ""
180
- if new_addons:
181
- combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
182
-
183
- # Combine existing instructions with new instructions
184
- combined_instructions = existing_instructions
185
- if instructions:
186
- combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
187
-
188
- # Calculate total addons price
189
- combined_addons_list = combined_addons.split("; ")
190
- combined_addons_price = sum(
191
- float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
192
- )
193
-
194
- # Update the cart item in Salesforce (updating quantity)
195
- sf.Cart_Item__c.update(cart_item_id, {
196
- "Quantity__c": existing_quantity + quantity, # Add the selected quantity
197
- "Add_Ons__c": combined_addons,
198
- "Add_Ons_Price__c": combined_addons_price,
199
- "Instructions__c": combined_instructions,
200
- "Price__c": (existing_quantity + quantity) * item_price + combined_addons_price,
201
- "Category__c": category,
202
- "Section__c": section
203
- })
204
- else:
205
- # If the item is not already in the cart, create a new entry
206
- addons_string = "None"
207
- if addons:
208
- addons_string = new_addons
209
-
210
- total_price = item_price * quantity + addons_price # Multiply by the quantity
211
-
212
- # Create new cart item in Salesforce
213
- sf.Cart_Item__c.create({
214
- "Name": item_name,
215
- "Price__c": total_price,
216
- "Base_Price__c": item_price,
217
- "Quantity__c": quantity, # Use the selected quantity
218
- "Add_Ons_Price__c": addons_price,
219
- "Add_Ons__c": addons_string,
220
- "Image1__c": item_image,
221
- "Customer_Email__c": customer_email,
222
- "Instructions__c": instructions,
223
- "Category__c": category,
224
- "Section__c": section
225
- })
226
-
227
- return jsonify({"success": True, "message": "Item added to cart successfully."})
228
-
229
- except KeyError as e:
230
- # Handle missing expected keys in request data
231
- return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400
232
 
233
- except Exception as e:
234
- # Log the error for debugging and return a general error message
235
- print(f"Error adding item to cart: {str(e)}")
236
- return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
237
 
 
132
  first_letter=first_letter # Pass first letter to the template
133
  )
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
 
 
 
 
136