nagasurendra commited on
Commit
d8f0b85
·
verified ·
1 Parent(s): 70c64e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -267,32 +267,36 @@ def get_addons():
267
 
268
  @app.route("/cart/update_quantity", methods=["POST"])
269
  def update_quantity():
270
- data = request.json # Extract JSON data from the request
271
- email = data.get('email') # Customer email
272
- item_name = data.get('item_name') # Item name (Cart Item Name in Salesforce)
273
- quantity = data.get('quantity') # New quantity
 
274
 
275
- # Validate inputs
276
  if not email or not item_name or not quantity:
277
  return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400
278
 
279
  try:
280
- # Query the cart item using the correct field names
281
  cart_items = sf.query(
282
- f"SELECT Id, Quantity__c FROM Cart_Item__c WHERE Customer_Email__c = '{email}' AND Name__c = '{item_name}'"
283
  )['records']
284
 
 
285
  if not cart_items:
286
  return jsonify({"success": False, "error": "Cart item not found."}), 404
287
 
288
- # Get the first matching record ID
289
  cart_item_id = cart_items[0]['Id']
290
 
291
- # Update the quantity in Salesforce
292
  sf.Cart_Item__c.update(cart_item_id, {"Quantity__c": quantity})
293
 
 
294
  return jsonify({"success": True, "new_quantity": quantity})
295
  except Exception as e:
 
296
  return jsonify({"success": False, "error": str(e)}), 500
297
 
298
 
 
267
 
268
  @app.route("/cart/update_quantity", methods=["POST"])
269
  def update_quantity():
270
+ # Step 1: Extract JSON data from the request
271
+ data = request.json
272
+ email = data.get('email') # Get customer's email
273
+ item_name = data.get('item_name') # Get item's name
274
+ quantity = data.get('quantity') # Get the updated quantity
275
 
276
+ # Step 2: Validate the input
277
  if not email or not item_name or not quantity:
278
  return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400
279
 
280
  try:
281
+ # Step 3: Query Salesforce to find the cart item
282
  cart_items = sf.query(
283
+ f"SELECT Id, Quantity__c FROM Cart_Item__c WHERE Customer_Email__c = '{email}' AND Name = '{item_name}'"
284
  )['records']
285
 
286
+ # Step 4: Check if the item exists in the cart
287
  if not cart_items:
288
  return jsonify({"success": False, "error": "Cart item not found."}), 404
289
 
290
+ # Step 5: Get the unique Salesforce ID of the cart item
291
  cart_item_id = cart_items[0]['Id']
292
 
293
+ # Step 6: Update the quantity in Salesforce
294
  sf.Cart_Item__c.update(cart_item_id, {"Quantity__c": quantity})
295
 
296
+ # Step 7: Return a success response
297
  return jsonify({"success": True, "new_quantity": quantity})
298
  except Exception as e:
299
+ # Handle errors and return an error response
300
  return jsonify({"success": False, "error": str(e)}), 500
301
 
302