Spaces:
Runtime error
Runtime error
Update Cart_Page.py
Browse files- Cart_Page.py +132 -0
Cart_Page.py
CHANGED
@@ -111,3 +111,135 @@ def cart():
|
|
111 |
print(f"Error fetching cart items: {e}")
|
112 |
return render_template("cart.html", cart_items=[], subtotal=0, reward_points=0, coupons=[], suggestions=[])
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
print(f"Error fetching cart items: {e}")
|
112 |
return render_template("cart.html", cart_items=[], subtotal=0, reward_points=0, coupons=[], suggestions=[])
|
113 |
|
114 |
+
|
115 |
+
|
116 |
+
@cart_page.route("/cart/add_suggestion_to_cart", methods=["POST"])
|
117 |
+
def add_suggestion_to_cart():
|
118 |
+
try:
|
119 |
+
# Get data from the request
|
120 |
+
data = request.get_json()
|
121 |
+
item_name = data.get('item_name').strip()
|
122 |
+
item_price = data.get('item_price')
|
123 |
+
item_image = data.get('item_image')
|
124 |
+
item_id = data.get('item_id')
|
125 |
+
customer_email = data.get('customer_email')
|
126 |
+
addons = data.get('addons', [])
|
127 |
+
instructions = data.get('instructions', "")
|
128 |
+
|
129 |
+
# Default values if addons and instructions are not provided
|
130 |
+
addons_price = 0
|
131 |
+
addons_string = "None"
|
132 |
+
|
133 |
+
# Check if the customer already has this item in their cart
|
134 |
+
query = f"""
|
135 |
+
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
136 |
+
FROM Cart_Item__c
|
137 |
+
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
138 |
+
"""
|
139 |
+
result = sf.query(query)
|
140 |
+
cart_items = result.get("records", [])
|
141 |
+
|
142 |
+
# If item already exists in the cart, update its quantity and other details
|
143 |
+
if cart_items:
|
144 |
+
cart_item_id = cart_items[0]['Id']
|
145 |
+
existing_quantity = cart_items[0]['Quantity__c']
|
146 |
+
existing_addons = cart_items[0].get('Add_Ons__c', "None")
|
147 |
+
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
148 |
+
existing_instructions = cart_items[0].get('Instructions__c', "")
|
149 |
+
|
150 |
+
# Combine existing and new addons
|
151 |
+
combined_addons = existing_addons if existing_addons != "None" else ""
|
152 |
+
if addons:
|
153 |
+
combined_addons = f"{combined_addons}; {addons}".strip("; ")
|
154 |
+
|
155 |
+
combined_instructions = existing_instructions
|
156 |
+
if instructions:
|
157 |
+
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
158 |
+
|
159 |
+
combined_addons_list = combined_addons.split("; ")
|
160 |
+
combined_addons_price = sum(
|
161 |
+
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
162 |
+
)
|
163 |
+
|
164 |
+
# Update the cart item
|
165 |
+
sf.Cart_Item__c.update(cart_item_id, {
|
166 |
+
"Quantity__c": existing_quantity + 1,
|
167 |
+
"Add_Ons__c": combined_addons,
|
168 |
+
"Add_Ons_Price__c": combined_addons_price,
|
169 |
+
"Instructions__c": combined_instructions,
|
170 |
+
"Price__c": (existing_quantity + 1) * float(item_price) + combined_addons_price
|
171 |
+
})
|
172 |
+
else:
|
173 |
+
# If item doesn't exist in cart, create a new cart item
|
174 |
+
total_price = float(item_price) + addons_price
|
175 |
+
|
176 |
+
# Create a new cart item in Salesforce
|
177 |
+
sf.Cart_Item__c.create({
|
178 |
+
"Name": item_name,
|
179 |
+
"Price__c": total_price,
|
180 |
+
"Base_Price__c": item_price,
|
181 |
+
"Quantity__c": 1,
|
182 |
+
"Add_Ons_Price__c": addons_price,
|
183 |
+
"Add_Ons__c": addons_string,
|
184 |
+
"Image1__c": item_image,
|
185 |
+
"Customer_Email__c": customer_email,
|
186 |
+
"Instructions__c": instructions
|
187 |
+
})
|
188 |
+
|
189 |
+
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
190 |
+
|
191 |
+
except Exception as e:
|
192 |
+
print(f"Error adding item to cart: {str(e)}")
|
193 |
+
return jsonify({"success": False, "error": str(e)})
|
194 |
+
|
195 |
+
|
196 |
+
|
197 |
+
@cart_page.route("/cart/add_item", methods=["POST"])
|
198 |
+
def add_item_to_cart():
|
199 |
+
data = request.json # Extract JSON data from the request
|
200 |
+
email = data.get('email') # Customer email
|
201 |
+
item_name = data.get('item_name') # Item name
|
202 |
+
quantity = data.get('quantity', 1) # Quantity to add (default is 1)
|
203 |
+
addons = data.get('addons', []) # Add-ons for the item (optional)
|
204 |
+
|
205 |
+
# Validate inputs
|
206 |
+
if not email or not item_name:
|
207 |
+
return jsonify({"success": False, "error": "Email and item name are required."}), 400
|
208 |
+
|
209 |
+
try:
|
210 |
+
# Add a new item to the cart with the provided details
|
211 |
+
sf.Cart_Item__c.create({
|
212 |
+
"Customer_Email__c": email, # Associate the cart item with the customer's email
|
213 |
+
"Item_Name__c": item_name, # Item name
|
214 |
+
"Quantity__c": quantity, # Quantity to add
|
215 |
+
"Add_Ons__c": addons_string
|
216 |
+
})
|
217 |
+
|
218 |
+
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
219 |
+
except Exception as e:
|
220 |
+
print(f"Error adding item to cart: {str(e)}") # Log the error for debugging
|
221 |
+
return jsonify({"success": False, "error": str(e)}), 500
|
222 |
+
|
223 |
+
|
224 |
+
|
225 |
+
@cart_page.route('/cart/remove/<item_name>', methods=['POST'])
|
226 |
+
def remove_cart_item(item_name):
|
227 |
+
try:
|
228 |
+
customer_email = session.get('user_email')
|
229 |
+
if not customer_email:
|
230 |
+
return jsonify({'success': False, 'message': 'User email not found. Please log in again.'}), 400
|
231 |
+
query = f"""
|
232 |
+
SELECT Id FROM Cart_Item__c
|
233 |
+
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
234 |
+
"""
|
235 |
+
result = sf.query(query)
|
236 |
+
if result['totalSize'] == 0:
|
237 |
+
return jsonify({'success': False, 'message': 'Item not found in cart.'}), 400
|
238 |
+
cart_item_id = result['records'][0]['Id']
|
239 |
+
sf.Cart_Item__c.delete(cart_item_id)
|
240 |
+
return jsonify({'success': True, 'message': f"'{item_name}' removed successfully!"}), 200
|
241 |
+
except Exception as e:
|
242 |
+
print(f"Error: {str(e)}")
|
243 |
+
return jsonify({'success': False, 'message': f"An error occurred: {str(e)}"}), 500
|
244 |
+
|
245 |
+
|