Rammohan0504 commited on
Commit
55390a4
·
verified ·
1 Parent(s): 750c1a8

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +147 -101
templates/cart.html CHANGED
@@ -3,7 +3,8 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Your Cart</title>
 
7
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
8
  <style>
9
  body {
@@ -11,7 +12,7 @@
11
  background-color: #f8f9fa;
12
  }
13
  .cart-container {
14
- max-width: 800px;
15
  margin: 20px auto;
16
  padding: 15px;
17
  background-color: #fff;
@@ -20,8 +21,8 @@
20
  }
21
  .cart-item {
22
  display: flex;
23
- justify-content: space-between;
24
  align-items: center;
 
25
  border-bottom: 1px solid #dee2e6;
26
  padding: 10px 0;
27
  }
@@ -36,12 +37,13 @@
36
  margin-left: 15px;
37
  }
38
  .cart-item-title {
39
- font-size: 1.1rem;
40
  font-weight: bold;
41
  }
42
  .cart-item-quantity {
43
  display: flex;
44
  align-items: center;
 
45
  }
46
  .cart-item-quantity button {
47
  width: 30px;
@@ -64,173 +66,217 @@
64
  text-align: right;
65
  margin-top: 15px;
66
  }
67
- .coupon-section {
68
- display: flex;
69
- justify-content: space-between;
70
- align-items: center;
71
- margin-top: 20px;
72
- }
73
- .apply-button {
74
- background-color: #28a745;
75
- color: #fff;
76
- padding: 5px 20px;
77
- font-size: 1rem;
78
- cursor: pointer;
79
- border-radius: 5px;
80
- }
81
- .apply-button:disabled {
82
- background-color: #6c757d;
83
- }
84
  .checkout-button {
85
  background-color: #007bff;
86
  color: #fff;
87
  padding: 10px;
88
  border-radius: 5px;
 
89
  width: 100%;
90
  font-size: 1.2rem;
91
  cursor: pointer;
92
  margin-top: 10px;
93
  }
94
- .error-message {
95
- color: red;
96
- margin-top: 10px;
 
 
 
 
 
 
 
 
 
 
 
 
97
  }
98
  </style>
99
  </head>
100
  <body>
101
  <div class="container">
102
  <div class="cart-container">
103
- <h3>Your Cart</h3>
104
-
 
 
 
 
105
  <!-- Cart Items -->
106
  {% for item in cart_items %}
107
- <div class="cart-item">
108
  <img src="{{ item.Image1__c }}" alt="{{ item.Name }}" onerror="this.src='/static/placeholder.jpg';">
109
  <div class="cart-item-details">
110
  <div class="cart-item-title">{{ item.Name }}</div>
111
- <div class="cart-item-quantity">
 
 
 
 
 
 
112
  <button onclick="updateQuantity('decrease', '{{ item.Name }}')">-</button>
113
- <input type="text" value="{{ item.Quantity__c }}" readonly>
114
  <button onclick="updateQuantity('increase', '{{ item.Name }}')">+</button>
115
  </div>
116
  </div>
117
  <div class="cart-item-actions">
118
  <div class="text-primary">
119
- $<span class="item-price">{{ item.Price__c }}</span>
120
  </div>
121
- <button class="btn btn-danger btn-sm" onclick="removeItem('{{ item.Name }}')">Remove</button>
122
  </div>
123
  </div>
 
 
124
  {% endfor %}
125
 
126
- <!-- Coupon Code Section -->
127
- <div class="coupon-section">
128
- <select id="couponCode" class="form-control" style="width: 70%;"></select>
129
- <button class="apply-button" onclick="applyCoupon()">Apply Coupon</button>
 
130
  </div>
131
 
132
- <div class="error-message" id="couponError"></div>
133
-
134
- <!-- Cart Summary -->
135
- <div class="cart-summary">
136
- <p><strong>Subtotal: </strong><span id="subtotal">${{ subtotal }}</span></p>
137
- <p><strong>Discount: </strong><span id="discount">0.00</span></p>
138
- <p><strong>Total: </strong><span id="total">$<span id="total-price">{{ total_price }}</span></span></p>
 
