Spaces:
Runtime error
Runtime error
Update app.py (#1)
Browse files- Update app.py (bf89757a623bcdf4805a2113efbbab1faec4d040)
app.py
CHANGED
|
@@ -142,12 +142,13 @@ def cart():
|
|
| 142 |
""")
|
| 143 |
cart_items = result.get("records", [])
|
| 144 |
|
| 145 |
-
#
|
|
|
|
|
|
|
|
|
|
| 146 |
for item in cart_items:
|
| 147 |
item['Add_Ons__c'] = item.get('Add_Ons__c', "None") # Default to "None" if no add-ons
|
| 148 |
|
| 149 |
-
# Calculate subtotal
|
| 150 |
-
subtotal = sum(item['Quantity__c'] * item['Price__c'] for item in cart_items)
|
| 151 |
except Exception as e:
|
| 152 |
print(f"Error fetching cart items: {e}")
|
| 153 |
cart_items = []
|
|
@@ -155,28 +156,38 @@ def cart():
|
|
| 155 |
|
| 156 |
return render_template("cart.html", cart_items=cart_items, subtotal=subtotal)
|
| 157 |
|
|
|
|
| 158 |
@app.route('/cart/add', methods=['POST'])
|
| 159 |
def add_to_cart():
|
| 160 |
data = request.json # Extract JSON payload
|
| 161 |
item_name = data.get('itemName').strip()
|
| 162 |
-
item_price = data.get('itemPrice')
|
| 163 |
item_image = data.get('itemImage')
|
| 164 |
addons = data.get('addons', []) # Extract add-ons array
|
| 165 |
-
customer_email = session.get('user_email') #
|
| 166 |
|
| 167 |
if not item_name or not item_price:
|
| 168 |
return jsonify({"success": False, "error": "Item name and price are required."})
|
| 169 |
|
| 170 |
-
# Convert add-ons array to a
|
| 171 |
-
addons_string = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
try:
|
| 174 |
# Save the cart item in Salesforce
|
| 175 |
sf.Cart_Item__c.create({
|
| 176 |
"Name": item_name, # Item name
|
| 177 |
-
"Price__c":
|
|
|
|
| 178 |
"Quantity__c": 1, # Default quantity is 1
|
| 179 |
-
"Add_Ons__c": addons_string, # Add-ons
|
| 180 |
"Image1__c": item_image, # Item image URL
|
| 181 |
"Customer_Email__c": customer_email, # Associated customer's email
|
| 182 |
})
|
|
@@ -292,16 +303,22 @@ def checkout():
|
|
| 292 |
user_id = session.get('user_id')
|
| 293 |
if not email or not user_id:
|
| 294 |
return jsonify({"success": False, "message": "User not logged in"})
|
|
|
|
| 295 |
try:
|
|
|
|
| 296 |
result = sf.query(f"""
|
| 297 |
SELECT Id, Name, Price__c, Quantity__c, Add_Ons__c
|
| 298 |
FROM Cart_Item__c
|
| 299 |
WHERE Customer_Email__c = '{email}'
|
| 300 |
""")
|
| 301 |
-
cart_items = result
|
| 302 |
if not cart_items:
|
| 303 |
return jsonify({"success": False, "message": "Cart is empty"})
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
order_data = {
|
| 306 |
"Customer_Name__c": user_id,
|
| 307 |
"Customer_Email__c": email,
|
|
@@ -313,14 +330,17 @@ def checkout():
|
|
| 313 |
"Add_Ons__c": "\n".join(
|
| 314 |
[item['Add_Ons__c'] if item.get('Add_Ons__c') else "None" for item in cart_items]
|
| 315 |
),
|
| 316 |
-
|
| 317 |
}
|
| 318 |
sf.Order__c.create(order_data)
|
|
|
|
|
|
|
| 319 |
for item in cart_items:
|
| 320 |
sf.Cart_Item__c.delete(item["Id"])
|
|
|
|
| 321 |
return jsonify({"success": True, "message": "Order placed successfully!"})
|
| 322 |
except Exception as e:
|
| 323 |
return jsonify({"success": False, "error": str(e)})
|
| 324 |
|
|
|
|
| 325 |
if __name__ == "__main__":
|
| 326 |
app.run(debug=False, host="0.0.0.0", port=7860)
|
|
|
|
| 142 |
""")
|
| 143 |
cart_items = result.get("records", [])
|
| 144 |
|
| 145 |
+
# Calculate the subtotal, including add-ons prices
|
| 146 |
+
subtotal = sum(item['Price__c'] * item['Quantity__c'] for item in cart_items)
|
| 147 |
+
|
| 148 |
+
# Ensure add-ons are processed properly
|
| 149 |
for item in cart_items:
|
| 150 |
item['Add_Ons__c'] = item.get('Add_Ons__c', "None") # Default to "None" if no add-ons
|
| 151 |
|
|
|
|
|
|
|
| 152 |
except Exception as e:
|
| 153 |
print(f"Error fetching cart items: {e}")
|
| 154 |
cart_items = []
|
|
|
|
| 156 |
|
| 157 |
return render_template("cart.html", cart_items=cart_items, subtotal=subtotal)
|
| 158 |
|
| 159 |
+
|
| 160 |
@app.route('/cart/add', methods=['POST'])
|
| 161 |
def add_to_cart():
|
| 162 |
data = request.json # Extract JSON payload
|
| 163 |
item_name = data.get('itemName').strip()
|
| 164 |
+
item_price = data.get('itemPrice') # Base price of the item
|
| 165 |
item_image = data.get('itemImage')
|
| 166 |
addons = data.get('addons', []) # Extract add-ons array
|
| 167 |
+
customer_email = session.get('user_email') # Get the logged-in user's email
|
| 168 |
|
| 169 |
if not item_name or not item_price:
|
| 170 |
return jsonify({"success": False, "error": "Item name and price are required."})
|
| 171 |
|
| 172 |
+
# Convert add-ons array to a formatted string and calculate the add-ons total price
|
| 173 |
+
addons_string = "None"
|
| 174 |
+
addons_total_price = 0
|
| 175 |
+
if addons:
|
| 176 |
+
addons_details = [f"{addon['name']} (${addon['price']})" for addon in addons]
|
| 177 |
+
addons_total_price = sum(addon['price'] for addon in addons)
|
| 178 |
+
addons_string = "; ".join(addons_details)
|
| 179 |
+
|
| 180 |
+
# Calculate the total price (item price + add-ons)
|
| 181 |
+
total_price = item_price + addons_total_price
|
| 182 |
|
| 183 |
try:
|
| 184 |
# Save the cart item in Salesforce
|
| 185 |
sf.Cart_Item__c.create({
|
| 186 |
"Name": item_name, # Item name
|
| 187 |
+
"Price__c": total_price, # Total price (item + add-ons)
|
| 188 |
+
"Base_Price__c": item_price, # Base price without add-ons
|
| 189 |
"Quantity__c": 1, # Default quantity is 1
|
| 190 |
+
"Add_Ons__c": addons_string, # Add-ons with name and price
|
| 191 |
"Image1__c": item_image, # Item image URL
|
| 192 |
"Customer_Email__c": customer_email, # Associated customer's email
|
| 193 |
})
|
|
|
|
| 303 |
user_id = session.get('user_id')
|
| 304 |
if not email or not user_id:
|
| 305 |
return jsonify({"success": False, "message": "User not logged in"})
|
| 306 |
+
|
| 307 |
try:
|
| 308 |
+
# Fetch cart items for the user
|
| 309 |
result = sf.query(f"""
|
| 310 |
SELECT Id, Name, Price__c, Quantity__c, Add_Ons__c
|
| 311 |
FROM Cart_Item__c
|
| 312 |
WHERE Customer_Email__c = '{email}'
|
| 313 |
""")
|
| 314 |
+
cart_items = result.get("records", [])
|
| 315 |
if not cart_items:
|
| 316 |
return jsonify({"success": False, "message": "Cart is empty"})
|
| 317 |
+
|
| 318 |
+
# Calculate the total price of the order
|
| 319 |
+
total_price = sum(item['Price__c'] * item['Quantity__c'] for item in cart_items)
|
| 320 |
+
|
| 321 |
+
# Create the order in Salesforce
|
| 322 |
order_data = {
|
| 323 |
"Customer_Name__c": user_id,
|
| 324 |
"Customer_Email__c": email,
|
|
|
|
| 330 |
"Add_Ons__c": "\n".join(
|
| 331 |
[item['Add_Ons__c'] if item.get('Add_Ons__c') else "None" for item in cart_items]
|
| 332 |
),
|
|
|
|
| 333 |
}
|
| 334 |
sf.Order__c.create(order_data)
|
| 335 |
+
|
| 336 |
+
# Clear the cart after placing the order
|
| 337 |
for item in cart_items:
|
| 338 |
sf.Cart_Item__c.delete(item["Id"])
|
| 339 |
+
|
| 340 |
return jsonify({"success": True, "message": "Order placed successfully!"})
|
| 341 |
except Exception as e:
|
| 342 |
return jsonify({"success": False, "error": str(e)})
|
| 343 |
|
| 344 |
+
|
| 345 |
if __name__ == "__main__":
|
| 346 |
app.run(debug=False, host="0.0.0.0", port=7860)
|