nagasurendra commited on
Commit
8945517
·
verified ·
1 Parent(s): 574fabb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py CHANGED
@@ -378,6 +378,84 @@ def cart():
378
  return render_template("cart.html", cart_items=[], subtotal=0, reward_points=0, coupons=[], suggestions=[])
379
 
380
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
381
 
382
  @app.route('/cart/add', methods=['POST'])
383
  def add_to_cart():
 
378
  return render_template("cart.html", cart_items=[], subtotal=0, reward_points=0, coupons=[], suggestions=[])
379
 
380
 
381
+ @app.route("/cart/add_suggestion_to_cart", methods=["POST"])
382
+ def add_suggestion_to_cart():
383
+ try:
384
+ # Get data from the request
385
+ data = request.get_json()
386
+ item_name = data.get('item_name').strip()
387
+ item_price = data.get('item_price')
388
+ item_image = data.get('item_image')
389
+ item_id = data.get('item_id')
390
+ customer_email = data.get('customer_email')
391
+ addons = data.get('addons', [])
392
+ instructions = data.get('instructions', "")
393
+
394
+ # Default values if addons and instructions are not provided
395
+ addons_price = 0
396
+ addons_string = "None"
397
+
398
+ # Check if the customer already has this item in their cart
399
+ query = f"""
400
+ SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
401
+ FROM Cart_Item__c
402
+ WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
403
+ """
404
+ result = sf.query(query)
405
+ cart_items = result.get("records", [])
406
+
407
+ # If item already exists in the cart, update its quantity and other details
408
+ if cart_items:
409
+ cart_item_id = cart_items[0]['Id']
410
+ existing_quantity = cart_items[0]['Quantity__c']
411
+ existing_addons = cart_items[0].get('Add_Ons__c', "None")
412
+ existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
413
+ existing_instructions = cart_items[0].get('Instructions__c', "")
414
+
415
+ # Combine existing and new addons
416
+ combined_addons = existing_addons if existing_addons != "None" else ""
417
+ if addons:
418
+ combined_addons = f"{combined_addons}; {addons}".strip("; ")
419
+
420
+ combined_instructions = existing_instructions
421
+ if instructions:
422
+ combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
423
+
424
+ combined_addons_list = combined_addons.split("; ")
425
+ combined_addons_price = sum(
426
+ float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
427
+ )
428
+
429
+ # Update the cart item
430
+ sf.Cart_Item__c.update(cart_item_id, {
431
+ "Quantity__c": existing_quantity + 1,
432
+ "Add_Ons__c": combined_addons,
433
+ "Add_Ons_Price__c": combined_addons_price,
434
+ "Instructions__c": combined_instructions,
435
+ "Price__c": (existing_quantity + 1) * float(item_price) + combined_addons_price
436
+ })
437
+ else:
438
+ # If item doesn't exist in cart, create a new cart item
439
+ total_price = float(item_price) + addons_price
440
+
441
+ # Create a new cart item in Salesforce
442
+ sf.Cart_Item__c.create({
443
+ "Name": item_name,
444
+ "Price__c": total_price,
445
+ "Base_Price__c": item_price,
446
+ "Quantity__c": 1,
447
+ "Add_Ons_Price__c": addons_price,
448
+ "Add_Ons__c": addons_string,
449
+ "Image1__c": item_image,
450
+ "Customer_Email__c": customer_email,
451
+ "Instructions__c": instructions
452
+ })
453
+
454
+ return jsonify({"success": True, "message": "Item added to cart successfully."})
455
+
456
+ except Exception as e:
457
+ print(f"Error adding item to cart: {str(e)}")
458
+ return jsonify({"success": False, "error": str(e)})
459
 
460
  @app.route('/cart/add', methods=['POST'])
461
  def add_to_cart():