forked from khushi-joshi-05/Food-ordering-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carts.js
277 lines (228 loc) · 9.77 KB
/
carts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
function addItemToCart() {
var itemName = localStorage.getItem('itemName');
var itemPrice = localStorage.getItem('itemPrice');
addToCart(itemName, itemPrice);
}
const addToCart = function(name, price){
let cartItems = localStorage.getItem('cartItems');
cartItems = cartItems ? JSON.parse(cartItems) : [];
if(name==null && price==null) return;
const existingItem = cartItems.find(item => item.name === name);
if (!existingItem) {
cartItems.push({ name, price });
localStorage.setItem('cartItems', JSON.stringify(cartItems));
console.log(cartItems);
}
updateCartDisplay();
calculateBill();
}
const updateCartDisplay = function() {
const cartBody = document.querySelector(".items");
cartBody.innerHTML = '';
let cartItems = localStorage.getItem('cartItems');
cartItems = cartItems ? JSON.parse(cartItems) : [];
cartItems.forEach(item => {
const cartRow = document.createElement("tr");
const cartItemName = document.createElement("td");
const cartItemPrice = document.createElement("td");
cartItemName.innerText = item.name;
cartItemPrice.innerText = item.price;
cartItemPrice.classList.add("price");
cartRow.appendChild(cartItemName);
cartRow.appendChild(cartItemPrice);
cartBody.appendChild(cartRow);
});
}
// calculate total bill amount
let total = 0;
const calculateBill = ()=>{
itemPrices = document.querySelectorAll(".price");
for (p of itemPrices){
if (p!=null){
console.log(p.innerText);
total += parseFloat(p.innerText.replace('$',''));
}
}
console.log(total);
if(total!=0 && (!(isNaN(total)))){
document.getElementById("bill").innerText = "$" + total.toFixed(2)
}
}
document.addEventListener('DOMContentLoaded', function () {
addItemToCart();
});
// let orderBtn = document.querySelector(".butt");
// orderBtn.addEventListener("click", ()=>{
// if(total==0){
// alert("Please add something in the cart to place the order");
// }
// else{
// alert("Order placed!");
// }
// })
// const applyFirstTimeDiscount = () => {
// let isFirstTimeUser = localStorage.getItem('isFirstTimeUser');
// if (!isFirstTimeUser) {
// const couponCode = generateCouponCode();
// localStorage.setItem('couponCode', couponCode);
// localStorage.setItem('isFirstTimeUser', true);
// document.getElementById('couponCode').innerText = `Use coupon code ${couponCode} for 30% off!`;
// alert(`Congratulations! Your coupon code is ${couponCode}. You've received a 30% discount on your first order.`);
// }
// }
// bringing new cart to it
// document.addEventListener('DOMContentLoaded', () => {
// loadCartFromLocalStorage();
// document.getElementById('cart-items').addEventListener('click', (event) => {
// if (event.target.classList.contains('increase-quantity')) {
// updateQuantity(event.target, 1);
// } else if (event.target.classList.contains('decrease-quantity')) {
// updateQuantity(event.target, -1);
// }
// });
// });
// function loadCartFromLocalStorage() {
// const cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];
// const cartItemsContainer = document.getElementById('cart-items');
// cartItemsContainer.innerHTML = ''; // Clear existing items
// cartItems.forEach(item => {
// cartItemsContainer.appendChild(createCartItemElement(item));
// });
// console.log(cartItems);
// updateTotal();
// }
// function updateQuantity(button, change) {
// const cartItemRow = button.closest('.cart-item');
// const quantityElement = cartItemRow.querySelector('.quantity');
// const priceElement = cartItemRow.querySelector(".price");
// const unitPrice = parseFloat(cartItemRow.getAttribute('data-product-price'));
// const newQuantity = parseInt(quantityElement.textContent) + change;
// if (newQuantity <= 0) return; // Prevent quantity from being less than 1
// quantityElement.textContent = newQuantity;
// priceElement.textContent = (unitPrice * newQuantity).toFixed(2);
// const decreaseBtn = cartItemRow.querySelector(".decrease-quantity");
// if (newQuantity == 1) {
// decreaseBtn.classList.add("disable");
// } else {
// decreaseBtn.classList.remove("disable");
// }
// updateTotal();
// saveCartToLocalStorage();
// }
// function updateTotal() {
// const cartItems = document.querySelectorAll('.cart-item');
// let total = 0.0;
// cartItems.forEach(item => {
// const price = parseFloat(item.getAttribute('data-product-price'));
// const quantity = parseInt(item.querySelector('.quantity').textContent);
// total += price * quantity;
// });
// document.getElementById('cart-total').innerHTML = (total != 0) ? `<span>Subtotal:</span> $${total.toFixed(2)}` : ``;
// handleEmptyCart(total);
// }
// function saveCartToLocalStorage() {
// const cartItems = [];
// document.querySelectorAll('.cart-item').forEach(item => {
// cartItems.push({
// id: item.getAttribute('data-product-id'),
// name: item.getAttribute('data-product-name'),
// unitPrice: item.getAttribute('data-product-price'),
// price: (parseFloat(item.getAttribute('data-product-price')) * parseInt(item.querySelector('.quantity').textContent)).toFixed(2),
// quantity: parseInt(item.querySelector('.quantity').textContent),
// image: item.getAttribute('data-product-image')
// });
// });
// localStorage.setItem('cartItems', JSON.stringify(cartItems));
// }
// function createCartItemElement(item) {
// const cartItemRow = document.createElement('div');
// cartItemRow.className = 'cart-item';
// cartItemRow.setAttribute('data-product-id', item.id);
// cartItemRow.setAttribute('data-product-price', item.price);
// cartItemRow.setAttribute('data-product-name', item.name);
// cartItemRow.setAttribute('data-product-image', item.image);
// cartItemRow.innerHTML = `
// <img src='${item.imgSrc}' alt='${item.name}-image' height='100px' width='100px'>
// <div class='detail'>
// <div>${item.name}</div>
// <div class="quantity-wrapper">
// <span class="btn decrease-quantity ${item.quantity == 1 ? 'disable' : ''}">-</span>
// <span class="quantity">${item.quantity}</span>
// <span class="btn increase-quantity">+</span>
// </div>
// <div class='price'>${parseFloat(item.price).toFixed(2)}</div>
// </div>`;
// const removeBtn = document.createElement('div');
// removeBtn.className = 'btn remove';
// removeBtn.innerHTML = 'x';
// removeBtn.addEventListener('click', (e) => {
// console.log(e.target.parentElement);
// e.target.parentElement.remove();
// updateTotal();
// saveCartToLocalStorage();
// });
// cartItemRow.appendChild(removeBtn);
// return cartItemRow;
// }
// function handleEmptyCart(total) {
// const emptyCartContainer = document.querySelector('.empty-cart');
// if (total!=0) {
// emptyCartContainer.innerHTML = ``;
// return(false);// Cart is not empty
// } else {
// emptyCartContainer.innerHTML = `
// <h4>Empty Menu!</h4>
// <p>Looks like you haven't made your choice yet... Check what we have got for you and get it swished.</p>
// <a href="./menu.html"><button class="butt">Explore Menu</button></a>`;
// alert("To place Order,Kindly add items to the cart");
// return(true); // Cart is empty
// }
// }
// // Function to generate a random coupon code
// const generateCouponCode = () => {
// const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
// let couponCode = '';
// for (let i = 0; i < 8; i++) {
// couponCode += characters.charAt(Math.floor(Math.random() * characters.length));
// }
// return couponCode;
// }
// // Check if it's the user's first order and apply discount
// const applyFirstTimeDiscount = () => {
// let couponCode = localStorage.getItem('couponCode');
// if (!couponCode) {
// couponCode = generateCouponCode();
// localStorage.setItem('couponCode', couponCode);
// }
// document.getElementById('couponCode').innerHTML = `Use coupon code <span style="font-weight: bold;"> ${couponCode} </span> for 30% off!`;
// document.querySelector(".coupen-inner").innerHTML = `Congratulations! Your coupon code is ${couponCode}. You've received a 30% discount on your first order.`;
// }
// // window.onload = applyFirstTimeDiscount;
// // Input for apply coupon code
// document.getElementById('applyCouponButton').addEventListener('click', function () {
// const couponCode = document.getElementById('inputCode').value;
// if (!couponCode) {
// alert('Please enter a Coupon Code.');
// return;
// }
// });
// const modal = document.getElementById("myModal");
// const closeButton = document.querySelector(".close");
// const orderNowButton = document.getElementById("orderNowButton");
// orderNowButton.addEventListener("click", () => {
// const total = updateTotal(); // Calculate the total
// const isEmptyCart = handleEmptyCart(total);
// if (isEmptyCart) {
// return; // Exit early if the cart is empty
// }
// // Only show modal if cart is not empty
// else {modal.style.display = "block";}
// });
// closeButton.addEventListener("click", () => {
// modal.style.display = "none";
// });
// window.onclick = (event) => {
// if (event.target == modal) {
// modal.style.display = "none";
// }
// };