nagasurendra commited on
Commit
33cad0b
·
verified ·
1 Parent(s): 221944a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -21
app.py CHANGED
@@ -149,7 +149,6 @@ def cart():
149
  return render_template("cart.html", cart_items=[], subtotal=0)
150
 
151
 
152
-
153
  @app.route('/cart/add', methods=['POST'])
154
  def add_to_cart():
155
  data = request.json # Extract JSON payload
@@ -162,28 +161,55 @@ def add_to_cart():
162
  if not item_name or not item_price:
163
  return jsonify({"success": False, "error": "Item name and price are required."})
164
 
165
- # Convert add-ons array to a formatted string and calculate the add-ons total price
166
- addons_string = "None"
167
- addons_total_price = 0
168
- if addons:
169
- addons_details = [f"{addon['name']} (${addon['price']})" for addon in addons]
170
- addons_total_price = sum(addon['price'] for addon in addons)
171
- addons_string = "; ".join(addons_details)
 
172
 
173
- # Calculate the total price (item price + add-ons)
174
- total_price = item_price + addons_total_price
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
- try:
177
- # Save the cart item in Salesforce
178
- sf.Cart_Item__c.create({
179
- "Name": item_name, # Item name
180
- "Price__c": total_price, # Total price (item + add-ons)
181
- "Base_Price__c": item_price, # Base price without add-ons
182
- "Quantity__c": 1, # Default quantity is 1
183
- "Add_Ons__c": addons_string, # Add-ons with name and price
184
- "Image1__c": item_image, # Item image URL
185
- "Customer_Email__c": customer_email, # Associated customer's email
186
- })
187
  return jsonify({"success": True, "message": "Item added to cart successfully."})
188
  except Exception as e:
189
  print(f"Error adding item to cart: {str(e)}")
@@ -191,6 +217,7 @@ def add_to_cart():
191
 
192
 
193
 
 
194
  @app.route("/cart/add_item", methods=["POST"])
195
  def add_item_to_cart():
196
  data = request.json # Extract JSON data from the request
 
149
  return render_template("cart.html", cart_items=[], subtotal=0)
150
 
151
 
 
152
  @app.route('/cart/add', methods=['POST'])
153
  def add_to_cart():
154
  data = request.json # Extract JSON payload
 
161
  if not item_name or not item_price:
162
  return jsonify({"success": False, "error": "Item name and price are required."})
163
 
164
+ try:
165
+ # Query the cart to check if the item already exists
166
+ query = f"""
167
+ SELECT Id, Quantity__c, Add_Ons__c FROM Cart_Item__c
168
+ WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
169
+ """
170
+ result = sf.query(query)
171
+ cart_items = result.get("records", [])
172
 
173
+ if cart_items:
174
+ # If the item exists in the cart
175
+ cart_item_id = cart_items[0]['Id']
176
+ existing_quantity = cart_items[0]['Quantity__c']
177
+ existing_addons = cart_items[0].get('Add_Ons__c', "None")
178
+
179
+ # Combine existing add-ons with new add-ons
180
+ new_addons = "; ".join([
181
+ f"{addon['name']} (${addon['price']})" for addon in addons
182
+ ])
183
+ combined_addons = existing_addons if existing_addons != "None" else ""
184
+ if new_addons:
185
+ combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
186
+
187
+ # Update the item in the cart
188
+ sf.Cart_Item__c.update(cart_item_id, {
189
+ "Quantity__c": existing_quantity + 1, # Increase quantity by 1
190
+ "Add_Ons__c": combined_addons, # Update add-ons
191
+ "Price__c": (existing_quantity + 1) * item_price, # Update total price
192
+ })
193
+ else:
194
+ # If the item does not exist in the cart, create a new one
195
+ addons_string = "None"
196
+ if addons:
197
+ addons_string = "; ".join([
198
+ f"{addon['name']} (${addon['price']})" for addon in addons
199
+ ])
200
+
201
+ total_price = item_price + sum(addon['price'] for addon in addons)
202
+
203
+ sf.Cart_Item__c.create({
204
+ "Name": item_name, # Item name
205
+ "Price__c": total_price, # Total price (item + add-ons)
206
+ "Base_Price__c": item_price, # Base price without add-ons
207
+ "Quantity__c": 1, # Default quantity is 1
208
+ "Add_Ons__c": addons_string, # Add-ons with name and price
209
+ "Image1__c": item_image, # Item image URL
210
+ "Customer_Email__c": customer_email, # Associated customer's email
211
+ })
212
 
 
 
 
 
 
 
 
 
 
 
 
213
  return jsonify({"success": True, "message": "Item added to cart successfully."})
214
  except Exception as e:
215
  print(f"Error adding item to cart: {str(e)}")
 
217
 
218
 
219
 
220
+
221
  @app.route("/cart/add_item", methods=["POST"])
222
  def add_item_to_cart():
223
  data = request.json # Extract JSON data from the request