nagasurendra commited on
Commit
90e7132
·
verified ·
1 Parent(s): 190deb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -169,6 +169,8 @@ def add_to_cart():
169
  SELECT Id, Quantity__c, Add_Ons__c FROM Cart_Item__c
170
  WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
171
  """
 
 
172
  result = sf.query(query)
173
  cart_items = result.get("records", [])
174
 
@@ -177,6 +179,7 @@ def add_to_cart():
177
  cart_item_id = cart_items[0]['Id']
178
  existing_quantity = cart_items[0]['Quantity__c']
179
  existing_addons = cart_items[0].get('Add_Ons__c', "None")
 
180
 
181
  # Combine existing add-ons with new add-ons
182
  new_addons = "; ".join([
@@ -190,6 +193,7 @@ def add_to_cart():
190
  sf.Cart_Item__c.update(cart_item_id, {
191
  "Quantity__c": existing_quantity + 1, # Increase quantity by 1
192
  "Add_Ons__c": combined_addons, # Update add-ons
 
193
  "Price__c": (existing_quantity + 1) * item_price, # Update total price
194
  })
195
  else:
@@ -200,13 +204,15 @@ def add_to_cart():
200
  f"{addon['name']} (${addon['price']})" for addon in addons
201
  ])
202
 
203
- total_price = item_price + sum(addon['price'] for addon in addons)
 
204
 
205
  sf.Cart_Item__c.create({
206
  "Name": item_name, # Item name
207
  "Price__c": total_price, # Total price (item + add-ons)
208
  "Base_Price__c": item_price, # Base price without add-ons
209
  "Quantity__c": 1, # Default quantity is 1
 
210
  "Add_Ons__c": addons_string, # Add-ons with name and price
211
  "Image1__c": item_image, # Item image URL
212
  "Customer_Email__c": customer_email, # Associated customer's email
@@ -303,7 +309,7 @@ def update_quantity():
303
  try:
304
  # Query the cart item in Salesforce
305
  cart_items = sf.query(
306
- f"SELECT Id, Quantity__c, Price__c, Base_Price__c, Add_Ons__c FROM Cart_Item__c "
307
  f"WHERE Customer_Email__c = '{email}' AND Name = '{item_name}'"
308
  )['records']
309
 
@@ -313,27 +319,16 @@ def update_quantity():
313
  # Retrieve the first matching record
314
  cart_item_id = cart_items[0]['Id']
315
  base_price = cart_items[0]['Base_Price__c']
316
- addons_string = cart_items[0].get('Add_Ons__c', "None")
317
-
318
- # Calculate the add-ons price
319
- addons_price = 0
320
- if addons_string and addons_string != "None":
321
- try:
322
- # Extract prices from "Addon1 ($2.00); Addon2 ($3.50)"
323
- addons_price = sum(
324
- float(price.strip()) for price in addons_string.split("$")[1::2]
325
- if price.strip().replace('.', '', 1).isdigit()
326
- )
327
- except ValueError:
328
- addons_price = 0 # Default to 0 if parsing fails
329
 
330
  # Calculate the new price
331
- new_price = (base_price * quantity) + addons_price
332
 
333
  # Update the record in Salesforce
334
  sf.Cart_Item__c.update(cart_item_id, {
335
  "Quantity__c": quantity,
336
- "Price__c": new_price
 
337
  })
338
 
339
  return jsonify({"success": True, "new_quantity": quantity, "new_price": new_price})
@@ -353,7 +348,7 @@ def checkout():
353
  try:
354
  # Fetch cart items for the user
355
  result = sf.query(f"""
356
- SELECT Id, Name, Price__c, Quantity__c, Add_Ons__c
357
  FROM Cart_Item__c
358
  WHERE Customer_Email__c = '{email}'
