Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -112,44 +112,21 @@ def filter_menu(preference):
|
|
112 |
|
113 |
return html_content
|
114 |
|
115 |
-
# Function to
|
116 |
-
def
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
order = sf.Order.create({
|
131 |
-
'AccountId': customer_id, # Assuming Account is linked to Customer
|
132 |
-
'Status': 'Draft', # You can set this as per your requirement
|
133 |
-
'OrderNumber': 'Auto-generated', # Use your custom logic if required
|
134 |
-
'Name': f"Order for {customer_name}",
|
135 |
-
'Customer_Email__c': customer_email # Assuming a custom field for email in Order
|
136 |
-
})
|
137 |
-
|
138 |
-
# Create Order Items for each cart item
|
139 |
-
for item in cart:
|
140 |
-
sf.OrderItem.create({
|
141 |
-
'OrderId': order['id'],
|
142 |
-
'Product2Id': item['product_id'], # Assuming you have Product2 Id for each item
|
143 |
-
'Quantity': item['quantity'],
|
144 |
-
'UnitPrice': item['price'],
|
145 |
-
'TotalPrice': item['totalCost'],
|
146 |
-
'Special_Instructions__c': item['instructions'],
|
147 |
-
})
|
148 |
-
|
149 |
-
return "Order successfully placed and saved to Salesforce."
|
150 |
-
|
151 |
-
except Exception as e:
|
152 |
-
return f"Error during order creation: {str(e)}"
|
153 |
|
154 |
# JavaScript for Modal and Cart
|
155 |
def modal_js():
|
@@ -200,39 +177,18 @@ def modal_js():
|
|
200 |
const cartButton = document.getElementById('cart-button');
|
201 |
cartButton.innerText = `View Cart (${cart.length} items)`;
|
202 |
}
|
|
|
|
|
|
|
|
|
|
|
203 |
function proceedToCheckout() {
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
totalCost: item.totalCost,
|
211 |
-
instructions: item.instructions
|
212 |
-
}));
|
213 |
-
|
214 |
-
const email = 'user@example.com'; // Replace with dynamic email
|
215 |
-
|
216 |
-
fetch('/create_order', {
|
217 |
-
method: 'POST',
|
218 |
-
headers: {
|
219 |
-
'Content-Type': 'application/json',
|
220 |
-
},
|
221 |
-
body: JSON.stringify({
|
222 |
-
cart: cartData,
|
223 |
-
email: email
|
224 |
-
})
|
225 |
-
})
|
226 |
-
.then(response => response.json())
|
227 |
-
.then(data => {
|
228 |
-
alert(data.message);
|
229 |
-
if (data.success) {
|
230 |
-
// Optionally redirect to a confirmation page or clear cart
|
231 |
-
}
|
232 |
-
})
|
233 |
-
.catch(error => {
|
234 |
-
alert("Error during checkout: " + error);
|
235 |
-
});
|
236 |
}
|
237 |
</script>
|
238 |
"""
|
@@ -267,7 +223,25 @@ with gr.Blocks() as app:
|
|
267 |
menu_output = gr.HTML()
|
268 |
gr.HTML("<div id='cart-button' style='position: fixed; top: 20px; right: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 30px; cursor: pointer; z-index: 1000;' onclick='openCartModal()'>View Cart</div>")
|
269 |
gr.HTML("<div id='cart-modal' style='display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: white; z-index: 1000; overflow-y: auto;'><div style='padding: 20px;'><div style='text-align: right;'><button onclick='closeCartModal()' style='background: none; border: none; font-size: 24px; cursor: pointer;'>×</button></div><h1>Your Cart</h1><div id='cart-items'></div><p id='cart-total-cost' style='font-size: 1.2em; font-weight: bold;'>Total Cart Cost: $0.00</p><button style='background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;' onclick='proceedToCheckout()'>Proceed to Checkout</button></div></div>")
|
270 |
-
gr.HTML(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
gr.HTML(modal_js())
|
272 |
|
273 |
login_button.click(
|
|
|
112 |
|
113 |
return html_content
|
114 |
|
115 |
+
# Function to handle the cart operations and add to cart
|
116 |
+
def add_to_cart(item_name, item_price, item_quantity, add_ons, special_instructions):
|
117 |
+
cart = []
|
118 |
+
total_cart_cost = 0
|
119 |
+
total_cost = item_price * item_quantity
|
120 |
+
cart.append({
|
121 |
+
"name": item_name,
|
122 |
+
"price": item_price,
|
123 |
+
"quantity": item_quantity,
|
124 |
+
"add_ons": add_ons,
|
125 |
+
"instructions": special_instructions,
|
126 |
+
"total_cost": total_cost
|
127 |
+
})
|
128 |
+
total_cart_cost += total_cost
|
129 |
+
return cart, total_cart_cost
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
# JavaScript for Modal and Cart
|
132 |
def modal_js():
|
|
|
177 |
const cartButton = document.getElementById('cart-button');
|
178 |
cartButton.innerText = `View Cart (${cart.length} items)`;
|
179 |
}
|
180 |
+
function updateCartTotalCost() {
|
181 |
+
const totalCostElement = document.getElementById('cart-total-cost');
|
182 |
+
totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
|
183 |
+
totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
|
184 |
+
}
|
185 |
function proceedToCheckout() {
|
186 |
+
alert("Proceeding to checkout...");
|
187 |
+
}
|
188 |
+
// Reset all selected add-ons when opening a new item modal
|
189 |
+
function resetAddOns() {
|
190 |
+
const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
|
191 |
+
checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
}
|
193 |
</script>
|
194 |
"""
|
|
|
223 |
menu_output = gr.HTML()
|
224 |
gr.HTML("<div id='cart-button' style='position: fixed; top: 20px; right: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 30px; cursor: pointer; z-index: 1000;' onclick='openCartModal()'>View Cart</div>")
|
225 |
gr.HTML("<div id='cart-modal' style='display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: white; z-index: 1000; overflow-y: auto;'><div style='padding: 20px;'><div style='text-align: right;'><button onclick='closeCartModal()' style='background: none; border: none; font-size: 24px; cursor: pointer;'>×</button></div><h1>Your Cart</h1><div id='cart-items'></div><p id='cart-total-cost' style='font-size: 1.2em; font-weight: bold;'>Total Cart Cost: $0.00</p><button style='background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;' onclick='proceedToCheckout()'>Proceed to Checkout</button></div></div>")
|
226 |
+
gr.HTML("""
|
227 |
+
<div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
|
228 |
+
<div style="text-align: right;">
|
229 |
+
<button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">×</button>
|
230 |
+
</div>
|
231 |
+
<img id="modal-image" style="width: 100%; height: 300px; border-radius: 8px; margin-bottom: 20px;" />
|
232 |
+
<h2 id="modal-name"></h2>
|
233 |
+
<p id="modal-description"></p>
|
234 |
+
<p id="modal-price"></p>
|
235 |
+
<label for="biryani-extras"><strong>Add-ons :</strong></label>
|
236 |
+
<div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
|
237 |
+
<!-- Add-on options will be populated here -->
|
238 |
+
</div>
|
239 |
+
<label for="quantity">Quantity:</label>
|
240 |
+
<input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
|
241 |
+
<textarea id="special-instructions" placeholder="Add your special instructions here..." style="width: 100%; height: 60px;"></textarea>
|
242 |
+
<button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
|
243 |
+
</div>
|
244 |
+
""")
|
245 |
gr.HTML(modal_js())
|
246 |
|
247 |
login_button.click(
|