139
  </div>
140
 
141
- <!-- Checkout Button -->
142
  <button class="checkout-button" onclick="proceedToOrder()">Proceed to Order</button>
143
  </div>
144
  </div>
145
 
146
  <script>
147
- // Fetch the coupon codes related to the logged-in user and populate the dropdown
148
- document.addEventListener("DOMContentLoaded", function() {
149
- const customerEmail = "{{ customer_email }}"; // Assuming email is passed to the template
150
-
151
- fetch(`/get_coupons/${customerEmail}`)
 
 
 
 
152
  .then(response => response.json())
153
  .then(data => {
154
  if (data.success) {
155
- let couponDropdown = document.getElementById("couponCode");
156
  data.coupons.forEach(coupon => {
157
- let option = document.createElement("option");
158
  option.value = coupon.Coupon_Code__c;
159
- option.text = coupon.Coupon_Code__c;
160
- couponDropdown.appendChild(option);
161
  });
162
  } else {
163
- document.getElementById('couponError').innerText = "No coupons available.";
164
  }
165
  })
166
- .catch(error => console.error("Error fetching coupons:", error));
167
  });
168
 
169
- // Update item quantity
170
  function updateQuantity(action, itemName) {
171
- let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
172
  let quantity = parseInt(quantityInput.value);
 
173
  if (action === 'increase') {
174
  quantity++;
175
  } else if (action === 'decrease' && quantity > 1) {
176
  quantity--;
177
  }
178
- if (quantity < 1) {
179
- quantity = 1;
180
- }
181
- quantityInput.value = quantity;
182
- // Update price and subtotal
183
- updateCart(itemName, quantity);
 
 
 
 
 
 
 
 
 
 
184
  }
185
 
186
- // Apply coupon
187
- function applyCoupon() {
188
- let couponCode = document.getElementById('couponCode').value;
189
- if (!couponCode) {
190
- document.getElementById('couponError').innerText = "Please select a coupon.";
191
- return;
192
- }
 
 
 
 
 
 
 
 
 
193
 
194
- fetch(`/apply_coupon`, {
 
195
  method: 'POST',
196
- headers: { 'Content-Type': 'application/json' },
197
- body: JSON.stringify({ coupon_code: couponCode })
198
  })
199
  .then(response => response.json())
200
  .then(data => {
201
  if (data.success) {
202
- let discount = data.discount;
203
- document.getElementById('discount').innerText = discount.toFixed(2);
204
- let subtotal = parseFloat(document.getElementById('subtotal').innerText.replace('$', ''));
205
- let total = subtotal - discount;
206
- document.getElementById('total-price').innerText = total.toFixed(2);
207
- document.getElementById('couponError').innerText = `Coupon applied! You saved $${discount.toFixed(2)}`;
208
  } else {
209
- document.getElementById('couponError').innerText = "Invalid coupon code.";
210
  }
211
  })
212
- .catch(error => console.error("Error applying coupon:", error));
213
- }
214
-
215
- // Update the cart prices based on the quantity changes
216
- function updateCart(itemName, quantity) {
217
- let price = parseFloat(document.querySelector(`.item[data-item-name="${itemName}"] .item-price`).innerText.replace('$', ''));
218
- let newPrice = price * quantity;
219
- document.querySelector(`.item[data-item-name="${itemName}"] .item-price`).innerText = newPrice.toFixed(2);
220
- // Update subtotal and total
221
- let subtotal = 0;
222
- document.querySelectorAll('.item').forEach(item => {
223
- subtotal += parseFloat(item.querySelector('.item-price').innerText.replace('$', ''));
224
- });
225
- document.getElementById('subtotal').innerText = "$" + subtotal.toFixed(2);
226
- let discount = parseFloat(document.getElementById('discount').innerText);
227
- let total = subtotal - discount;
228
- document.getElementById('total-price').innerText = total.toFixed(2);
229
- }
230
-
231
- // Proceed to the order summary page
232
- function proceedToOrder() {
233
- window.location.href = '/order_summary'; // Redirect to order summary page
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  }
235
  </script>
236
  </body>
 
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/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
9
  <style>
10
  body {
 
12
  background-color: #f8f9fa;
13
  }
14
  .cart-container {
15
+ max-width: 768px;
16
  margin: 20px auto;
17
  padding: 15px;
18
  background-color: #fff;
 
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
  }
 
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;
 
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
+ .apply-coupon {
81
+ margin-top: 20px;
82
+ }
83
+ .apply-button {
84
+ background-color: #28a745;
85
+ color: #fff;
86
+ padding: 5px 20px;
87
+ font-size: 1rem;
88
+ font-weight: bold;
89
+ cursor: pointer;
90
+ border-radius: 5px;
91
+ border: none;
92
+ }
93
+ .apply-button:hover {
94
+ background-color: #218838;
95
  }
96
  </style>
97
  </head>
98
  <body>
99
  <div class="container">
100
  <div class="cart-container">
101
+ <div style="text-align: right;">
102
+ <a href="/menu" style="text-decoration: none; font-size: 1.5rem; color: #007bff;">&times;</a>
103
+ </div>
104
+
105
+ <h4 class="mb-4">Your Cart</h4>
106
+
107
  <!-- Cart Items -->
108
  {% for item in cart_items %}
109
+ <div class="cart-item" data-item-name="{{ item.Name }}">
110
  <img src="{{ item.Image1__c }}" alt="{{ item.Name }}" onerror="this.src='/static/placeholder.jpg';">
111
  <div class="cart-item-details">
112
  <div class="cart-item-title">{{ item.Name }}</div>
113
+ <div class="cart-item-addons">
114
+ <small class="text-muted">Add-ons: {{ item.Add_Ons__c }}</small>
115
+ </div>
116
+ <div class="cart-item-instructions">
117
+ <small class="text-muted">Instructions: {{ item.Instructions__c or "None" }}</small>
118
+ </div>
119
+ <div class="cart-item-quantity mt-2">
120
  <button onclick="updateQuantity('decrease', '{{ item.Name }}')">-</button>
121
+ <input type="text" value="{{ item.Quantity__c }}" readonly data-item-name="{{ item.Name }}">
122
  <button onclick="updateQuantity('increase', '{{ item.Name }}')">+</button>
123
  </div>
124
  </div>
125
  <div class="cart-item-actions">
126
  <div class="text-primary">
127
+ $<span class="base-price">{{ item.Price__c }}</span>
128
  </div>
129
+ <button class="btn btn-danger btn-sm" onclick="removeItemFromCart('{{ item.Name }}')">Remove</button>
130
  </div>
131
  </div>
132
+ {% else %}
133
+ <p class="text-center">Your cart is empty.</p>
134
  {% endfor %}
135
 
136
+ <!-- Subtotal -->
137
+ <div class="cart-summary">
138
+ <p class="fw-bold" id="subtotal-text">Subtotal: ${{ subtotal }}</p>
139
+ <p class="fw-bold" id="discount-text">Discount: $0.00</p>
140
+ <p class="fw-bold" id="total-text">Total: ${{ subtotal }}</p>
141
  </div>
142
 
143
+ <!-- Apply Coupon Section -->
144
+ <div class="apply-coupon">
145
+ <label for="coupon-code">Coupon Code:</label>
146
+ <select id="coupon-code" class="form-select">
147
+ <option value="">Select a coupon code</option>
148
+ </select>
149
+ <button class="apply-button mt-2" onclick="applyCoupon()">Apply Coupon</button>
150
+ <p id="coupon-message" class="text-danger mt-2"></p>
151
  </div>
152
 
 
153
  <button class="checkout-button" onclick="proceedToOrder()">Proceed to Order</button>
154
  </div>
155
  </div>
156
 
157
  <script>
