nagasurendra commited on
Commit
20f0433
·
verified ·
1 Parent(s): 3e71efa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -37
app.py CHANGED
@@ -365,19 +365,20 @@ def cart():
365
 
366
  @app.route('/cart/add', methods=['POST'])
367
  def add_to_cart():
368
- data = request.json # Extract JSON payload from frontend
369
- item_name = data.get('itemName').strip() # Item name
370
- item_price = data.get('itemPrice') # Base price of the item
371
- item_image = data.get('itemImage') # Item image
372
- addons = data.get('addons', []) # Add-ons array
373
- instructions = data.get('instructions', '') # Special instructions
374
- customer_email = session.get('user_email') # Get logged-in user's email
 
 
375
 
376
  if not item_name or not item_price:
377
  return jsonify({"success": False, "error": "Item name and price are required."})
378
 
379
  try:
380
- # Query the cart to check if the item already exists
381
  query = f"""
382
  SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c FROM Cart_Item__c
383
  WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
@@ -385,61 +386,57 @@ def add_to_cart():
385
  result = sf.query(query)
386
  cart_items = result.get("records", [])
387
 
388
- # Calculate the price of the new add-ons
389
- addons_price = sum(addon['price'] for addon in addons) # New add-ons price
390
- new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons]) # Format new add-ons
391
 
392
  if cart_items:
393
- # If the item already exists in the cart, update it
394
  cart_item_id = cart_items[0]['Id']
395
  existing_quantity = cart_items[0]['Quantity__c']
396
- existing_addons = cart_items[0].get('Add_Ons__c', "None") # Previous add-ons
397
- existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0) # Previous add-ons price
398
- existing_instructions = cart_items[0].get('Instructions__c', "") # Previous instructions
399
 
400
- # Combine the existing and new add-ons
401
  combined_addons = existing_addons if existing_addons != "None" else ""
402
  if new_addons:
403
  combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
404
 
405
- # Combine the existing and new instructions
406
  combined_instructions = existing_instructions
407
  if instructions:
408
  combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
409
 
410
- # Recalculate the total add-ons price
411
  combined_addons_list = combined_addons.split("; ")
412
  combined_addons_price = sum(
413
  float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
414
  )
415
 
416
- # Update the item in the cart
417
  sf.Cart_Item__c.update(cart_item_id, {
418
- "Quantity__c": existing_quantity + 1, # Increase quantity by 1
419
- "Add_Ons__c": combined_addons, # Update add-ons list
420
- "Add_Ons_Price__c": combined_addons_price, # Update add-ons price
421
- "Instructions__c": combined_instructions, # Update instructions
422
- "Price__c": (existing_quantity + 1) * item_price + combined_addons_price, # Update total price
 
 
423
  })
424
  else:
425
- # If the item does not exist in the cart, create a new one
426
  addons_string = "None"
427
  if addons:
428
- addons_string = new_addons # Use the formatted add-ons string
429
 
430
- total_price = item_price + addons_price # Base price + add-ons price
431
 
432
- # Create a new cart item
433
  sf.Cart_Item__c.create({
434
- "Name": item_name, # Item name
435
- "Price__c": total_price, # Total price (item + add-ons)
436
- "Base_Price__c": item_price, # Base price without add-ons
437
- "Quantity__c": 1, # Default quantity is 1
438
- "Add_Ons_Price__c": addons_price, # Total add-ons price
439
- "Add_Ons__c": addons_string, # Add-ons with names and prices
440
- "Image1__c": item_image, # Item image URL
441
- "Customer_Email__c": customer_email, # Associated customer's email
442
- "Instructions__c": instructions # Save instructions
 
 
443
  })
444
 
445
  return jsonify({"success": True, "message": "Item added to cart successfully."})
 
365
 
366
  @app.route('/cart/add', methods=['POST'])
367
  def add_to_cart():
368
+ data = request.json
369
+ item_name = data.get('itemName').strip()
370
+ item_price = data.get('itemPrice')
371
+ item_image = data.get('itemImage')
372
+ addons = data.get('addons', [])
373
+ instructions = data.get('instructions', '')
374
+ category = data.get('category')
375
+ section = data.get('section')
376
+ customer_email = session.get('user_email')
377
 
378
  if not item_name or not item_price:
379
  return jsonify({"success": False, "error": "Item name and price are required."})
380
 
381
  try:
 
382
  query = f"""
383
  SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c FROM Cart_Item__c
384
  WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
 
386
  result = sf.query(query)
387
  cart_items = result.get("records", [])
388
 
389
+ addons_price = sum(addon['price'] for addon in addons)
390
+ new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
 
391
 
392
  if cart_items:
 
393
  cart_item_id = cart_items[0]['Id']
394
  existing_quantity = cart_items[0]['Quantity__c']
395
+ existing_addons = cart_items[0].get('Add_Ons__c', "None")
396
+ existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
397
+ existing_instructions = cart_items[0].get('Instructions__c', "")
398
 
 
399
  combined_addons = existing_addons if existing_addons != "None" else ""
400
  if new_addons:
401
  combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
402
 
 
403
  combined_instructions = existing_instructions
404
  if instructions:
405
  combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
406
 
 
407
  combined_addons_list = combined_addons.split("; ")
408
  combined_addons_price = sum(
409
  float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
410
  )
411
 
 
412
  sf.Cart_Item__c.update(cart_item_id, {
413
+ "Quantity__c": existing_quantity + 1,
414
+ "Add_Ons__c": combined_addons,
415
+ "Add_Ons_Price__c": combined_addons_price,
416
+ "Instructions__c": combined_instructions,
417
+ "Price__c": (existing_quantity + 1) * item_price + combined_addons_price,
418
+ "Category__c": category,
419
+ "Section__c": section
420
  })
421
  else:
 
422
  addons_string = "None"
423
  if addons:
424
+ addons_string = new_addons
425
 
426
+ total_price = item_price + addons_price
427
 
 
428
  sf.Cart_Item__c.create({
429
+ "Name": item_name,
430
+ "Price__c": total_price,
431
+ "Base_Price__c": item_price,
432
+ "Quantity__c": 1,
433
+ "Add_Ons_Price__c": addons_price,
434
+ "Add_Ons__c": addons_string,
435
+ "Image1__c": item_image,
436
+ "Customer_Email__c": customer_email,
437
+ "Instructions__c": instructions,
438
+ "Category__c": category,
439
+ "Section__c": section
440
  })
441
 
442
  return jsonify({"success": True, "message": "Item added to cart successfully."})