Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1108,7 +1108,7 @@ def checkout():
|
|
1108 |
def add_to_cart():
|
1109 |
data = request.json
|
1110 |
item_name = data.get('itemName').strip()
|
1111 |
-
item_price = data.get('itemPrice') #
|
1112 |
item_image = data.get('itemImage')
|
1113 |
addons = data.get('addons', [])
|
1114 |
instructions = data.get('instructions', '')
|
@@ -1116,14 +1116,15 @@ def add_to_cart():
|
|
1116 |
section = data.get('section')
|
1117 |
customer_email = session.get('user_email')
|
1118 |
|
|
|
1119 |
if not item_name or not item_price:
|
1120 |
return jsonify({"success": False, "error": "Item name and price are required."})
|
1121 |
|
1122 |
try:
|
1123 |
-
# Convert item_price to a float
|
1124 |
-
item_price = float(item_price.replace('$', '').strip()) #
|
1125 |
|
1126 |
-
# Calculate
|
1127 |
addons_price = sum(float(addon['price'].replace('$', '').strip()) for addon in addons)
|
1128 |
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
1129 |
|
@@ -1136,7 +1137,7 @@ def add_to_cart():
|
|
1136 |
cart_items = result.get("records", [])
|
1137 |
|
1138 |
if cart_items:
|
1139 |
-
#
|
1140 |
cart_item_id = cart_items[0]['Id']
|
1141 |
existing_quantity = cart_items[0]['Quantity__c']
|
1142 |
existing_addons = cart_items[0].get('Add_Ons__c', "None")
|
@@ -1153,26 +1154,29 @@ def add_to_cart():
|
|
1153 |
if instructions:
|
1154 |
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
1155 |
|
1156 |
-
# Calculate the
|
1157 |
-
|
|
|
|
|
1158 |
|
1159 |
-
# Update the cart item
|
|
|
1160 |
sf.Cart_Item__c.update(cart_item_id, {
|
1161 |
"Quantity__c": existing_quantity + 1,
|
1162 |
"Add_Ons__c": combined_addons,
|
1163 |
-
"Add_Ons_Price__c":
|
1164 |
"Instructions__c": combined_instructions,
|
1165 |
-
"Price__c": updated_price,
|
1166 |
"Category__c": category,
|
1167 |
"Section__c": section
|
1168 |
})
|
1169 |
else:
|
1170 |
-
#
|
1171 |
addons_string = "None"
|
1172 |
if addons:
|
1173 |
addons_string = new_addons
|
1174 |
|
1175 |
-
# Calculate total price including
|
1176 |
total_price = item_price + addons_price
|
1177 |
|
1178 |
# Create a new cart item in Salesforce
|
|
|
1108 |
def add_to_cart():
|
1109 |
data = request.json
|
1110 |
item_name = data.get('itemName').strip()
|
1111 |
+
item_price = data.get('itemPrice') # Get price as string (e.g., "$16.0")
|
1112 |
item_image = data.get('itemImage')
|
1113 |
addons = data.get('addons', [])
|
1114 |
instructions = data.get('instructions', '')
|
|
|
1116 |
section = data.get('section')
|
1117 |
customer_email = session.get('user_email')
|
1118 |
|
1119 |
+
# Check if the item name and price are provided
|
1120 |
if not item_name or not item_price:
|
1121 |
return jsonify({"success": False, "error": "Item name and price are required."})
|
1122 |
|
1123 |
try:
|
1124 |
+
# Convert item_price to a float after removing any non-numeric characters (e.g., "$")
|
1125 |
+
item_price = float(item_price.replace('$', '').strip()) # Ensure it's a numeric value
|
1126 |
|
1127 |
+
# Calculate addons price: Treat addon prices as strings too and convert them to numbers
|
1128 |
addons_price = sum(float(addon['price'].replace('$', '').strip()) for addon in addons)
|
1129 |
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
1130 |
|
|
|
1137 |
cart_items = result.get("records", [])
|
1138 |
|
1139 |
if cart_items:
|
1140 |
+
# Item already exists, update it
|
1141 |
cart_item_id = cart_items[0]['Id']
|
1142 |
existing_quantity = cart_items[0]['Quantity__c']
|
1143 |
existing_addons = cart_items[0].get('Add_Ons__c', "None")
|
|
|
1154 |
if instructions:
|
1155 |
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
1156 |
|
1157 |
+
# Calculate the combined addon price
|
1158 |
+
combined_addons_price = sum(
|
1159 |
+
float(addon.split("($")[1][:-1]) for addon in combined_addons.split("; ") if "($" in addon
|
1160 |
+
)
|
1161 |
|
1162 |
+
# Update the cart item with the new quantity and price
|
1163 |
+
updated_price = (existing_quantity + 1) * item_price + combined_addons_price
|
1164 |
sf.Cart_Item__c.update(cart_item_id, {
|
1165 |
"Quantity__c": existing_quantity + 1,
|
1166 |
"Add_Ons__c": combined_addons,
|
1167 |
+
"Add_Ons_Price__c": combined_addons_price,
|
1168 |
"Instructions__c": combined_instructions,
|
1169 |
+
"Price__c": updated_price,
|
1170 |
"Category__c": category,
|
1171 |
"Section__c": section
|
1172 |
})
|
1173 |
else:
|
1174 |
+
# Item is not in the cart, create a new cart item
|
1175 |
addons_string = "None"
|
1176 |
if addons:
|
1177 |
addons_string = new_addons
|
1178 |
|
1179 |
+
# Calculate total price including addons
|
1180 |
total_price = item_price + addons_price
|
1181 |
|
1182 |
# Create a new cart item in Salesforce
|