158
+ // Fetch coupon codes automatically on page load
159
+ document.addEventListener("DOMContentLoaded", function () {
160
+ const customerEmail = "{{ customer_email }}";
161
+
162
+ fetch('/get_coupon_codes', {
163
+ method: 'POST',
164
+ headers: { 'Content-Type': 'application/json' },
165
+ body: JSON.stringify({ email: customerEmail })
166
+ })
167
  .then(response => response.json())
168
  .then(data => {
169
  if (data.success) {
170
+ const couponSelect = document.getElementById('coupon-code');
171
  data.coupons.forEach(coupon => {
172
+ const option = document.createElement('option');
173
  option.value = coupon.Coupon_Code__c;
174
+ option.textContent = coupon.Coupon_Code__c;
175
+ couponSelect.appendChild(option);
176
  });
177
  } else {
178
+ document.getElementById('coupon-message').innerText = "No active coupons found.";
179
  }
180
  })
181
+ .catch(err => console.error('Error fetching coupons:', err));
182
  });
183
 
184
+ // Update quantity
185
  function updateQuantity(action, itemName) {
186
+ const quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
187
  let quantity = parseInt(quantityInput.value);
188
+
189
  if (action === 'increase') {
190
  quantity++;
191
  } else if (action === 'decrease' && quantity > 1) {
192
  quantity--;
193
  }
194
+
195
+ fetch('/cart/update_quantity', {
196
+ method: 'POST',
197
+ headers: { 'Content-Type': 'application/json' },
198
+ body: JSON.stringify({ item_name: itemName, quantity: quantity })
199
+ })
200
+ .then(response => response.json())
201
+ .then(data => {
202
+ if (data.success) {
203
+ quantityInput.value = quantity;
204
+ location.reload();
205
+ } else {
206
+ alert('Error updating quantity: ' + data.message);
207
+ }
208
+ })
209
+ .catch(err => console.error('Error:', err));
210
  }
211
 
212
+ // Remove item
213
+ function removeItemFromCart(itemName) {
214
+ fetch(`/cart/remove/${encodeURIComponent(itemName)}`, {
215
+ method: 'POST'
216
+ })
217
+ .then(response => response.json())
218
+ .then(data => {
219
+ if (data.success) {
220
+ alert(data.message);
221
+ location.reload();
222
+ } else {
223
+ alert('Error removing item: ' + data.message);
224
+ }
225
+ })
226
+ .catch(err => console.error('Error:', err));
227
+ }
228
 
229
+ function proceedToOrder() {
230
+ fetch('/checkout', {
231
  method: 'POST',
 
 
232
  })
233
  .then(response => response.json())
234
  .then(data => {
235
  if (data.success) {
236
+ alert(data.message);
237
+ window.location.href = '/order'; // Redirect to menu or order confirmation page
 
 
 
 
238
  } else {
239
+ alert(data.error || data.message);
240
  }
241
  })
242
+ .catch(err => console.error('Error during checkout:', err));
243
+ }
244
+
245
+ // Apply coupon
246
+ function applyCoupon() {
247
+ const couponCode = document.getElementById('coupon-code').value;
248
+ const subtotal = parseFloat(document.getElementById('subtotal-text').innerText.replace('Subtotal: $', ''));
249
+ const message = document.getElementById('coupon-message');
250
+
251
+ if (!couponCode) {
252
+ message.innerText = "Please select a coupon code.";
253
+ return;
254
+ }
255
+
256
+ fetch('/apply_coupon', {
257
+ method: 'POST',
258
+ headers: { 'Content-Type': 'application/json' },
259
+ body: JSON.stringify({ coupon_code: couponCode, subtotal: subtotal })
260
+ })
261
+ .then(response => response.json())
262
+ .then(data => {
263
+ if (data.success) {
264
+ const discount = data.discount;
265
+ const total = subtotal - discount;
266
+
267
+ document.getElementById('discount-text').innerText = `Discount: $${discount.toFixed(2)}`;
268
+ document.getElementById('total-text').innerText = `Total: $${total.toFixed(2)}`;
269
+ message.innerText = `Coupon applied! You saved $${discount.toFixed(2)}`;
270
+ message.classList.remove('text-danger');
271
+ message.classList.add('text-success');
272
+ } else {
273
+ message.innerText = data.message;
274
+ }
275
+ })
276
+ .catch(err => {
277
+ console.error('Error applying coupon:', err);
278
+ message.innerText = 'Error applying coupon.';
279
+ });
280
  }
281
  </script>
282
  </body>