DSatishchandra commited on
Commit
22c7e1a
·
verified ·
1 Parent(s): 9a49712

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +297 -19
templates/cart.html CHANGED
@@ -1,26 +1,304 @@
1
  <!DOCTYPE html>
2
- <html>
3
  <head>
 
 
4
  <title>Cart</title>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  </head>
6
  <body>
7
- <h1>Biryani Hub - Cart</h1>
8
- <form method="POST" action="/place_order">
9
- <ul>
10
- {% for item in cart %}
11
- <li>{{ item.Name }} - ${{ item.Price }} x {{ item.Quantity }}</li>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  {% endfor %}
13
- </ul>
14
- <p>Total: ${{ total }}</p>
15
- {% if error %}
16
- <p style="color: red;">{{ error }}</p>
17
- {% endif %}
18
- {% if success %}
19
- <p style="color: green;">{{ success }}</p>
20
- {% endif %}
21
- <input type="email" name="email" placeholder="Enter your email" required>
22
- <button type="submit">Place Order</button>
23
- </form>
24
- <a href="/menu">Back to Menu</a>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  </body>
26
- </html>
 
1
  <!DOCTYPE html>
2
+ <html lang="en">
3
  <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>Cart</title>
7
+ <!-- Bootstrap CSS -->
8
+ <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
9
+ <style>
10
+ body {
11
+ font-family: Arial, sans-serif;
12
+ background-color: #f8f9fa;
13
+ }
14
+ .cart-container {
15
+ max-width: 768px;
16
+ margin: 20px auto;
17
+ padding: 15px;
18
+ background-color: #fff;
19
+ border-radius: 10px;
20
+ box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
21
+ }
22
+ .cart-item {
23
+ display: flex;
24
+ align-items: center;
25
+ justify-content: space-between;
26
+ border-bottom: 1px solid #dee2e6;
27
+ padding: 10px 0;
28
+ }
29
+ .cart-item img {
30
+ width: 70px;
31
+ height: 70px;
32
+ object-fit: cover;
33
+ border-radius: 5px;
34
+ }
35
+ .cart-item-details {
36
+ flex: 1;
37
+ margin-left: 15px;
38
+ }
39
+ .cart-item-title {
40
+ font-size: 1rem;
41
+ font-weight: bold;
42
+ }
43
+ .cart-item-quantity {
44
+ display: flex;
45
+ align-items: center;
46
+ margin-top: 5px;
47
+ }
48
+ .cart-item-quantity button {
49
+ width: 30px;
50
+ height: 30px;
51
+ border: none;
52
+ background-color: #f0f0f0;
53
+ font-size: 1rem;
54
+ font-weight: bold;
55
+ cursor: pointer;
56
+ }
57
+ .cart-item-quantity input {
58
+ width: 40px;
59
+ text-align: center;
60
+ border: none;
61
+ background-color: #f8f9fa;
62
+ font-size: 1rem;
63
+ margin: 0 5px;
64
+ }
65
+ .cart-summary {
66
+ text-align: right;
67
+ margin-top: 15px;
68
+ }
69
+ .checkout-button {
70
+ background-color: #007bff;
71
+ color: #fff;
72
+ padding: 10px;
73
+ border-radius: 5px;
74
+ border: none;
75
+ width: 100%;
76
+ font-size: 1.2rem;
77
+ cursor: pointer;
78
+ margin-top: 10px;
79
+ }
80
+ .suggestion-section {
81
+ margin-top: 20px;
82
+ padding: 15px;
83
+ background-color: #f8f9fa;
84
+ border-radius: 10px;
85
+ }
86
+ .suggestion-item {
87
+ display: flex;
88
+ align-items: center;
89
+ justify-content: space-between;
90
+ padding: 10px 0;
91
+ }
92
+ .suggestion-item img {
93
+ width: 50px;
94
+ height: 50px;
95
+ object-fit: cover;
96
+ border-radius: 5px;
97
+ }
98
+ .add-back-button {
99
+ padding: 5px 15px;
100
+ font-size: 0.9rem;
101
+ border-radius: 20px;
102
+ border: 1px solid #007bff;
103
+ color: #007bff;
104
+ background-color: #fff;
105
+ cursor: pointer;
106
+ }
107
+ .add-back-button:hover {
108
+ background-color: #007bff;
109
+ color: #fff;
110
+ }
111
+ </style>
112
  </head>
