Spaces:
Sleeping
Sleeping
Update templates/cart.html
Browse files- templates/cart.html +74 -101
templates/cart.html
CHANGED
@@ -75,22 +75,12 @@
|
|
75 |
color: #fff;
|
76 |
padding: 5px 20px;
|
77 |
font-size: 1rem;
|
78 |
-
font-weight: bold;
|
79 |
cursor: pointer;
|
80 |
border-radius: 5px;
|
81 |
-
border: none;
|
82 |
-
transition: background-color 0.3s ease, transform 0.2s ease;
|
83 |
}
|
84 |
.apply-button:disabled {
|
85 |
background-color: #6c757d;
|
86 |
-
cursor: not-allowed;
|
87 |
-
}
|
88 |
-
|
89 |
-
.apply-button:hover {
|
90 |
-
background-color: #218838; /* Darker green on hover */
|
91 |
-
transform: scale(1.05); /* Slightly enlarge the button when hovered */
|
92 |
}
|
93 |
-
|
94 |
.checkout-button {
|
95 |
background-color: #007bff;
|
96 |
color: #fff;
|
@@ -118,12 +108,6 @@
|
|
118 |
<img src="{{ item.Image1__c }}" alt="{{ item.Name }}" onerror="this.src='/static/placeholder.jpg';">
|
119 |
<div class="cart-item-details">
|
120 |
<div class="cart-item-title">{{ item.Name }}</div>
|
121 |
-
<div class="cart-item-addons">
|
122 |
-
<small class="text-muted">Add-ons: {{ item.Add_Ons__c }}</small>
|
123 |
-
</div>
|
124 |
-
<div class="cart-item-instructions">
|
125 |
-
<small class="text-muted">Instructions: {{ item.Instructions__c or "None" }}</small>
|
126 |
-
</div>
|
127 |
<div class="cart-item-quantity">
|
128 |
<button onclick="updateQuantity('decrease', '{{ item.Name }}')">-</button>
|
129 |
<input type="text" value="{{ item.Quantity__c }}" readonly>
|
@@ -139,58 +123,52 @@
|
|
139 |
</div>
|
140 |
{% endfor %}
|
141 |
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
<p id="subtotal-text" class="fw-bold">Subtotal: ${{ subtotal }}</p>
|
157 |
-
<p id="discount-text" class="fw-bold">Discount: $0.00</p>
|
158 |
-
<p id="total-text" class="fw-bold">Total: ${{ subtotal }}</p> <!-- The final total after discount -->
|
159 |
-
<button class="checkout-button" onclick="proceedToOrder()">Proceed to Order</button>
|
160 |
-
</div>
|
161 |
-
|
162 |
-
<script>
|
163 |
-
// Fetch coupon codes automatically when the page loads
|
164 |
-
document.addEventListener("DOMContentLoaded", function() {
|
165 |
-
const customerEmail = "{{ customer_email }}"; // The email stored in the session
|
166 |
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
.then(response => response.json())
|
170 |
.then(data => {
|
171 |
-
if (data.success
|
172 |
-
|
173 |
-
data.
|
174 |
-
|
175 |
option.value = coupon.Coupon_Code__c;
|
176 |
-
option.
|
177 |
-
|
178 |
});
|
179 |
} else {
|
180 |
-
document.getElementById('
|
181 |
-
document.getElementById('coupon-message').style.color = "red";
|
182 |
}
|
183 |
})
|
184 |
-
.catch(error =>
|
185 |
-
|
186 |
-
});
|
187 |
-
}
|
188 |
-
});
|
189 |
-
|
190 |
|
191 |
// Update item quantity
|
192 |
function updateQuantity(action, itemName) {
|
193 |
-
let quantityInput = document.querySelector(input[data-item-name="${itemName}"]);
|
194 |
let quantity = parseInt(quantityInput.value);
|
195 |
if (action === 'increase') {
|
196 |
quantity++;
|
@@ -205,60 +183,55 @@
|
|
205 |
updateCart(itemName, quantity);
|
206 |
}
|
207 |
|
208 |
-
// Apply coupon
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
|
|
|
|
213 |
|
214 |
-
|
215 |
-
fetch('/apply_coupon', {
|
216 |
method: 'POST',
|
217 |
-
headers: {
|
218 |
-
|
219 |
-
},
|
220 |
-
body: JSON.stringify({ coupon_code: couponCode, subtotal: subtotal })
|
221 |
})
|
222 |
.then(response => response.json())
|
223 |
.then(data => {
|
224 |
if (data.success) {
|
225 |
-
|
226 |
-
|
227 |
-
document.getElementById('
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
} else {
|
232 |
-
|
233 |
-
couponMessage.style.color = 'red';
|
234 |
}
|
235 |
})
|
236 |
-
.catch(error =>
|
237 |
-
|
238 |
-
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
});
|
241 |
-
|
242 |
-
|
243 |
-
|
|
|
244 |
}
|
245 |
-
}
|
246 |
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
}
|
251 |
-
|
252 |
-
.then(data => {
|
253 |
-
if (data.success) {
|
254 |
-
alert(data.message);
|
255 |
-
window.location.href = '/order'; // Redirect to menu or order confirmation page
|
256 |
-
} else {
|
257 |
-
alert(data.error || data.message);
|
258 |
-
}
|
259 |
-
})
|
260 |
-
.catch(err => console.error('Error during checkout:', err));
|
261 |
-
}
|
262 |
-
</script>
|
263 |
</body>
|
264 |
-
</html>
|
|
|
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;
|
|
|
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>
|
|
|
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++;
|
|
|
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>
|
237 |
+
</html>
|