Rammohan0504 commited on
Commit
1f2fbe3
·
verified ·
1 Parent(s): a1d1806

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -1108,7 +1108,7 @@ def checkout():
1108
  def add_to_cart():
1109
  data = request.json
1110
  item_name = data.get('itemName').strip()
1111
- item_price = float(data.get('itemPrice', 0)) # Convert item price to a float for calculations
1112
  item_image = data.get('itemImage')
1113
  addons = data.get('addons', [])
1114
  instructions = data.get('instructions', '')
@@ -1116,10 +1116,19 @@ def add_to_cart():
1116
  section = data.get('section')
1117
  customer_email = session.get('user_email')
1118
 
 
1119
  if not item_name or not item_price:
1120
  return jsonify({"success": False, "error": "Item name and price are required."})
1121
 
1122
  try:
 
 
 
 
 
 
 
 
1123
  query = f"""
1124
  SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c FROM Cart_Item__c
1125
  WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
@@ -1127,11 +1136,8 @@ def add_to_cart():
1127
  result = sf.query(query)
1128
  cart_items = result.get("records", [])
1129
 
1130
- # Calculate total addon price
1131
- addons_price = sum(float(addon['price']) for addon in addons)
1132
- new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
1133
-
1134
  if cart_items:
 
1135
  cart_item_id = cart_items[0]['Id']
1136
  existing_quantity = cart_items[0]['Quantity__c']
1137
  existing_addons = cart_items[0].get('Add_Ons__c', "None")
@@ -1148,30 +1154,29 @@ def add_to_cart():
1148
  if instructions:
1149
  combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
1150
 
1151
- # Calculate the combined addon price (convert addons to float)
1152
- combined_addons_list = combined_addons.split("; ")
1153
  combined_addons_price = sum(
1154
- float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
1155
  )
1156
 
1157
- # Update the cart item with new quantity and price
1158
  updated_price = (existing_quantity + 1) * item_price + combined_addons_price
1159
  sf.Cart_Item__c.update(cart_item_id, {
1160
  "Quantity__c": existing_quantity + 1,
1161
  "Add_Ons__c": combined_addons,
1162
  "Add_Ons_Price__c": combined_addons_price,
1163
  "Instructions__c": combined_instructions,
1164
- "Price__c": updated_price, # Updated price after adding addons and quantity
1165
  "Category__c": category,
1166
  "Section__c": section
1167
  })
1168
  else:
1169
- # If the item is not in the cart, create a new cart item
1170
  addons_string = "None"
1171
  if addons:
1172
  addons_string = new_addons
1173
 
1174
- # Calculate total price with addons
1175
  total_price = item_price + addons_price
1176
 
1177
  # Create a new cart item in Salesforce
@@ -1195,6 +1200,7 @@ def add_to_cart():
1195
  return jsonify({"success": False, "error": str(e)})
1196
 
1197
 
 
1198
  @app.route("/order", methods=["GET"])
1199
  def order_summary():
1200
  email = session.get('user_email') # Fetch logged-in user's email
 
1108
  def add_to_cart():
1109
  data = request.json
1110
  item_name = data.get('itemName').strip()
1111
+ item_price = data.get('itemPrice') # Get price as string (e.g., "$16.0")
1112
  item_image = data.get('itemImage')
1113
  addons = data.get('addons', [])
1114
  instructions = data.get('instructions', '')
 
1116
  section = data.get('section')
1117
  customer_email = session.get('user_email')
1118
 
1119
+ # Check if the item name and price are provided
1120
  if not item_name or not item_price:
1121
  return jsonify({"success": False, "error": "Item name and price are required."})
1122
 
1123
  try:
1124
+ # Convert item_price to a float after removing any non-numeric characters (e.g., "$")
1125
+ item_price = float(item_price.replace('$', '').strip()) # Ensure it's a numeric value
1126
+
1127
+ # Calculate addons price: Treat addon prices as strings too and convert them to numbers
1128
+ addons_price = sum(float(addon['price'].replace('$', '').strip()) for addon in addons)
1129
+ new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
1130
+
1131
+ # Query to check if the item is already in the cart
1132
  query = f"""
1133
  SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c FROM Cart_Item__c
1134
  WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
 
1136
  result = sf.query(query)
1137
  cart_items = result.get("records", [])
1138
 
 
 
 
 
1139
  if cart_items:
1140
+ # Item already exists, update it
1141
  cart_item_id = cart_items[0]['Id']
1142
  existing_quantity = cart_items[0]['Quantity__c']
1143
  existing_addons = cart_items[0].get('Add_Ons__c', "None")
 
1154
  if instructions:
1155
  combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
1156
 
1157
+ # Calculate the combined addon price
 
1158
  combined_addons_price = sum(
1159
+ float(addon.split("($")[1][:-1]) for addon in combined_addons.split("; ") if "($" in addon
1160
  )
1161
 
1162
+ # Update the cart item with the new quantity and price
1163
  updated_price = (existing_quantity + 1) * item_price + combined_addons_price
1164
  sf.Cart_Item__c.update(cart_item_id, {
1165
  "Quantity__c": existing_quantity + 1,
1166
  "Add_Ons__c": combined_addons,
1167
  "Add_Ons_Price__c": combined_addons_price,
1168
  "Instructions__c": combined_instructions,
1169
+ "Price__c": updated_price,
1170
  "Category__c": category,
1171
  "Section__c": section
1172
  })
1173
  else:
1174
+ # Item is not in the cart, create a new cart item
1175
  addons_string = "None"
1176
  if addons:
1177
  addons_string = new_addons
1178
 
1179
+ # Calculate total price including addons
1180
  total_price = item_price + addons_price
1181
 
1182
  # Create a new cart item in Salesforce
 
1200
  return jsonify({"success": False, "error": str(e)})
1201
 
1202
 
1203
+
1204
  @app.route("/order", methods=["GET"])
1205
  def order_summary():
1206
  email = session.get('user_email') # Fetch logged-in user's email