nagasurendra commited on
Commit
61b8aca
·
verified ·
1 Parent(s): e433df0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -43
app.py CHANGED
@@ -165,68 +165,54 @@ def add_to_cart():
165
  addons = data.get('addons', [])
166
  customer_email = session.get('user_email') # Get logged-in user's email from session
167
 
 
168
  if not item_name or not item_price:
169
  return jsonify({"success": False, "error": "Item name and price are required."})
170
 
171
  try:
172
- # Check if the item already exists in the cart for this custpmer
173
- query= f""" SELECT Id, Quantity__c FROM Cart_Item__c
174
- WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
175
- """
176
- result = sf.query(query)
177
- if result['totalSize'] > 0:
178
- # Update quantity if the item exists
179
- cart_item = result['records'][0]
180
- sf.Cart_Item__c.update(cart_item['Id'], {
181
- "Quantity__c": cart_item['Quantity__c'] + 1
182
- })
183
- else:
184
- # Add a new item if it doesn't exist
185
- sf.Cart_Item__c.create({
186
- "Name": item_name,
187
- "Price__c": item_price,
188
- "Quantity__c": 1,
189
- "Add_Ons__c": ";".join(addons) if addons and isinstance(addons, list) else "None",
190
- "Image1__c": item_image,
191
  "Customer_Email__c": customer_email, # Associate with the logged-in user
192
-
193
- })
194
- return jsonify({"success": True, "message": "Item added/updated successfully."})
195
  except Exception as e:
 
196
  return jsonify({"success": False, "error": str(e)})
197
 
 
198
  @app.route("/cart/add_item", methods=["POST"])
199
  def add_item_to_cart():
200
  data = request.json # Extract JSON data from the request
201
  email = data.get('email') # Customer email
202
  item_name = data.get('item_name') # Item name
203
- quantity = data.get('quantity', 0) # Quantity to add (default is 1) // default value is 1
204
-
205
- try:
206
- # Check if the item already exists in the cart for this customer
207
- cart_items = sf.query(
208
- f"SELECT Id, Quantity__c FROM Cart_Item__c WHERE Customer_Email__c = '{email}' AND Item_Name__c = '{item_name}'"
209
- )['records']
210
 
211
- if cart_items:
212
- # If the item already exists, update its quantity
213
- cart_item = cart_items[0]
214
- new_quantity = cart_item['Quantity__c'] + quantity
215
- sf.Cart_Item__c.update(cart_item['Id'], {"Quantity__c": new_quantity})
216
- return jsonify({"success": True, "message": "Item quantity updated successfully."})
217
- else:
218
- # If the item does not exist, add it to the cart
219
- sf.Cart_Item__c.create({
220
- "Customer_Email__c": email,
221
- "Item_Name__c": item_name,
222
- "Quantity__c": quantity
223
- })
224
 
225
- return jsonify({"success": True, "message": "Item added/updated successfully."})
 
 
 
 
 
 
 
 
 
226
  except Exception as e:
 
227
  return jsonify({"success": False, "error": str(e)}), 500
228
 
229
 
 
230
  @app.route('/cart/remove/<item_name>', methods=['POST'])
231
  def remove_cart_item(item_name):
232
  try:
 
165
  addons = data.get('addons', [])
166
  customer_email = session.get('user_email') # Get logged-in user's email from session
167
 
168
+ # Validate the required fields
169
  if not item_name or not item_price:
170
  return jsonify({"success": False, "error": "Item name and price are required."})
171
 
172
  try:
173
+ # Add a new item to the cart with the provided details
174
+ sf.Cart_Item__c.create({
175
+ "Name": item_name, # Item name
176
+ "Price__c": item_price, # Item price
177
+ "Quantity__c": 1, # Always add as a new entry with quantity 1
178
+ "Add_Ons__c": ";".join(addons) if addons and isinstance(addons, list) else "None", # Convert add-ons to a string
179
+ "Image1__c": item_image, # Item image
 
 
 
 
 
 
 
 
 
 
 
 
180
  "Customer_Email__c": customer_email, # Associate with the logged-in user
181
+ })
182
+ return jsonify({"success": True, "message": "Item added to cart successfully."})
 
183
  except Exception as e:
184
+ print(f"Error adding item to cart: {str(e)}") # Log the error for debugging
185
  return jsonify({"success": False, "error": str(e)})
186
 
187
+
188
  @app.route("/cart/add_item", methods=["POST"])
189
  def add_item_to_cart():
190
  data = request.json # Extract JSON data from the request
191
  email = data.get('email') # Customer email
192
  item_name = data.get('item_name') # Item name
193
+ quantity = data.get('quantity', 1) # Quantity to add (default is 1)
194
+ addons = data.get('addons', []) # Add-ons for the item (optional)
 
 
 
 
 
195
 
196
+ # Validate inputs
197
+ if not email or not item_name:
198
+ return jsonify({"success": False, "error": "Email and item name are required."}), 400
 
 
 
 
 
 
 
 
 
 
199
 
200
+ try:
201
+ # Add a new item to the cart with the provided details
202
+ sf.Cart_Item__c.create({
203
+ "Customer_Email__c": email, # Associate the cart item with the customer's email
204
+ "Item_Name__c": item_name, # Item name
205
+ "Quantity__c": quantity, # Quantity to add
206
+ "Add_Ons__c": ";".join(addons) if addons and isinstance(addons, list) else "None" # Add-ons (if any)
207
+ })
208
+
209
+ return jsonify({"success": True, "message": "Item added to cart successfully."})
210
  except Exception as e:
211
+ print(f"Error adding item to cart: {str(e)}") # Log the error for debugging
212
  return jsonify({"success": False, "error": str(e)}), 500
213
 
214
 
215
+
216
  @app.route('/cart/remove/<item_name>', methods=['POST'])
217
  def remove_cart_item(item_name):
218
  try: