Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -130,30 +130,34 @@ def menu():
|
|
130 |
|
131 |
|
132 |
|
133 |
-
|
134 |
@app.route("/cart", methods=["GET"])
|
135 |
def cart():
|
136 |
-
email = session.get('user_email')
|
137 |
if not email:
|
138 |
-
return redirect(url_for("login"))
|
|
|
139 |
try:
|
|
|
140 |
result = sf.query(f"""
|
141 |
SELECT Name, Price__c, Quantity__c, Add_Ons__c, Image1__c
|
142 |
FROM Cart_Item__c
|
143 |
WHERE Customer_Email__c = '{email}'
|
144 |
""")
|
145 |
cart_items = result.get("records", [])
|
|
|
|
|
146 |
for item in cart_items:
|
147 |
-
item['Add_Ons__c'] = item.get('Add_Ons__c', "None")
|
148 |
|
|
|
149 |
subtotal = sum(item['Quantity__c'] * item['Price__c'] for item in cart_items)
|
150 |
except Exception as e:
|
151 |
print(f"Error fetching cart items: {e}")
|
152 |
cart_items = []
|
153 |
subtotal = 0
|
154 |
-
|
155 |
-
return render_template("cart.html", cart_items=cart_items, subtotal=subtotal)
|
156 |
|
|
|
|
|
157 |
|
158 |
|
159 |
@app.route('/cart/add', methods=['POST'])
|
@@ -162,28 +166,28 @@ def add_to_cart():
|
|
162 |
item_name = data.get('itemName').strip()
|
163 |
item_price = data.get('itemPrice')
|
164 |
item_image = data.get('itemImage')
|
165 |
-
addons = data.get('addons', [])
|
166 |
-
addons_string = ";".join(addons) if addons else "None"
|
167 |
-
|
168 |
customer_email = session.get('user_email') # Get logged-in user's email from session
|
169 |
|
170 |
-
# Validate the required fields
|
171 |
if not item_name or not item_price:
|
172 |
return jsonify({"success": False, "error": "Item name and price are required."})
|
173 |
|
|
|
|
|
|
|
174 |
try:
|
175 |
-
#
|
176 |
sf.Cart_Item__c.create({
|
177 |
-
"Name": item_name,
|
178 |
-
"Price__c": item_price,
|
179 |
-
"Quantity__c": 1,
|
180 |
-
"Add_Ons__c":
|
181 |
-
"Image1__c": item_image,
|
182 |
-
"Customer_Email__c": customer_email,
|
183 |
})
|
184 |
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
185 |
except Exception as e:
|
186 |
-
print(f"Error adding item to cart: {str(e)}")
|
187 |
return jsonify({"success": False, "error": str(e)})
|
188 |
|
189 |
|
|
|
130 |
|
131 |
|
132 |
|
|
|
133 |
@app.route("/cart", methods=["GET"])
|
134 |
def cart():
|
135 |
+
email = session.get('user_email') # Get logged-in user's email
|
136 |
if not email:
|
137 |
+
return redirect(url_for("login")) # Redirect to login if not logged in
|
138 |
+
|
139 |
try:
|
140 |
+
# Query cart items
|
141 |
result = sf.query(f"""
|
142 |
SELECT Name, Price__c, Quantity__c, Add_Ons__c, Image1__c
|
143 |
FROM Cart_Item__c
|
144 |
WHERE Customer_Email__c = '{email}'
|
145 |
""")
|
146 |
cart_items = result.get("records", [])
|
147 |
+
|
148 |
+
# Process each item and ensure add-ons are displayed 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 are present
|
151 |
|
152 |
+
# Calculate subtotal
|
153 |
subtotal = sum(item['Quantity__c'] * item['Price__c'] for item in cart_items)
|
154 |
except Exception as e:
|
155 |
print(f"Error fetching cart items: {e}")
|
156 |
cart_items = []
|
157 |
subtotal = 0
|
|
|
|
|
158 |
|
159 |
+
# Render the cart page
|
160 |
+
return render_template("cart.html", cart_items=cart_items, subtotal=subtotal)
|
161 |
|
162 |
|
163 |
@app.route('/cart/add', methods=['POST'])
|
|
|
166 |
item_name = data.get('itemName').strip()
|
167 |
item_price = data.get('itemPrice')
|
168 |
item_image = data.get('itemImage')
|
169 |
+
addons = data.get('addons', []) # Default to an empty list if no add-ons are selected
|
|
|
|
|
170 |
customer_email = session.get('user_email') # Get logged-in user's email from session
|
171 |
|
|
|
172 |
if not item_name or not item_price:
|
173 |
return jsonify({"success": False, "error": "Item name and price are required."})
|
174 |
|
175 |
+
# Convert the add-ons list to a semicolon-separated string
|
176 |
+
addons_string = ";".join(addons) if addons else "None"
|
177 |
+
|
178 |
try:
|
179 |
+
# Save the item to the cart in Salesforce
|
180 |
sf.Cart_Item__c.create({
|
181 |
+
"Name": item_name,
|
182 |
+
"Price__c": item_price,
|
183 |
+
"Quantity__c": 1,
|
184 |
+
"Add_Ons__c": addons_string, # Save add-ons as a string
|
185 |
+
"Image1__c": item_image,
|
186 |
+
"Customer_Email__c": customer_email,
|
187 |
})
|
188 |
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
189 |
except Exception as e:
|
190 |
+
print(f"Error adding item to cart: {str(e)}")
|
191 |
return jsonify({"success": False, "error": str(e)})
|
192 |
|
193 |
|