-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshopping.js
298 lines (269 loc) · 8.69 KB
/
shopping.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const products = [
// * Laptop deals
{
image: 'products/hp.png',
name: 'HP Pavilion 15',
price: 50000,
rating: 4.5,
category: 'Laptop'
},
{
image: 'products/victus.png',
name: 'HP Victus 16',
price: 200000,
rating: 3.8,
category: 'Laptop'
},
{
image: 'products/mac-book.png',
name: 'Apple MacBook Air M2',
price: 120000,
rating: 4.7,
category: 'Laptop'
},
{
image: 'products/acer.png',
name: 'Acer Nitro 5',
price: 70000,
rating: 4.2,
category: 'Laptop'
},
{
image: 'products/dell.png',
name: 'Dell XPS 13',
price: 150000,
rating: 4.6,
category: 'Laptop'
},
{
image: 'products/Controller.png',
name: 'Gaming Controller',
price: 2500,
rating: 4.4,
category: 'Gaming Devices'
},
{
image: 'products/backlit-keyboard.png',
name: 'Backlit Keyboard',
price: 3500,
rating: 4.1,
category: 'Gaming Devices'
},
{
image: 'products/headphone.png',
name: 'Wireless Headphones',
price: 4000,
rating: 4.3,
category: 'Gaming Devices'
},
{
image: 'products/mouse.png',
name: 'Gaming Mouse',
price: 1500,
rating: 4.0,
category: 'Gaming Devices'
},
{
image: 'products/gaming-cpu.png',
name: 'Gaming CPU',
price: 30000,
rating: 4.5,
category: 'Gaming Devices'
},
// * Mobiles
{
image: 'products/iphone.png',
name: 'iPhone 14 Pro',
price: 99900,
rating: 4.8,
category: 'Mobiles'
},
{
image: 'products/iq7.png',
name: 'iQOO 7 Legend',
price: 35000,
rating: 4.2,
category: 'Mobiles'
},
{
image: 'products/one+.png',
name: 'OnePlus 9 Pro',
price: 54900,
rating: 4.6,
category: 'Mobiles'
},
{
image: 'products/iqo.png',
name: 'iQOO Neo 6',
price: 33000,
rating: 4.3,
category: 'Mobiles'
},
{
image: 'products/infinix.png',
name: 'Infinix Zero 5G',
price: 19999,
rating: 4.0,
category: 'Mobiles'
},
];
const productGrid = document.querySelector('.product-grid');
// Group products by category
const groupedProducts = products.reduce((acc, product) => {
if (!acc[product.category]) {
acc[product.category] = [];
}
acc[product.category].push(product);
return acc;
}, {});
// Create and append products for each category
Object.entries(groupedProducts).forEach(([category, categoryProducts]) => {
const categoryTitle = document.createElement('h2');
categoryTitle.textContent = `${category} Deals`;
categoryTitle.classList.add('category-title');
productGrid.appendChild(categoryTitle);
categoryProducts.forEach(product => {
const productCard = document.createElement('div');
productCard.classList.add('product-card');
// Generate star rating HTML
const fullStars = Math.floor(product.rating);
const halfStar = product.rating % 1 >= 0.5;
const emptyStars = 5 - fullStars - (halfStar ? 1 : 0);
let ratingHTML = '';
for (let i = 0; i < fullStars; i++) {
ratingHTML += '<i class="fas fa-star"></i>';
}
if (halfStar) {
ratingHTML += '<i class="fas fa-star-half-alt"></i>';
}
for (let i = 0; i < emptyStars; i++) {
ratingHTML += '<i class="far fa-star"></i>';
}
productCard.innerHTML = `
<div class="like-button" onclick="toggleLike(this)">
<i class="far fa-heart"></i>
</div>
<img src="${product.image}" alt="${product.name}">
<h3>${product.name}</h3>
<p class="price">₹${product.price.toFixed(2)}</p>
<div class="rating">
${ratingHTML}
<span>(${product.rating.toFixed(1)})</span>
</div>
<div class="cart-controls">
<button class="decrease" onclick="decreaseQuantity(this)">-</button>
<span class="quantity">0</span>
<button class="increase" onclick="increaseQuantity(this)">+</button>
</div>
<button class="add-to-cart" onclick="addToCart('${product.name}', this)">Add to Cart</button>
`;
productGrid.appendChild(productCard);
});
});
function toggleLike(element) {
element.querySelector('i').classList.toggle('fas');
element.querySelector('i').classList.toggle('far');
}
function decreaseQuantity(button) {
const quantityElement = button.parentElement.querySelector('.quantity');
let quantity = parseInt(quantityElement.textContent);
if (quantity > 0) {
quantityElement.textContent = --quantity;
}
}
function increaseQuantity(button) {
const quantityElement = button.parentElement.querySelector('.quantity');
let quantity = parseInt(quantityElement.textContent);
quantityElement.textContent = ++quantity;
}
function addToCart(productName, button) {
const quantityElement = button.parentElement.querySelector('.quantity');
const quantity = parseInt(quantityElement.textContent);
if (quantity > 0) {
alert(`Added ${quantity} ${productName}(s) to cart!`);
// Implement actual cart functionality here
quantityElement.textContent = '0'; // Reset quantity after adding to cart
} else {
alert('Please select a quantity before adding to cart.');
}
}
// Add these variables at the top of the file
let cart = [];
let cartTotal = 0;
// Modify the addToCart function
function addToCart(productName, button) {
const quantityElement = button.parentElement.querySelector('.quantity');
const quantity = parseInt(quantityElement.textContent);
if (quantity > 0) {
const product = products.find(p => p.name === productName);
const existingItem = cart.find(item => item.name === productName);
if (existingItem) {
existingItem.quantity += quantity;
} else {
cart.push({ ...product, quantity });
}
updateCartDisplay();
quantityElement.textContent = '0'; // Reset quantity after adding to cart
} else {
alert('Please select a quantity before adding to cart.');
}
}
// Add these new functions
function updateCartDisplay() {
const cartItems = document.getElementById('cart-items');
cartItems.innerHTML = '';
cartTotal = 0;
cart.forEach(item => {
const itemElement = document.createElement('div');
itemElement.classList.add('cart-item');
itemElement.innerHTML = `
<span>${item.name}</span>
<span>₹${item.price.toFixed(2)}</span>
<div class="cart-item-controls">
<button onclick="decreaseCartQuantity('${item.name}')">-</button>
<span>${item.quantity}</span>
<button onclick="increaseCartQuantity('${item.name}')">+</button>
</div>
<button onclick="removeFromCart('${item.name}')">Remove</button>
`;
cartItems.appendChild(itemElement);
cartTotal += item.price * item.quantity;
});
document.getElementById('cart-total').textContent = `Total: ₹${cartTotal.toFixed(2)}`;
}
function increaseCartQuantity(productName) {
const item = cart.find(item => item.name === productName);
if (item) {
item.quantity++;
updateCartDisplay();
}
}
function decreaseCartQuantity(productName) {
const item = cart.find(item => item.name === productName);
if (item && item.quantity > 1) {
item.quantity--;
updateCartDisplay();
}
}
function removeFromCart(productName) {
cart = cart.filter(item => item.name !== productName);
updateCartDisplay();
}
// Add an event listener for the checkout button
document.getElementById('checkout-btn').addEventListener('click', function() {
if (cart.length > 0) {
alert('Proceeding to checkout. Total: ₹' + cartTotal.toFixed(2));
// Here you would typically redirect to a checkout page
} else {
alert('Your cart is empty. Add some items before checking out.');
}
});
// Modify the event listener for the checkout button
document.getElementById('checkout-btn').addEventListener('click', function() {
if (cart.length > 0) {
localStorage.setItem('cart', JSON.stringify(cart));
window.location.href = 'checkout.html';
} else {
alert('Your cart is empty. Add some items before checking out.');
}
});