-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.js
More file actions
103 lines (93 loc) · 3.42 KB
/
Copy pathcart.js
File metadata and controls
103 lines (93 loc) · 3.42 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
// Initialize cart from localStorage or set to an empty array
let cart = JSON.parse(localStorage.getItem('cart')) || [];
const cartItemsContainer = document.getElementById('cart-items');
const totalItemsLabel = document.getElementById('totalnum');
const notificationContainer = document.createElement('div');
notificationContainer.classList.add('notification-container');
document.body.appendChild(notificationContainer);
// Function to add item to cart and optionally redirect to cart page
function addToCart(name, price, image, redirect = false) {
const existingItem = cart.find(item => item.name === name);
if (existingItem) {
existingItem.quantity += 1;
} else {
cart.push({ name, price, image, quantity: 1 });
}
updateCart();
showNotification(`Added ${name} to cart`);
// Redirect to cart page if needed
if (redirect) {
window.location.href = 'cart.html'; // Change 'cart.html' to your cart page's URL
}
}
// Function to update cart and localStorage
function updateCart() {
localStorage.setItem('cart', JSON.stringify(cart));
renderCartItems();
calculateTotal();
}
// Function to render cart items in the cart page
function renderCartItems() {
cartItemsContainer.innerHTML = '';
cart.forEach(item => {
const row = document.createElement('tr');
row.innerHTML = `
<td>
<div class="cart-item">
<img src="${item.image}" alt="${item.name}" />
<span>${item.name}</span>
</div>
</td>
<td>₹${item.price}</td>
<td>
<div class="quantity-controls">
<button class="quantity-btn" onclick="changeQuantity('${item.name}', 'decrease')">-</button>
<span>${item.quantity}</span>
<button class="quantity-btn" onclick="changeQuantity('${item.name}', 'increase')">+</button>
</div>
</td>
<td>₹${(item.price * item.quantity).toFixed(2)}</td>
<td>
<button class="remove-btn" onclick="removeFromCart('${item.name}')">Remove</button>
</td>
`;
cartItemsContainer.appendChild(row);
});
totalItemsLabel.innerText = `${cart.length} items`;
}
// Function to change quantity of items in the cart
function changeQuantity(name, action) {
const item = cart.find(i => i.name === name);
if (item) {
item.quantity = action === 'increase' ? item.quantity + 1 : Math.max(1, item.quantity - 1);
updateCart();
showNotification(action === 'increase' ? 'Quantity increased' : 'Quantity decreased');
}
}
// Function to remove item from cart
function removeFromCart(name) {
cart = cart.filter(item => item.name !== name);
updateCart();
showNotification('Item removed from cart');
}
// Function to calculate and display total price
function calculateTotal() {
const total = cart.reduce((sum, item) => sum + (item.price * item.quantity), 0);
document.querySelector('.totals-row .total-price').innerText = `₹${total.toFixed(2)}`;
}
// Function to show notification
function showNotification(message, isError = false) {
const notification = document.createElement('div');
notification.classList.add('notification', isError ? 'error' : 'success');
notification.innerText = message;
notificationContainer.appendChild(notification);
setTimeout(() => {
notification.classList.add('fade-out');
setTimeout(() => notification.remove(), 500);
}, 3000);
}
// Initialize cart on page load
document.addEventListener('DOMContentLoaded', () => {
renderCartItems();
calculateTotal();
});