Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -127,9 +127,6 @@ def menu():
|
|
127 |
selected_category=selected_category,
|
128 |
|
129 |
)
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
@app.route("/cart", methods=["GET"])
|
134 |
def cart():
|
135 |
email = session.get('user_email') # Get logged-in user's email
|
@@ -145,9 +142,9 @@ def cart():
|
|
145 |
""")
|
146 |
cart_items = result.get("records", [])
|
147 |
|
148 |
-
#
|
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 |
# Calculate subtotal
|
153 |
subtotal = sum(item['Quantity__c'] * item['Price__c'] for item in cart_items)
|
@@ -156,34 +153,32 @@ def cart():
|
|
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'])
|
164 |
def add_to_cart():
|
165 |
-
data = request.json
|
166 |
item_name = data.get('itemName').strip()
|
167 |
item_price = data.get('itemPrice')
|
168 |
item_image = data.get('itemImage')
|
169 |
-
addons = data.get('addons', []) #
|
170 |
-
customer_email = session.get('user_email') #
|
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
|
176 |
-
addons_string = ";".join(addons) if addons else "None"
|
177 |
|
178 |
try:
|
179 |
-
# Save the item
|
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, #
|
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:
|
@@ -242,18 +237,23 @@ def remove_cart_item(item_name):
|
|
242 |
|
243 |
@app.route('/api/addons', methods=['GET'])
|
244 |
def get_addons():
|
245 |
-
item_name = request.args.get('item_name')
|
246 |
if not item_name:
|
247 |
return jsonify({"success": False, "error": "Item name is required."})
|
248 |
|
249 |
try:
|
250 |
-
|
|
|
|
|
|
|
|
|
251 |
addons = sf.query(query)['records']
|
252 |
return jsonify({"success": True, "addons": addons})
|
253 |
except Exception as e:
|
254 |
print(f"Error fetching add-ons: {e}")
|
255 |
return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."})
|
256 |
|
|
|
257 |
@app.route("/cart/update_quantity", methods=["POST"])
|
258 |
def update_quantity():
|
259 |
data = request.json # Extract JSON data from the request
|
|
|
127 |
selected_category=selected_category,
|
128 |
|
129 |
)
|
|
|
|
|
|
|
130 |
@app.route("/cart", methods=["GET"])
|
131 |
def cart():
|
132 |
email = session.get('user_email') # Get logged-in user's email
|
|
|
142 |
""")
|
143 |
cart_items = result.get("records", [])
|
144 |
|
145 |
+
# Process add-ons if present
|
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)
|
|
|
153 |
cart_items = []
|
154 |
subtotal = 0
|
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') # Logged-in user's 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 semicolon-separated string with names and prices
|
171 |
+
addons_string = ";".join([f"{addon['name']} (${addon['price']})" for addon in addons]) if addons else "None"
|
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": item_price, # Item price
|
178 |
+
"Quantity__c": 1, # Default quantity is 1
|
179 |
+
"Add_Ons__c": addons_string, # Add-ons string
|
180 |
+
"Image1__c": item_image, # Item image URL
|
181 |
+
"Customer_Email__c": customer_email, # Associated customer's email
|
182 |
})
|
183 |
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
184 |
except Exception as e:
|
|
|
237 |
|
238 |
@app.route('/api/addons', methods=['GET'])
|
239 |
def get_addons():
|
240 |
+
item_name = request.args.get('item_name') # Fetch the requested item name
|
241 |
if not item_name:
|
242 |
return jsonify({"success": False, "error": "Item name is required."})
|
243 |
|
244 |
try:
|
245 |
+
# Fetch add-ons related to the item (update query as needed)
|
246 |
+
query = f"""
|
247 |
+
SELECT Name, Price__c
|
248 |
+
FROM Add_Ons__c
|
249 |
+
"""
|
250 |
addons = sf.query(query)['records']
|
251 |
return jsonify({"success": True, "addons": addons})
|
252 |
except Exception as e:
|
253 |
print(f"Error fetching add-ons: {e}")
|
254 |
return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."})
|
255 |
|
256 |
+
|
257 |
@app.route("/cart/update_quantity", methods=["POST"])
|
258 |
def update_quantity():
|
259 |
data = request.json # Extract JSON data from the request
|