-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjavascript.js
More file actions
221 lines (185 loc) · 8.28 KB
/
Copy pathjavascript.js
File metadata and controls
221 lines (185 loc) · 8.28 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
document.addEventListener('DOMContentLoaded', () => {
// --- DOM Elements ---
const productGrid = document.querySelector('.product-grid');
const cartItemsContainer = document.getElementById('cart-items-container');
const cartCountElement = document.getElementById('cart-count');
const totalItemsElement = document.getElementById('total-items');
const totalPriceElement = document.getElementById('total-price');
const checkoutBtn = document.getElementById('checkout-btn');
const clearCartBtn = document.getElementById('clear-cart-btn');
const emptyCartMessage = document.querySelector('.empty-cart-message');
// --- Cart State ---
// Load cart from localStorage or initialize as empty array
let cart = JSON.parse(localStorage.getItem('shoppingCart')) || [];
// --- Functions ---
// Function to render products (if you were fetching from an API)
// For now, products are hardcoded in HTML, but this structure is useful
function renderProducts() {
// In a real app, you'd fetch product data and generate HTML here
// Since products are in HTML, we just need to attach listeners
attachProductListeners();
}
// Function to render cart items in the DOM
function renderCart() {
// Clear current cart display
cartItemsContainer.innerHTML = '';
if (cart.length === 0) {
if (emptyCartMessage) emptyCartMessage.style.display = 'block';
} else {
if (emptyCartMessage) emptyCartMessage.style.display = 'none';
cart.forEach(item => {
const cartItemElement = document.createElement('div');
cartItemElement.classList.add('cart-item');
cartItemElement.dataset.id = item.id; // Set data-id for removal
cartItemElement.innerHTML = `
<div class="cart-item-details">
<strong>${item.name}</strong> - $${item.price.toFixed(2)}
</div>
<div class="cart-item-actions">
<button class="quantity-decrease" ${item.quantity <= 1 ? 'style="visibility:hidden;"' : ''} >-</button>
<span class="item-quantity">${item.quantity}</span>
<button class="quantity-increase">+</button>
<button class="remove-item-btn"><i class="fas fa-trash-alt"></i></button>
</div>
`;
cartItemsContainer.appendChild(cartItemElement);
});
}
updateCartSummary();
saveCartToLocalStorage(); // Save cart whenever it's rendered
}
// Function to update cart summary (count and total price)
function updateCartSummary() {
let totalItems = 0;
let totalPrice = 0;
cart.forEach(item => {
totalItems += item.quantity;
totalPrice += item.price * item.quantity;
});
cartCountElement.textContent = totalItems;
totalItemsElement.textContent = totalItems;
totalPriceElement.textContent = totalPrice.toFixed(2);
// Enable/disable checkout and clear buttons based on cart content
const cartIsEmpty = cart.length === 0;
checkoutBtn.disabled = cartIsEmpty;
clearCartBtn.disabled = cartIsEmpty;
}
// Function to add an item to the cart
function addToCart(id, name, price) {
const existingItemIndex = cart.findIndex(item => item.id === id);
if (existingItemIndex > -1) {
// Item exists, increase quantity
cart[existingItemIndex].quantity += 1;
} else {
// Item does not exist, add new item
cart.push({ id, name, price, quantity: 1 });
}
renderCart(); // Re-render the cart display
}
// Function to update item quantity
function updateQuantity(id, change) {
const itemIndex = cart.findIndex(item => item.id === id);
if (itemIndex > -1) {
cart[itemIndex].quantity += change;
if (cart[itemIndex].quantity <= 0) {
// Remove item if quantity drops to 0 or less
removeFromCart(id);
} else {
renderCart();
}
}
}
// Function to remove an item from the cart
function removeFromCart(id) {
cart = cart.filter(item => item.id !== id);
renderCart(); // Re-render the cart display
}
// Function to clear the entire cart
function clearCart() {
cart = []; // Empty the array
renderCart(); // Re-render the cart display
}
// Function to save cart to localStorage
function saveCartToLocalStorage() {
localStorage.setItem('shoppingCart', JSON.stringify(cart));
}
// Function to handle simulated checkout
function handleCheckout() {
if (cart.length === 0) {
alert("Your cart is empty!");
return;
}
// Simulate checkout process
console.log("Simulating checkout process...");
console.log("Cart Items:", cart);
console.log(`Total Price: $${totalPriceElement.textContent}`);
// Display a confirmation message (replace with actual payment integration in a real app)
alert(`Checkout complete (Simulation)! Total: $${totalPriceElement.textContent}. Your cart has been cleared.`);
// Clear the cart after successful "checkout"
clearCart();
}
// --- Event Listeners ---
// Attach listeners to "Add to Cart" buttons (using event delegation on product grid)
function attachProductListeners() {
if (productGrid) {
productGrid.addEventListener('click', (e) => {
if (e.target.classList.contains('add-to-cart-btn')) {
const productItem = e.target.closest('.product-item');
const id = productItem.dataset.id;
const name = productItem.dataset.name;
// Ensure price is parsed as a number
const price = parseFloat(productItem.dataset.price);
if (id && name && !isNaN(price)) {
addToCart(id, name, price);
// Optional: Give user feedback
e.target.textContent = 'Added!';
setTimeout(() => {
e.target.textContent = 'Add to Cart';
}, 1000); // Reset button text after 1 second
} else {
console.error("Product data missing or invalid:", productItem.dataset);
}
}
});
} else {
console.error("Product grid container not found!");
}
}
// Attach listeners for cart actions (remove, quantity change) using event delegation
if (cartItemsContainer) {
cartItemsContainer.addEventListener('click', (e) => {
const target = e.target;
const cartItem = target.closest('.cart-item');
if (!cartItem) return; // Exit if click wasn't inside a cart item
const id = cartItem.dataset.id;
if (target.classList.contains('remove-item-btn') || target.closest('.remove-item-btn')) {
removeFromCart(id);
} else if (target.classList.contains('quantity-increase')) {
updateQuantity(id, 1); // Increase by 1
} else if (target.classList.contains('quantity-decrease')) {
updateQuantity(id, -1); // Decrease by 1
}
});
} else {
console.error("Cart items container not found!");
}
// Checkout Button Listener
if (checkoutBtn) {
checkoutBtn.addEventListener('click', handleCheckout);
} else {
console.error("Checkout button not found!");
}
// Clear Cart Button Listener
if (clearCartBtn) {
clearCartBtn.addEventListener('click', () => {
if (confirm("Are you sure you want to clear your entire cart?")) {
clearCart();
}
});
} else {
console.error("Clear Cart button not found!");
}
// --- Initial Setup ---
renderProducts(); // Attach listeners to initially loaded products
renderCart(); // Render cart based on localStorage or initial empty state
}); // End DOMContentLoaded