Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -153,12 +153,12 @@ def cart():
|
|
153 |
|
154 |
@app.route('/cart/add', methods=['POST'])
|
155 |
def add_to_cart():
|
156 |
-
data = request.json # Extract JSON payload
|
157 |
-
item_name = data.get('itemName').strip()
|
158 |
item_price = data.get('itemPrice') # Base price of the item
|
159 |
-
item_image = data.get('itemImage')
|
160 |
-
addons = data.get('addons', []) #
|
161 |
-
customer_email = session.get('user_email') # Get
|
162 |
|
163 |
if not item_name or not item_price:
|
164 |
return jsonify({"success": False, "error": "Item name and price are required."})
|
@@ -166,54 +166,57 @@ def add_to_cart():
|
|
166 |
try:
|
167 |
# Query the cart to check if the item already exists
|
168 |
query = f"""
|
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 |
|
|
|
|
|
|
|
|
|
177 |
if cart_items:
|
178 |
-
# If the item exists in the cart
|
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
|
185 |
-
new_addons = "; ".join([
|
186 |
-
f"{addon['name']} (${addon['price']})" for addon in addons
|
187 |
-
])
|
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 |
# Update the item in the cart
|
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":
|
197 |
"Price__c": (existing_quantity + 1) * item_price, # Update total price
|
198 |
})
|
199 |
else:
|
200 |
# If the item does not exist in the cart, create a new one
|
201 |
addons_string = "None"
|
202 |
if addons:
|
203 |
-
addons_string =
|
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
|
217 |
"Image1__c": item_image, # Item image URL
|
218 |
"Customer_Email__c": customer_email, # Associated customer's email
|
219 |
})
|
@@ -224,8 +227,6 @@ def add_to_cart():
|
|
224 |
return jsonify({"success": False, "error": str(e)})
|
225 |
|
226 |
|
227 |
-
|
228 |
-
|
229 |
@app.route("/cart/add_item", methods=["POST"])
|
230 |
def add_item_to_cart():
|
231 |
data = request.json # Extract JSON data from the request
|
|
|
153 |
|
154 |
@app.route('/cart/add', methods=['POST'])
|
155 |
def add_to_cart():
|
156 |
+
data = request.json # Extract JSON payload from frontend
|
157 |
+
item_name = data.get('itemName').strip() # Item name
|
158 |
item_price = data.get('itemPrice') # Base price of the item
|
159 |
+
item_image = data.get('itemImage') # Item image
|
160 |
+
addons = data.get('addons', []) # Add-ons array
|
161 |
+
customer_email = session.get('user_email') # Get logged-in user's email
|
162 |
|
163 |
if not item_name or not item_price:
|
164 |
return jsonify({"success": False, "error": "Item name and price are required."})
|
|
|
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 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 |
|
175 |
+
# Calculate the price of the new add-ons
|
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 |
|
186 |
+
# Combine the existing and new add-ons
|
|
|
|
|
|
|
187 |
combined_addons = existing_addons if existing_addons != "None" else ""
|
188 |
if new_addons:
|
189 |
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
190 |
|
191 |
+
# Recalculate the total add-ons price
|
192 |
+
combined_addons_list = combined_addons.split("; ")
|
193 |
+
combined_addons_price = sum(
|
194 |
+
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
195 |
+
)
|
196 |
+
|
197 |
# Update the item in the cart
|
198 |
sf.Cart_Item__c.update(cart_item_id, {
|
199 |
"Quantity__c": existing_quantity + 1, # Increase quantity by 1
|
200 |
+
"Add_Ons__c": combined_addons, # Update add-ons list
|
201 |
+
"Add_Ons_Price__c": combined_addons_price, # Update add-ons price
|
202 |
"Price__c": (existing_quantity + 1) * item_price, # Update total price
|
203 |
})
|
204 |
else:
|
205 |
# If the item does not exist in the cart, create a new one
|
206 |
addons_string = "None"
|
207 |
if addons:
|
208 |
+
addons_string = new_addons # Use the formatted add-ons string
|
|
|
|
|
209 |
|
210 |
+
total_price = item_price + addons_price # Base price + add-ons price
|
|
|
211 |
|
212 |
+
# Create a new cart item
|
213 |
sf.Cart_Item__c.create({
|
214 |
"Name": item_name, # Item name
|
215 |
"Price__c": total_price, # Total price (item + add-ons)
|
216 |
"Base_Price__c": item_price, # Base price without add-ons
|
217 |
"Quantity__c": 1, # Default quantity is 1
|
218 |
+
"Add_Ons_Price__c": addons_price, # Total add-ons price
|
219 |
+
"Add_Ons__c": addons_string, # Add-ons with names and prices
|
220 |
"Image1__c": item_image, # Item image URL
|
221 |
"Customer_Email__c": customer_email, # Associated customer's email
|
222 |
})
|
|
|
227 |
return jsonify({"success": False, "error": str(e)})
|
228 |
|
229 |
|
|
|
|
|
230 |
@app.route("/cart/add_item", methods=["POST"])
|
231 |
def add_item_to_cart():
|
232 |
data = request.json # Extract JSON data from the request
|