-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.js
More file actions
55 lines (48 loc) · 1.74 KB
/
cart.js
File metadata and controls
55 lines (48 loc) · 1.74 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
// Function to load the cart items
function loadCart() {
const cart = JSON.parse(localStorage.getItem('cart')) || [];
const cartItems = document.getElementById('cart-items');
const cartMessage = document.getElementById('cart-message');
cartItems.innerHTML = '';
if (cart.length === 0) {
// Show the empty cart message
if (cartMessage) {
cartMessage.innerHTML = '<td colspan="5" class="text-center">Your cart is empty.</td>';
}
} else {
// Populate the cart table with items
cart.forEach((item, index) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${item.name}</td>
<td>₹${item.price}</td>
<td>${item.quantity}</td>
<td>₹${(item.price * item.quantity).toFixed(2)}</td>
<td><button class="btn btn-danger btn-sm" onclick="removeFromCart(${index})">Remove</button></td>
`;
cartItems.appendChild(row);
});
// Hide the empty cart message if there are items
if (cartMessage) {
cartMessage.innerHTML = '';
}
}
}
// Function to clear the cart
function clearCart() {
localStorage.removeItem('cart');
loadCart();
}
// Function to remove an item from the cart
function removeFromCart(index) {
let cart = JSON.parse(localStorage.getItem('cart')) || [];
cart.splice(index, 1);
localStorage.setItem('cart', JSON.stringify(cart));
loadCart();
}
// Function to handle checkout and redirect to payment page
function proceedToCheckout() {
window.location.href = 'payment.html';
}
// Load the cart when the page loads
document.addEventListener('DOMContentLoaded', loadCart);