Rammohan0504 commited on
Commit
90e84ea
·
verified ·
1 Parent(s): 7cab842

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -17
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 = 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,15 +1116,14 @@ def add_to_cart():
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()) # Convert price to 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
 
@@ -1137,7 +1136,7 @@ def add_to_cart():
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,29 +1153,26 @@ def add_to_cart():
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,7 +1196,6 @@ def add_to_cart():
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
 
1108
  def add_to_cart():
1109
  data = request.json
1110
  item_name = data.get('itemName').strip()
1111
+ item_price = data.get('itemPrice') # This will be received as a 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
  if not item_name or not item_price:
1120
  return jsonify({"success": False, "error": "Item name and price are required."})
1121
 
1122
  try:
1123
+ # Convert item_price to a float (if it's a string like "$16.0", remove '$' and convert to float)
1124
+ item_price = float(item_price.replace('$', '').strip()) # Convert to numeric value
1125
 
1126
+ # Calculate the price of add-ons (ensure the price is handled as float)
1127
  addons_price = sum(float(addon['price'].replace('$', '').strip()) for addon in addons)
1128
  new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
1129
 
 
1136
  cart_items = result.get("records", [])
1137
 
1138
  if cart_items:
1139
+ # If item exists in the cart, update it
1140
  cart_item_id = cart_items[0]['Id']
1141
  existing_quantity = cart_items[0]['Quantity__c']
1142
  existing_addons = cart_items[0].get('Add_Ons__c', "None")
 
1153
  if instructions:
1154
  combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
1155
 
1156
+ # Calculate the total price including the add-ons
1157
+ updated_price = (existing_quantity + 1) * item_price + addons_price
 
 
1158
 
1159
+ # Update the cart item in the database
 
1160
  sf.Cart_Item__c.update(cart_item_id, {
1161
  "Quantity__c": existing_quantity + 1,
1162
  "Add_Ons__c": combined_addons,
1163
+ "Add_Ons_Price__c": addons_price,
1164
  "Instructions__c": combined_instructions,
1165
+ "Price__c": updated_price, # Updated price based on quantity and add-ons
1166
  "Category__c": category,
1167
  "Section__c": section
1168
  })
1169
  else:
1170
+ # If the item is not in the cart, create a new cart item
1171
  addons_string = "None"
1172
  if addons:
1173
  addons_string = new_addons
1174
 
1175
+ # Calculate total price including add-ons
1176
  total_price = item_price + addons_price
1177
 
1178
  # Create a new cart item in Salesforce
 
1196
  return jsonify({"success": False, "error": str(e)})
1197
 
1198
 
 
1199
  @app.route("/order", methods=["GET"])
1200
  def order_summary():
1201
  email = session.get('user_email') # Fetch logged-in user's email