nagasurendra commited on
Commit
5c3fd6b
·
verified ·
1 Parent(s): a86b838

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -26
app.py CHANGED
@@ -164,10 +164,9 @@ def add_to_cart():
164
  return jsonify({"success": False, "error": "Item name and price are required."})
165
 
166
  try:
167
- # Query the cart to check if the item already exists with the same instructions
168
  query = f"""
169
- SELECT Id, Quantity__c, Add_Ons__c, Instructions__c
170
- FROM Cart_Item__c
171
  WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
172
  """
173
  result = sf.query(query)
@@ -177,33 +176,57 @@ def add_to_cart():
177
  addons_price = sum(addon['price'] for addon in addons) # New add-ons price
178
  new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons]) # Format new add-ons
179
 
180
- # Check for the same instructions
181
- matched_item = None
182
- for item in cart_items:
183
- if item.get('Instructions__c', '') == instructions:
184
- matched_item = item
185
- break
186
-
187
- if matched_item:
188
- # If the item already exists with the same instructions, update it
189
- cart_item_id = matched_item['Id']
190
- existing_quantity = matched_item['Quantity__c']
191
-
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  sf.Cart_Item__c.update(cart_item_id, {
193
- "Quantity__c": existing_quantity + 1 # Increase quantity by 1
 
 
 
 
194
  })
195
  else:
196
- # If instructions are different or item doesn't exist, create a new cart item
 
 
 
 
 
 
 
197
  sf.Cart_Item__c.create({
198
- "Name": item_name,
199
- "Price__c": item_price + addons_price,
200
- "Base_Price__c": item_price,
201
- "Quantity__c": 1,
202
- "Add_Ons__c": new_addons,
203
- "Add_Ons_Price__c": addons_price,
204
- "Image1__c": item_image,
205
- "Customer_Email__c": customer_email,
206
- "Instructions__c": instructions # Save the new instructions
207
  })
208
 
209
  return jsonify({"success": True, "message": "Item added to cart successfully."})
 
164
  return jsonify({"success": False, "error": "Item name and price are required."})
165
 
166
  try:
167
+ # Query the cart to check if the item already exists
168
  query = f"""
169
+ SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c FROM Cart_Item__c
 
170
  WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
171
  """
172
  result = sf.query(query)
 
176
  addons_price = sum(addon['price'] for addon in addons) # New add-ons price
177
  new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons]) # Format new add-ons
178
 
179
+ if cart_items:
180
+ # If the item already exists in the cart, update it
181
+ cart_item_id = cart_items[0]['Id']
182
+ existing_quantity = cart_items[0]['Quantity__c']
183
+ existing_addons = cart_items[0].get('Add_Ons__c', "None") # Previous add-ons
184
+ existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0) # Previous add-ons price
185
+ existing_instructions = cart_items[0].get('Instructions__c', "") # Previous instructions
186
+
187
+ # Combine the existing and new add-ons
188
+ combined_addons = existing_addons if existing_addons != "None" else ""
189
+ if new_addons:
190
+ combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
191
+
192
+ # Combine the existing and new instructions
193
+ combined_instructions = existing_instructions
194
+ if instructions:
195
+ combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
196
+
197
+ # Recalculate the total add-ons price
198
+ combined_addons_list = combined_addons.split("; ")
199
+ combined_addons_price = sum(
200
+ float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
201
+ )
202
+
203
+ # Update the item in the cart
204
  sf.Cart_Item__c.update(cart_item_id, {
205
+ "Quantity__c": existing_quantity + 1, # Increase quantity by 1
206
+ "Add_Ons__c": combined_addons, # Update add-ons list
207
+ "Add_Ons_Price__c": combined_addons_price, # Update add-ons price
208
+ "Instructions__c": combined_instructions, # Update instructions
209
+ "Price__c": (existing_quantity + 1) * item_price + combined_addons_price, # Update total price
210
  })
211
  else:
212
+ # If the item does not exist in the cart, create a new one
213
+ addons_string = "None"
214
+ if addons:
215
+ addons_string = new_addons # Use the formatted add-ons string
216
+
217
+ total_price = item_price + addons_price # Base price + add-ons price
218
+
219
+ # Create a new cart item
220
  sf.Cart_Item__c.create({
221
+ "Name": item_name, # Item name
222
+ "Price__c": total_price, # Total price (item + add-ons)
223
+ "Base_Price__c": item_price, # Base price without add-ons
224
+ "Quantity__c": 1, # Default quantity is 1
225
+ "Add_Ons_Price__c": addons_price, # Total add-ons price
226
+ "Add_Ons__c": addons_string, # Add-ons with names and prices
227
+ "Image1__c": item_image, # Item image URL
228
+ "Customer_Email__c": customer_email, # Associated customer's email
229
+ "Instructions__c": instructions # Save instructions
230
  })
231
 
232
  return jsonify({"success": True, "message": "Item added to cart successfully."})