nagasurendra commited on
Commit
b25a46a
·
verified ·
1 Parent(s): a0d5227

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -256,21 +256,21 @@ def get_addons():
256
  except Exception as e:
257
  print(f"Error fetching add-ons: {e}")
258
  return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."})
259
- @app.route("/cart/update_quantity", methods=["POST"])
260
  def update_quantity():
261
- data = request.json
262
- print(f"Incoming request data: {data}") # Debug log
263
-
264
- email = data.get('email') # Customer email
265
- item_name = data.get('item_name') # Item name
266
- quantity = data.get('quantity') # New quantity
 
 
267
 
 
268
  if not email or not item_name or quantity is None:
269
  return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400
270
 
271
- # The rest of the logic remains the same
272
-
273
-
274
  try:
275
  # Query the cart item in Salesforce
276
  cart_items = sf.query(
@@ -289,9 +289,12 @@ def update_quantity():
289
  # Calculate the add-ons price
290
  addons_price = 0
291
  if addons_string and addons_string != "None":
292
- addons_price = sum(
293
- [float(price) for price in addons_string.split("$")[1::2]] # Example parsing logic
294
- )
 
 
 
295
 
296
  # Calculate the new price
297
  new_price = (base_price + addons_price) * quantity
@@ -307,7 +310,7 @@ def update_quantity():
307
  print(f"Error updating quantity: {str(e)}")
308
  return jsonify({"success": False, "error": str(e)}), 500
309
 
310
-
311
 
312
  @app.route("/checkout", methods=["POST"])
313
  def checkout():
 
256
  except Exception as e:
257
  print(f"Error fetching add-ons: {e}")
258
  return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."})
259
+ @app.route("/cart/update_quantity", methods=["POST"])
260
  def update_quantity():
261
+ data = request.json # Extract JSON data from the request
262
+ email = data.get('email')
263
+ item_name = data.get('item_name')
264
+ try:
265
+ # Convert quantity to an integer
266
+ quantity = int(data.get('quantity'))
267
+ except (ValueError, TypeError):
268
+ return jsonify({"success": False, "error": "Invalid quantity provided."}), 400
269
 
270
+ # Validate inputs
271
  if not email or not item_name or quantity is None:
272
  return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400
273
 
 
 
 
274
  try:
275
  # Query the cart item in Salesforce
276
  cart_items = sf.query(
 
289
  # Calculate the add-ons price
290
  addons_price = 0
291
  if addons_string and addons_string != "None":
292
+ try:
293
+ addons_price = sum(
294
+ [float(price.strip()) for price in addons_string.split("$")[1::2] if price.strip().replace('.', '', 1).isdigit()]
295
+ )
296
+ except ValueError:
297
+ addons_price = 0 # Default to 0 if parsing fails
298
 
299
  # Calculate the new price
300
  new_price = (base_price + addons_price) * quantity
 
310
  print(f"Error updating quantity: {str(e)}")
311
  return jsonify({"success": False, "error": str(e)}), 500
312
 
313
+
314
 
315
  @app.route("/checkout", methods=["POST"])
316
  def checkout():