nagasurendra commited on
Commit
1bda345
·
verified ·
1 Parent(s): 1a745fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -42
app.py CHANGED
@@ -161,37 +161,27 @@ def create_modal_window():
161
  </div>
162
  """
163
  return modal_html
164
- from flask import Flask, request, jsonify
165
-
166
- # Define an API endpoint for cart submission
167
- @app.post("/submit_cart")
168
- def submit_cart():
169
- data = request.json
170
- cart = data.get("cart", [])
171
- name = data.get("name", "")
172
- email = data.get("email", "")
173
-
174
  try:
175
- # Construct cart summary
176
  cart_summary = "\n".join([
177
  f"Item: {item['name']}, Quantity: {item['quantity']}, Total Cost: ${item['totalCost']:.2f}, "
178
  f"Extras: {', '.join(extra['name'] for extra in item['extras']) or 'None'}, "
179
  f"Instructions: {item['instructions'] or 'None'}"
180
- for item in cart
181
  ])
182
- total_cost = sum(item['totalCost'] for item in cart)
183
 
184
  # Save to Salesforce
185
  sf.Order__c.create({
186
- "Customer_Name__c": name,
187
- "Customer_Email__c": email,
188
- "Order_Details__c": cart_summary,
189
- "Total_Cost__c": total_cost
 
190
  })
191
- return jsonify({"status": "success", "message": "Order saved successfully in Salesforce!"})
192
  except Exception as e:
193
- return jsonify({"status": "error", "message": str(e)})
194
-
195
 
196
  # JavaScript for Modal and Cart
197
  def modal_js():
@@ -290,34 +280,25 @@ def modal_js():
290
  totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
291
  }
292
  function proceedToCheckout() {
293
- const cartSummary = cart.map(item => ({
294
- name: item.name,
295
- quantity: item.quantity,
296
- totalCost: item.totalCost,
297
- extras: item.extras.map(extra => ({ name: extra.name, price: extra.price })),
298
- instructions: item.instructions
299
- }));
300
- const name = prompt("Enter your name:");
301
- const email = prompt("Enter your email:");
302
 
303
  fetch("/submit_cart", {
304
  method: "POST",
305
  headers: { "Content-Type": "application/json" },
306
- body: JSON.stringify({ cart: cartSummary, name, email })
 
 
 
 
 
 
 
 
307
  })
308
- .then(response => response.json())
309
- .then(data => {
310
- if (data.status === "success") {
311
- alert("Order submitted successfully!");
312
- cart = []; // Clear the cart
313
- updateCart(); // Update the cart display
314
- } else {
315
- alert("Error: " + data.message);
316
- }
317
- })
318
- .catch(error => console.error("Error submitting cart:", error));
319
  }
320
-
321
  // Reset all selected add-ons when opening a new item modal
322
  function resetAddOns() {
323
  const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
 
161
  </div>
162
  """
163
  return modal_html
164
+ def save_cart_to_salesforce(cart_data, customer_name, customer_email):
 
 
 
 
 
 
 
 
 
165
  try:
 
166
  cart_summary = "\n".join([
167
  f"Item: {item['name']}, Quantity: {item['quantity']}, Total Cost: ${item['totalCost']:.2f}, "
168
  f"Extras: {', '.join(extra['name'] for extra in item['extras']) or 'None'}, "
169
  f"Instructions: {item['instructions'] or 'None'}"
170
+ for item in cart_data
171
  ])
172
+ total_cost = sum(item['totalCost'] for item in cart_data)
173
 
174
  # Save to Salesforce
175
  sf.Order__c.create({
176
+ "Customer_Name__c": customer_name,
177
+ "Customer_Email__c": customer_email,
178
+ "Order_Item__c": cart_summary,
179
+ "Total_Cost__c": total_cost,
180
+ "Order_Item__c": json.dumps(cart_data)
181
  })
182
+ return "Order saved successfully in Salesforce!"
183
  except Exception as e:
184
+ return f"Error saving order: {str(e)}"
 
185
 
186
  # JavaScript for Modal and Cart
187
  def modal_js():
 
280
  totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
281
  }
282
  function proceedToCheckout() {
283
+ const cartData = JSON.stringify(cart);
284
+ const customerName = prompt("Enter your name:");
285
+ const customerEmail = prompt("Enter your email:");
 
 
 
 
 
 
286
 
287
  fetch("/submit_cart", {
288
  method: "POST",
289
  headers: { "Content-Type": "application/json" },
290
+ body: JSON.stringify({ cart: cart, name: customerName, email: customerEmail })
291
+ })
292
+ .then(response => response.json())
293
+ .then(data => {
294
+ if (data.status === "success") {
295
+ alert("Order submitted successfully!");
296
+ } else {
297
+ alert("Error: " + data.message);
298
+ }
299
  })
300
+ .catch(error => console.error("Error submitting cart:", error));
 
 
 
 
 
 
 
 
 
 
301
  }
 
302
  // Reset all selected add-ons when opening a new item modal
303
  function resetAddOns() {
304
  const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');