Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -565,6 +565,109 @@ def cart():
|
|
565 |
except Exception as e:
|
566 |
print(f"Error fetching cart items: {e}")
|
567 |
return render_template("cart.html", cart_items=[], subtotal=0, reward_points=0, coupons=[], suggestions=[])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
568 |
|
569 |
|
570 |
@app.route("/cart/add_suggestion_to_cart", methods=["POST"])
|
|
|
565 |
except Exception as e:
|
566 |
print(f"Error fetching cart items: {e}")
|
567 |
return render_template("cart.html", cart_items=[], subtotal=0, reward_points=0, coupons=[], suggestions=[])
|
568 |
+
|
569 |
+
@app.route('/cart/add', methods=['POST'])
|
570 |
+
def add_to_cart():
|
571 |
+
try:
|
572 |
+
# Get data from request
|
573 |
+
data = request.json
|
574 |
+
item_name = data.get('itemName', '').strip()
|
575 |
+
item_price = data.get('itemPrice')
|
576 |
+
item_image = data.get('itemImage')
|
577 |
+
addons = data.get('addons', [])
|
578 |
+
instructions = data.get('instructions', '')
|
579 |
+
category = data.get('category')
|
580 |
+
section = data.get('section')
|
581 |
+
quantity = data.get('quantity', 1) # Get the quantity field from the request
|
582 |
+
customer_email = session.get('user_email')
|
583 |
+
|
584 |
+
# Basic validation for required fields
|
585 |
+
if not item_name or not item_price:
|
586 |
+
return jsonify({"success": False, "error": "Item name and price are required."}), 400
|
587 |
+
|
588 |
+
if not customer_email:
|
589 |
+
return jsonify({"success": False, "error": "User email is required."}), 400
|
590 |
+
|
591 |
+
# Query to check if the item is already in the cart
|
592 |
+
query = f"""
|
593 |
+
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
594 |
+
FROM Cart_Item__c
|
595 |
+
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
596 |
+
"""
|
597 |
+
result = sf.query(query)
|
598 |
+
cart_items = result.get("records", [])
|
599 |
+
|
600 |
+
# Calculate the total price for the addons
|
601 |
+
addons_price = sum(addon['price'] for addon in addons)
|
602 |
+
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
603 |
+
|
604 |
+
# If the item is already in the cart, update it
|
605 |
+
if cart_items:
|
606 |
+
cart_item_id = cart_items[0]['Id']
|
607 |
+
existing_quantity = cart_items[0]['Quantity__c']
|
608 |
+
existing_addons = cart_items[0].get('Add_Ons__c', "None")
|
609 |
+
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
610 |
+
existing_instructions = cart_items[0].get('Instructions__c', "")
|
611 |
+
|
612 |
+
# Combine the new addons with the existing ones
|
613 |
+
combined_addons = existing_addons if existing_addons != "None" else ""
|
614 |
+
if new_addons:
|
615 |
+
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
616 |
+
|
617 |
+
# Combine existing instructions with new instructions
|
618 |
+
combined_instructions = existing_instructions
|
619 |
+
if instructions:
|
620 |
+
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
621 |
+
|
622 |
+
# Calculate total addons price
|
623 |
+
combined_addons_list = combined_addons.split("; ")
|
624 |
+
combined_addons_price = sum(
|
625 |
+
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
626 |
+
)
|
627 |
+
|
628 |
+
# Update the cart item in Salesforce (updating quantity)
|
629 |
+
sf.Cart_Item__c.update(cart_item_id, {
|
630 |
+
"Quantity__c": existing_quantity + quantity, # Add the selected quantity
|
631 |
+
"Add_Ons__c": combined_addons,
|
632 |
+
"Add_Ons_Price__c": combined_addons_price,
|
633 |
+
"Instructions__c": combined_instructions,
|
634 |
+
"Price__c": (existing_quantity + quantity) * item_price + combined_addons_price,
|
635 |
+
"Category__c": category,
|
636 |
+
"Section__c": section
|
637 |
+
})
|
638 |
+
else:
|
639 |
+
# If the item is not already in the cart, create a new entry
|
640 |
+
addons_string = "None"
|
641 |
+
if addons:
|
642 |
+
addons_string = new_addons
|
643 |
+
|
644 |
+
total_price = item_price * quantity + addons_price # Multiply by the quantity
|
645 |
+
|
646 |
+
# Create new cart item in Salesforce
|
647 |
+
sf.Cart_Item__c.create({
|
648 |
+
"Name": item_name,
|
649 |
+
"Price__c": total_price,
|
650 |
+
"Base_Price__c": item_price,
|
651 |
+
"Quantity__c": quantity, # Use the selected quantity
|
652 |
+
"Add_Ons_Price__c": addons_price,
|
653 |
+
"Add_Ons__c": addons_string,
|
654 |
+
"Image1__c": item_image,
|
655 |
+
"Customer_Email__c": customer_email,
|
656 |
+
"Instructions__c": instructions,
|
657 |
+
"Category__c": category,
|
658 |
+
"Section__c": section
|
659 |
+
})
|
660 |
+
|
661 |
+
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
662 |
+
|
663 |
+
except KeyError as e:
|
664 |
+
# Handle missing expected keys in request data
|
665 |
+
return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400
|
666 |
+
|
667 |
+
except Exception as e:
|
668 |
+
# Log the error for debugging and return a general error message
|
669 |
+
print(f"Error adding item to cart: {str(e)}")
|
670 |
+
return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
|
671 |
|
672 |
|
673 |
@app.route("/cart/add_suggestion_to_cart", methods=["POST"])
|