-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
263 lines (233 loc) · 10.6 KB
/
Copy pathscript.js
File metadata and controls
263 lines (233 loc) · 10.6 KB
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
// Sample Product Data
const products = [
{ id: 1, name: 'Premium Wireless Headphones', category: 'electronics', price: 299, originalPrice: 499, rating: '★★★★★', image: '🎧', discount: 40 },
{ id: 2, name: 'Designer T-Shirt', category: 'fashion', price: 49, originalPrice: 99, rating: '★★★★☆', image: '👕', discount: 50 },
{ id: 3, name: 'Cozy Area Rug', category: 'home', price: 89, originalPrice: 149, rating: '★★★★★', image: '🏠', discount: 40 },
{ id: 4, name: 'JavaScript Guide Book', category: 'books', price: 29, originalPrice: 59, rating: '★★★★☆', image: '📚', discount: 50 },
{ id: 5, name: 'Yoga Mat Pro', category: 'sports', price: 39, originalPrice: 89, rating: '★★★★★', image: '🧘', discount: 56 },
{ id: 6, name: 'Organic Face Serum', category: 'beauty', price: 45, originalPrice: 99, rating: '★★★★☆', image: '💆', discount: 55 },
{ id: 7, name: '4K Webcam', category: 'electronics', price: 199, originalPrice: 349, rating: '★★★★★', image: '📹', discount: 43 },
{ id: 8, name: 'Summer Dress', category: 'fashion', price: 59, originalPrice: 129, rating: '★★★★☆', image: '👗', discount: 54 },
{ id: 9, name: 'Desk Lamp', category: 'home', price: 34, originalPrice: 79, rating: '★★★★☆', image: '💡', discount: 57 },
{ id: 10, name: 'Cooking Mastery', category: 'books', price: 25, originalPrice: 55, rating: '★★★★★', image: '👨', discount: 55 },
{ id: 11, name: 'Running Shoes', category: 'sports', price: 119, originalPrice: 199, rating: '★★★★☆', image: '👟', discount: 40 },
{ id: 12, name: 'Moisturizer Cream', category: 'beauty', price: 32, originalPrice: 75, rating: '★★★★★', image: '🧴', discount: 57 }
];
let cart = [];
let currentFilter = 'all';
// Initialize
window.addEventListener('DOMContentLoaded', () => {
displayProducts('all');
loadCart();
});
// Display Products
function displayProducts(category) {
currentFilter = category;
let filtered = category === 'all' ? products : products.filter(p => p.category === category);
const grids = document.querySelectorAll('.product-grid');
grids.forEach(grid => {
grid.innerHTML = filtered.map(product => `
<div class="product-card">
<div class="product-image">${product.image}</div>
<div class="product-info">
<p class="product-category">${product.category.charAt(0).toUpperCase() + product.category.slice(1)}</p>
<h4 class="product-name">${product.name}</h4>
<div class="product-rating">${product.rating}</div>
<div class="product-price">
<span class="original-price">$${product.originalPrice}</span>
<span class="current-price">$${product.price}</span>
<span class="discount-badge">-${product.discount}%</span>
</div>
<button class="add-to-cart" onclick="addToCart(${product.id})">Add to Cart</button>
</div>
</div>
`).join('');
});
}
// Filter Products
function filterCategory(category) {
displayProducts(category);
// Update filter buttons
document.querySelectorAll('.filter-btn').forEach(btn => {
btn.classList.remove('active');
});
event.target.classList.add('active');
}
// Add to Cart
function addToCart(productId) {
const product = products.find(p => p.id === productId);
const cartItem = cart.find(item => item.id === productId);
if (cartItem) {
cartItem.quantity++;
} else {
cart.push({ ...product, quantity: 1 });
}
saveCart();
updateCartCount();
alert(`✓ "${product.name}" added to cart!`);
}
// Update Cart Count
function updateCartCount() {
const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0);
document.getElementById('cartCount').textContent = totalItems;
}
// Save Cart to localStorage
function saveCart() {
localStorage.setItem('cart', JSON.stringify(cart));
}
// Load Cart from localStorage
function loadCart() {
const saved = localStorage.getItem('cart');
if (saved) {
cart = JSON.parse(saved);
updateCartCount();
}
}
// Section Navigation
function showHome() {
hideAllSections();
document.getElementById('homeSection').classList.add('active');
displayProducts('all');
}
function showCategories() {
hideAllSections();
document.getElementById('categoriesSection').classList.add('active');
displayProducts('all');
}
function showDeals() {
hideAllSections();
document.getElementById('dealsSection').classList.add('active');
let deals = products.filter(p => p.discount >= 40);
document.getElementById('dealsProductGrid').innerHTML = deals.map(product => `
<div class="product-card">
<div class="product-image">${product.image}</div>
<div class="product-info">
<p class="product-category">${product.category.charAt(0).toUpperCase() + product.category.slice(1)}</p>
<h4 class="product-name">${product.name}</h4>
<div class="product-rating">${product.rating}</div>
<div class="product-price">
<span class="original-price">$${product.originalPrice}</span>
<span class="current-price">$${product.price}</span>
<span class="discount-badge">-${product.discount}%</span>
</div>
<button class="add-to-cart" onclick="addToCart(${product.id})">Add to Cart</button>
</div>
</div>
`).join('');
}
function showCart() {
hideAllSections();
document.getElementById('cartSection').classList.add('active');
displayCart();
}
function showAccount() {
hideAllSections();
document.getElementById('accountSection').classList.add('active');
}
function hideAllSections() {
document.querySelectorAll('.section').forEach(section => {
section.classList.remove('active');
});
}
// Display Cart Items
function displayCart() {
const cartContainer = document.getElementById('cartItems');
if (cart.length === 0) {
cartContainer.innerHTML = '<div class="empty-cart"><i class="fas fa-shopping-cart"></i><p>Your cart is empty</p></div>';
document.getElementById('subtotal').textContent = '$0.00';
document.getElementById('shipping').textContent = '$0.00';
document.getElementById('tax').textContent = '$0.00';
document.getElementById('total').textContent = '$0.00';
return;
}
cartContainer.innerHTML = cart.map(item => `
<div class="cart-item">
<div class="cart-item-image">${item.image}</div>
<div class="cart-item-details">
<h4>${item.name}</h4>
<p>$${item.price} x ${item.quantity}</p>
</div>
<div class="cart-item-actions">
<span>$${(item.price * item.quantity).toFixed(2)}</span>
<button class="qty-btn" onclick="updateQuantity(${item.id}, -1)">-</button>
<span>${item.quantity}</span>
<button class="qty-btn" onclick="updateQuantity(${item.id}, 1)">+</button>
<button class="remove-btn" onclick="removeFromCart(${item.id})">Remove</button>
</div>
</div>
`).join('');
updateCartSummary();
}
// Update Quantity
function updateQuantity(productId, change) {
const item = cart.find(i => i.id === productId);
if (item) {
item.quantity += change;
if (item.quantity <= 0) {
removeFromCart(productId);
} else {
saveCart();
updateCartCount();
displayCart();
}
}
}
// Remove from Cart
function removeFromCart(productId) {
cart = cart.filter(item => item.id !== productId);
saveCart();
updateCartCount();
displayCart();
}
// Update Cart Summary
function updateCartSummary() {
const subtotal = cart.reduce((sum, item) => sum + (item.price * item.quantity), 0);
const shipping = subtotal > 100 ? 0 : 10;
const tax = subtotal * 0.08;
const total = subtotal + shipping + tax;
document.getElementById('subtotal').textContent = '$' + subtotal.toFixed(2);
document.getElementById('shipping').textContent = '$' + shipping.toFixed(2);
document.getElementById('tax').textContent = '$' + tax.toFixed(2);
document.getElementById('total').textContent = '$' + total.toFixed(2);
}
// Checkout
function checkout() {
if (cart.length === 0) {
alert('Your cart is empty!');
return;
}
const total = document.getElementById('total').textContent;
alert(`Order confirmed! Total: ${total}\n\nThank you for shopping at ShopSphere!`);
cart = [];
saveCart();
updateCartCount();
displayCart();
}
// Search Functionality
document.addEventListener('DOMContentLoaded', () => {
const searchInput = document.getElementById('searchInput');
if (searchInput) {
searchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
const query = searchInput.value.toLowerCase();
const filtered = products.filter(p => p.name.toLowerCase().includes(query));
document.getElementById('productGrid').innerHTML = filtered.map(product => `
<div class="product-card">
<div class="product-image">${product.image}</div>
<div class="product-info">
<p class="product-category">${product.category}</p>
<h4 class="product-name">${product.name}</h4>
<div class="product-rating">${product.rating}</div>
<div class="product-price">
<span class="original-price">$${product.originalPrice}</span>
<span class="current-price">$${product.price}</span>
<span class="discount-badge">-${product.discount}%</span>
</div>
<button class="add-to-cart" onclick="addToCart(${product.id})">Add to Cart</button>
</div>
</div>
`).join('');
}
});
}
});