113
  <body>
114
+ <div class="container">
115
+ <div class="cart-container">
116
+ <h4 class="mb-4">Your Cart</h4>
117
+
118
+ <!-- Cart Items -->
119
+ {% for item in cart_items %}
120
+ <div class="cart-item">
121
+ <img src="{{ item.Image1__c }}" alt="{{ item.Name }}" onerror="this.src='/static/placeholder.jpg';">
122
+ <div class="cart-item-details">
123
+ <div class="cart-item-title">{{ item.Name }}</div>
124
+ <div class="cart-item-quantity mt-2">
125
+ <!-- Decrease button -->
126
+ <button onclick="updateQuantity('decrease', '{{ item.Name }}', '{{ customer_email }}')">-</button>
127
+
128
+ <!-- Quantity input field -->
129
+ <input type="text" value="{{ item.Quantity__c }}" readonly data-item-name="{{ item.Name }}">
130
+
131
+ <!-- Increase button -->
132
+ <button onclick="updateQuantity('increase', '{{ item.Name }}', '{{ customer_email }}')">+</button>
133
+ </div>
134
+ </div>
135
+ <div class="cart-item-actions">
136
+ <div class="text-primary">${{ item.Price__c }}</div>
137
+ <button class="btn btn-danger btn-sm" onclick="removeItemFromCart('{{ item.Name }}')">Remove</button>
138
+ </div>
139
+ </div>
140
+ {% else %}
141
+ <p class="text-center">Your cart is empty.</p>
142
  {% endfor %}
