Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -768,7 +768,7 @@ def add_suggestion_to_cart():
|
|
768 |
@app.route('/cart/add', methods=['POST'])
|
769 |
def add_to_cart():
|
770 |
data = request.json
|
771 |
-
item_name = data.get('itemName').strip()
|
772 |
item_price = data.get('itemPrice')
|
773 |
item_image = data.get('itemImage')
|
774 |
addons = data.get('addons', [])
|
@@ -777,20 +777,28 @@ def add_to_cart():
|
|
777 |
section = data.get('section')
|
778 |
customer_email = session.get('user_email')
|
779 |
|
|
|
780 |
if not item_name or not item_price:
|
781 |
-
return jsonify({"success": False, "error": "Item name and price are required."})
|
|
|
|
|
|
|
782 |
|
783 |
try:
|
|
|
784 |
query = f"""
|
785 |
-
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
|
|
786 |
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
787 |
"""
|
788 |
result = sf.query(query)
|
789 |
cart_items = result.get("records", [])
|
790 |
|
|
|
791 |
addons_price = sum(addon['price'] for addon in addons)
|
792 |
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
793 |
|
|
|
794 |
if cart_items:
|
795 |
cart_item_id = cart_items[0]['Id']
|
796 |
existing_quantity = cart_items[0]['Quantity__c']
|
@@ -798,19 +806,23 @@ def add_to_cart():
|
|
798 |
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
799 |
existing_instructions = cart_items[0].get('Instructions__c', "")
|
800 |
|
|
|
801 |
combined_addons = existing_addons if existing_addons != "None" else ""
|
802 |
if new_addons:
|
803 |
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
804 |
|
|
|
805 |
combined_instructions = existing_instructions
|
806 |
if instructions:
|
807 |
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
808 |
|
|
|
809 |
combined_addons_list = combined_addons.split("; ")
|
810 |
combined_addons_price = sum(
|
811 |
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
812 |
)
|
813 |
|
|
|
814 |
sf.Cart_Item__c.update(cart_item_id, {
|
815 |
"Quantity__c": existing_quantity + 1,
|
816 |
"Add_Ons__c": combined_addons,
|
@@ -821,12 +833,14 @@ def add_to_cart():
|
|
821 |
"Section__c": section
|
822 |
})
|
823 |
else:
|
|
|
824 |
addons_string = "None"
|
825 |
if addons:
|
826 |
addons_string = new_addons
|
827 |
|
828 |
total_price = item_price + addons_price
|
829 |
|
|
|
830 |
sf.Cart_Item__c.create({
|
831 |
"Name": item_name,
|
832 |
"Price__c": total_price,
|
@@ -842,10 +856,11 @@ def add_to_cart():
|
|
842 |
})
|
843 |
|
844 |
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
|
|
845 |
except Exception as e:
|
|
|
846 |
print(f"Error adding item to cart: {str(e)}")
|
847 |
-
return jsonify({"success": False, "error":
|
848 |
-
|
849 |
|
850 |
|
851 |
@app.route("/cart/add_item", methods=["POST"])
|
@@ -901,8 +916,9 @@ def get_addons():
|
|
901 |
item_name = request.args.get('item_name')
|
902 |
item_section = request.args.get('item_section')
|
903 |
|
|
|
904 |
if not item_name or not item_section:
|
905 |
-
return jsonify({"success": False, "error": "Item name and section are required."})
|
906 |
|
907 |
try:
|
908 |
# Fetch customization options from Salesforce based on the section
|
@@ -914,10 +930,20 @@ def get_addons():
|
|
914 |
result = sf.query(query)
|
915 |
addons = result.get('records', [])
|
916 |
|
|
|
|
|
|
|
|
|
917 |
# Format data for frontend
|
918 |
formatted_addons = []
|
919 |
for addon in addons:
|
920 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
921 |
formatted_addons.append({
|
922 |
"name": addon["Name"],
|
923 |
"type": addon["Customization_Type__c"],
|
@@ -930,8 +956,10 @@ def get_addons():
|
|
930 |
return jsonify({"success": True, "addons": formatted_addons})
|
931 |
|
932 |
except Exception as e:
|
933 |
-
|
934 |
-
|
|
|
|
|
935 |
|
936 |
@app.route("/cart/update_quantity", methods=["POST"])
|
937 |
def update_quantity():
|
|
|
768 |
@app.route('/cart/add', methods=['POST'])
|
769 |
def add_to_cart():
|
770 |
data = request.json
|
771 |
+
item_name = data.get('itemName', '').strip()
|
772 |
item_price = data.get('itemPrice')
|
773 |
item_image = data.get('itemImage')
|
774 |
addons = data.get('addons', [])
|
|
|
777 |
section = data.get('section')
|
778 |
customer_email = session.get('user_email')
|
779 |
|
780 |
+
# Basic validation for required fields
|
781 |
if not item_name or not item_price:
|
782 |
+
return jsonify({"success": False, "error": "Item name and price are required."}), 400
|
783 |
+
|
784 |
+
if not customer_email:
|
785 |
+
return jsonify({"success": False, "error": "User email is required."}), 400
|
786 |
|
787 |
try:
|
788 |
+
# Query to check if the item is already in the cart
|
789 |
query = f"""
|
790 |
+
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
791 |
+
FROM Cart_Item__c
|
792 |
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
793 |
"""
|
794 |
result = sf.query(query)
|
795 |
cart_items = result.get("records", [])
|
796 |
|
797 |
+
# Calculate the total price for the addons
|
798 |
addons_price = sum(addon['price'] for addon in addons)
|
799 |
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
800 |
|
801 |
+
# If the item is already in the cart, update it
|
802 |
if cart_items:
|
803 |
cart_item_id = cart_items[0]['Id']
|
804 |
existing_quantity = cart_items[0]['Quantity__c']
|
|
|
806 |
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
807 |
existing_instructions = cart_items[0].get('Instructions__c', "")
|
808 |
|
809 |
+
# Combine the new addons with the existing ones
|
810 |
combined_addons = existing_addons if existing_addons != "None" else ""
|
811 |
if new_addons:
|
812 |
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
813 |
|
814 |
+
# Combine existing instructions with new instructions
|
815 |
combined_instructions = existing_instructions
|
816 |
if instructions:
|
817 |
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
818 |
|
819 |
+
# Calculate total addons price
|
820 |
combined_addons_list = combined_addons.split("; ")
|
821 |
combined_addons_price = sum(
|
822 |
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
823 |
)
|
824 |
|
825 |
+
# Update the cart item in Salesforce
|
826 |
sf.Cart_Item__c.update(cart_item_id, {
|
827 |
"Quantity__c": existing_quantity + 1,
|
828 |
"Add_Ons__c": combined_addons,
|
|
|
833 |
"Section__c": section
|
834 |
})
|
835 |
else:
|
836 |
+
# If the item is not already in the cart, create a new entry
|
837 |
addons_string = "None"
|
838 |
if addons:
|
839 |
addons_string = new_addons
|
840 |
|
841 |
total_price = item_price + addons_price
|
842 |
|
843 |
+
# Create new cart item in Salesforce
|
844 |
sf.Cart_Item__c.create({
|
845 |
"Name": item_name,
|
846 |
"Price__c": total_price,
|
|
|
856 |
})
|
857 |
|
858 |
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
859 |
+
|
860 |
except Exception as e:
|
861 |
+
# Log the error for debugging
|
862 |
print(f"Error adding item to cart: {str(e)}")
|
863 |
+
return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
|
|
|
864 |
|
865 |
|
866 |
@app.route("/cart/add_item", methods=["POST"])
|
|
|
916 |
item_name = request.args.get('item_name')
|
917 |
item_section = request.args.get('item_section')
|
918 |
|
919 |
+
# Check if both item_name and item_section are provided
|
920 |
if not item_name or not item_section:
|
921 |
+
return jsonify({"success": False, "error": "Item name and section are required."}), 400
|
922 |
|
923 |
try:
|
924 |
# Fetch customization options from Salesforce based on the section
|
|
|
930 |
result = sf.query(query)
|
931 |
addons = result.get('records', [])
|
932 |
|
933 |
+
# Check if we found any addons
|
934 |
+
if not addons:
|
935 |
+
return jsonify({"success": False, "error": "No customization options found for the given section."}), 404
|
936 |
+
|
937 |
# Format data for frontend
|
938 |
formatted_addons = []
|
939 |
for addon in addons:
|
940 |
+
# Ensure 'Options__c' exists and is not None
|
941 |
+
options = addon.get("Options__c", "")
|
942 |
+
if options: # If options are available, split them
|
943 |
+
options = options.split(", ") # Convert comma-separated options into a list
|
944 |
+
else:
|
945 |
+
options = [] # If no options, default to an empty list
|
946 |
+
|
947 |
formatted_addons.append({
|
948 |
"name": addon["Name"],
|
949 |
"type": addon["Customization_Type__c"],
|
|
|
956 |
return jsonify({"success": True, "addons": formatted_addons})
|
957 |
|
958 |
except Exception as e:
|
959 |
+
# Log the exception for debugging
|
960 |
+
app.logger.error(f"Error fetching addons: {str(e)}")
|
961 |
+
return jsonify({"success": False, "error": "An error occurred while fetching customization options."}), 500
|
962 |
+
|
963 |
|
964 |
@app.route("/cart/update_quantity", methods=["POST"])
|
965 |
def update_quantity():
|