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', '')
|
|
@@ -1127,7 +1127,8 @@ def add_to_cart():
|
|
| 1127 |
result = sf.query(query)
|
| 1128 |
cart_items = result.get("records", [])
|
| 1129 |
|
| 1130 |
-
|
|
|
|
| 1131 |
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
| 1132 |
|
| 1133 |
if cart_items:
|
|
@@ -1137,35 +1138,43 @@ def add_to_cart():
|
|
| 1137 |
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
| 1138 |
existing_instructions = cart_items[0].get('Instructions__c', "")
|
| 1139 |
|
|
|
|
| 1140 |
combined_addons = existing_addons if existing_addons != "None" else ""
|
| 1141 |
if new_addons:
|
| 1142 |
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
| 1143 |
|
|
|
|
| 1144 |
combined_instructions = existing_instructions
|
| 1145 |
if instructions:
|
| 1146 |
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
| 1147 |
|
|
|
|
| 1148 |
combined_addons_list = combined_addons.split("; ")
|
| 1149 |
combined_addons_price = sum(
|
| 1150 |
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
| 1151 |
)
|
| 1152 |
|
|
|
|
|
|
|
| 1153 |
sf.Cart_Item__c.update(cart_item_id, {
|
| 1154 |
"Quantity__c": existing_quantity + 1,
|
| 1155 |
"Add_Ons__c": combined_addons,
|
| 1156 |
"Add_Ons_Price__c": combined_addons_price,
|
| 1157 |
"Instructions__c": combined_instructions,
|
| 1158 |
-
"Price__c":
|
| 1159 |
"Category__c": category,
|
| 1160 |
"Section__c": section
|
| 1161 |
})
|
| 1162 |
else:
|
|
|
|
| 1163 |
addons_string = "None"
|
| 1164 |
if addons:
|
| 1165 |
addons_string = new_addons
|
| 1166 |
|
|
|
|
| 1167 |
total_price = item_price + addons_price
|
| 1168 |
|
|
|
|
| 1169 |
sf.Cart_Item__c.create({
|
| 1170 |
"Name": item_name,
|
| 1171 |
"Price__c": total_price,
|
|
@@ -1184,7 +1193,7 @@ def add_to_cart():
|
|
| 1184 |
except Exception as e:
|
| 1185 |
print(f"Error adding item to cart: {str(e)}")
|
| 1186 |
return jsonify({"success": False, "error": str(e)})
|
| 1187 |
-
|
| 1188 |
|
| 1189 |
@app.route("/order", methods=["GET"])
|
| 1190 |
def order_summary():
|
|
|
|
| 1108 |
def add_to_cart():
|
| 1109 |
data = request.json
|
| 1110 |
item_name = data.get('itemName').strip()
|
| 1111 |
+
item_price = float(data.get('itemPrice', 0)) # Convert item price to a float for calculations
|
| 1112 |
item_image = data.get('itemImage')
|
| 1113 |
addons = data.get('addons', [])
|
| 1114 |
instructions = data.get('instructions', '')
|
|
|
|
| 1127 |
result = sf.query(query)
|
| 1128 |
cart_items = result.get("records", [])
|
| 1129 |
|
| 1130 |
+
# Calculate total addon price
|
| 1131 |
+
addons_price = sum(float(addon['price']) for addon in addons)
|
| 1132 |
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
| 1133 |
|
| 1134 |
if cart_items:
|
|
|
|
| 1138 |
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
| 1139 |
existing_instructions = cart_items[0].get('Instructions__c', "")
|
| 1140 |
|
| 1141 |
+
# Combine existing addons with new addons
|
| 1142 |
combined_addons = existing_addons if existing_addons != "None" else ""
|
| 1143 |
if new_addons:
|
| 1144 |
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
| 1145 |
|
| 1146 |
+
# Combine instructions
|
| 1147 |
combined_instructions = existing_instructions
|
| 1148 |
if instructions:
|
| 1149 |
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
| 1150 |
|
| 1151 |
+
# Calculate the combined addon price (convert addons to float)
|
| 1152 |
combined_addons_list = combined_addons.split("; ")
|
| 1153 |
combined_addons_price = sum(
|
| 1154 |
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
| 1155 |
)
|
| 1156 |
|
| 1157 |
+
# Update the cart item with new quantity and price
|
| 1158 |
+
updated_price = (existing_quantity + 1) * item_price + combined_addons_price
|
| 1159 |
sf.Cart_Item__c.update(cart_item_id, {
|
| 1160 |
"Quantity__c": existing_quantity + 1,
|
| 1161 |
"Add_Ons__c": combined_addons,
|
| 1162 |
"Add_Ons_Price__c": combined_addons_price,
|
| 1163 |
"Instructions__c": combined_instructions,
|
| 1164 |
+
"Price__c": updated_price, # Updated price after adding addons and quantity
|
| 1165 |
"Category__c": category,
|
| 1166 |
"Section__c": section
|
| 1167 |
})
|
| 1168 |
else:
|
| 1169 |
+
# If the item is not in the cart, create a new cart item
|
| 1170 |
addons_string = "None"
|
| 1171 |
if addons:
|
| 1172 |
addons_string = new_addons
|
| 1173 |
|
| 1174 |
+
# Calculate total price with addons
|
| 1175 |
total_price = item_price + addons_price
|
| 1176 |
|
| 1177 |
+
# Create a new cart item in Salesforce
|
| 1178 |
sf.Cart_Item__c.create({
|
| 1179 |
"Name": item_name,
|
| 1180 |
"Price__c": total_price,
|
|
|
|
| 1193 |
except Exception as e:
|
| 1194 |
print(f"Error adding item to cart: {str(e)}")
|
| 1195 |
return jsonify({"success": False, "error": str(e)})
|
| 1196 |
+
|
| 1197 |
|
| 1198 |
@app.route("/order", methods=["GET"])
|
| 1199 |
def order_summary():
|