143
+
144
+ <!-- Subtotal -->
145
+ <div class="cart-summary">
146
+ <p class="fw-bold">Subtotal: ${{ subtotal }}</p>
147
+ <button class="checkout-button" onclick="proceedToOrder()">Proceed to Order</button>
148
+ </div>
149
+ </div>
150
+
151
+ <!-- Suggestions Section -->
152
+ <div class="suggestion-section">
153
+
154
+ {% for suggestion in suggestions %}
155
+ <div class="suggestion-item">
156
+ <img src="{{ suggestion.Image1__c }}" alt="{{ suggestion.Name }}" onerror="this.src='/static/placeholder.jpg';">
157
+ <div>
158
+ <div>{{ suggestion.Name }}</div>
159
+ <div class="text-muted">${{ suggestion.Price__c }}</div>
160
+ </div>
161
+ <button class="add-back-button" onclick="addSuggestion('{{ suggestion.Id }}')">Add</button>
162
+ </div>
163
+ {% endfor %}
164
+ </div>
165
+ </div>
166
+
167
+ <script>
168
+ function updateQuantity(action, itemName, price, customerEmail) {
169
+ const inputField = document.querySelector(`input[data-item-name="${itemName}"]`);
170
+ const subtotalField = document.querySelector('.cart-summary p');
171
+ let currentQuantity = parseInt(inputField.value);
172
+ // Adjust quantity based on the action
173
+ if (action === 'increase') {
174
+ currentQuantity += 1;
175
+ } else if (action === 'decrease' && currentQuantity > 1) {
176
+ currentQuantity -= 1;
177
+ }else{
178
+ alert("Quantity cannot be less than 1!");
179
+ return;
180
+ }
181
+
182
+ // Update the input field value locally
183
+ inputField.value = currentQuantity;
184
+
185
+ // Debugging: Log API request payload
186
+ console.log("Updating Quantity:", {
187
+ email: customerEmail,
188
+ item_name: itemName,
189
+ action: action,
190
+ price:price,
191
+ quantity: currentQuantity
192
+ });
193
+ // Calculate new subtotal
194
+ let newSubtotal = calculateSubtotal(); // Correctly declare the newSubtotal variable
195
+ subtotalField.innerText = `Subtotal: $${newSubtotal.toFixed(2)}`;
196
+ // Correctly append the itemId to the URL
197
+ // Make the server request to update the quantity
198
+ // Make API call to update the quantity on the backend
199
+ fetch(`/cart/update_quantity`, {
200
+ method: "POST",
201
+ headers: { "Content-Type": "application/json" },
202
+ body: JSON.stringify({ email: customerEmail,
203
+ item_name: itemName, action: action, quantity:currentQuantity }) //send the current quantity
204
+ })
205
+
206
+ .then(response => response.json())
207
+ .then(data => {
208
+ if (!data.success) {
209
+ alert(data.error || "Failed to update quantity.");
210
+ } else {
211
+ console.log(`Quantity updated for ${itemName}: ${currentQuantity}`);
212
+ }
213
+ })
214
+ .catch(err => console.error("Error updating quantity:", err));
215
+ }
216
+
217
+
218
+ function calculateSubtotal() {
219
+ let subtotal = 0;
220
+ document.querySelectorAll('.cart-item').forEach(item => {
221
+ const quantity = parseInt(item.querySelector('input').value);
222
+ const price = parseFloat(item.querySelector('.text-primary').innerText.replace('$', ''));
223
+ subtotal += quantity * price;
224
+ });
225
+ return subtotal;
226
+ }
227
+
228
+ function proceedToOrder() {
229
+ fetch('/checkout', {
230
+ method: 'POST',
231
+ })
232
+ .then(response => response.json())
233
+ .then(data => {
234
+ if (data.success) {
235
+ alert(data.message);
236
+ window.location.href = '/menu'; // Redirect to menu or order confirmation page
237
+ } else {
238
+ alert(data.error || data.message);
239
+ }
240
+ })
241
+ .catch(err => console.error('Error during checkout:', err));
242
+ }
243
+ function addSuggestion(itemId) {
244
+ fetch(`/cart/add_suggestion/${itemId}`, {
245
+ method: 'POST',
246
+ headers: { 'Content-Type': 'application/json' },
247
+ body: JSON.stringify({})
248
+ })
249
+ .then(response => response.json())
250
+ .then(data => {
251
+ if (data.success) {
252
+ alert('Item added to cart!');
253
+ location.reload();
254
+ } else {
255
+ alert(data.error);
256
+ }
257
+ })
258
+ .catch(err => console.error('Error adding suggestion:', err));
259
+ }
260
+ function removeItemFromCart(itemName) {
261
+ fetch(`/cart/remove/${encodeURIComponent(itemName)}`, {
262
+ method: 'POST',
263
+ headers: {
264
+ 'Content-Type': 'application/json'
265
+ }
266
+ })
267
+ .then(response => response.json())
268
+ .then(data => {
269
+ if (data.success) {
270
+ alert(data.message);
271
+ location.reload(); // Reload the page to update the cart
272
+ } else {
273
+ alert(data.message);
274
+ }
275
+ })
276
+ .catch(err => console.error('Error removing item:', err));
277
+ }
278
+ function addToCart(itemName, customerEmail) {
279
+ fetch(`/cart/add_item`, {
280
+ method: "POST",
281
+ headers: { "Content-Type": "application/json" },
282
+ body: JSON.stringify({
283
+ email: customerEmail,
284
+ item_name: itemName.trim(), //Ensure the item name is trimmed
285
+ quantity: 0 // DEFAULT QUANTITY PASSED HERE
286
+ })
287
+ })
288
+ .then(response => response.json())
289
+ .then(data => {
290
+ if (data.success) {
291
+ alert("Item added/updated successfully.");
292
+ location.reload(); // Reload the page to update the cart
293
+ } else {
294
+ alert(data.error || "Failed to add item to cart.");
295
+ }
296
+ })
297
+ .catch(err => console.error("Error adding item to cart:", err));
298
+ }
299
+
300
+
301
+ </script>
302
+
303
  </body>
304
+ </html>