359
  """)
@@ -362,7 +357,7 @@ def checkout():
362
  return jsonify({"success": False, "message": "Cart is empty"})
363
 
364
  # Calculate the total price of the order
365
- total_price = sum(item['Price__c'] for item in cart_items)
366
 
367
  # Create the order in Salesforce
368
  order_data = {
@@ -385,6 +380,7 @@ def checkout():
385
 
386
  return jsonify({"success": True, "message": "Order placed successfully!"})
387
  except Exception as e:
 
388
  return jsonify({"success": False, "error": str(e)})
389
 
390
 
 
169
  SELECT Id, Quantity__c, Add_Ons__c FROM Cart_Item__c
170
  WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
171
  """
172
+ addons_price = sum(addon['price'] for addon in addons)
173
+
174
  result = sf.query(query)
175
  cart_items = result.get("records", [])
176
 
 
179
  cart_item_id = cart_items[0]['Id']
180
  existing_quantity = cart_items[0]['Quantity__c']
181
  existing_addons = cart_items[0].get('Add_Ons__c', "None")
182
+ existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
183
 
184
  # Combine existing add-ons with new add-ons
185
  new_addons = "; ".join([
 
193
  sf.Cart_Item__c.update(cart_item_id, {
194
  "Quantity__c": existing_quantity + 1, # Increase quantity by 1
195
  "Add_Ons__c": combined_addons, # Update add-ons
196
+ "Add_Ons_Price__c": existing_addons_price + addons_price,
197
  "Price__c": (existing_quantity + 1) * item_price, # Update total price
198
  })
199
  else:
 
204
  f"{addon['name']} (${addon['price']})" for addon in addons
205
  ])
206
 
207
+ total_price = item_price
208
+ addons_price = sum(addon['price'] for addon in addons)
209
 
210
  sf.Cart_Item__c.create({
211
  "Name": item_name, # Item name
212
  "Price__c": total_price, # Total price (item + add-ons)
213
  "Base_Price__c": item_price, # Base price without add-ons
214
  "Quantity__c": 1, # Default quantity is 1
215
+ "Add_Ons_Price__c": addons_price,
216
  "Add_Ons__c": addons_string, # Add-ons with name and price
217
  "Image1__c": item_image, # Item image URL
218
  "Customer_Email__c": customer_email, # Associated customer's email
 
309
  try:
310
  # Query the cart item in Salesforce
311
  cart_items = sf.query(
312
+ f"SELECT Id, Quantity__c, Price__c, Base_Price__c, Add_Ons_Price__c FROM Cart_Item__c "
313
  f"WHERE Customer_Email__c = '{email}' AND Name = '{item_name}'"
314
  )['records']
315
 
 
319
  # Retrieve the first matching record
320
  cart_item_id = cart_items[0]['Id']
321
  base_price = cart_items[0]['Base_Price__c']
322
+ addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
 
 
 
 
 
 
 
 
 
 
 
 
323
 
324
  # Calculate the new price
325
+ new_price = base_price * quantity
326
 
327
  # Update the record in Salesforce
328
  sf.Cart_Item__c.update(cart_item_id, {
329
  "Quantity__c": quantity,
330
+ "Price__c": new_price, # Update base price
331
+ "Add_Ons_Price__c": addons_price # Add-ons price remains constant
332
  })
333
 
334
  return jsonify({"success": True, "new_quantity": quantity, "new_price": new_price})
 
348
  try:
349
  # Fetch cart items for the user
350
  result = sf.query(f"""
351
+ SELECT Id, Name, Price__c, Add_Ons_Price__c, Quantity__c, Add_Ons__c
352
  FROM Cart_Item__c
353
  WHERE Customer_Email__c = '{email}'
354
  """)
 
357
  return jsonify({"success": False, "message": "Cart is empty"})
358
 
359
  # Calculate the total price of the order
360
+ total_price = sum(item['Price__c'] for item in cart_items) + sum(item['Add_Ons_Price__c'] for item in cart_items)
361
 
362
  # Create the order in Salesforce
363
  order_data = {
 
380
 
381
  return jsonify({"success": True, "message": "Order placed successfully!"})
382
  except Exception as e:
383
+ print(f"Error during checkout: {str(e)}")
384
  return jsonify({"success": False, "error": str(e)})
385
 
386