-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (86 loc) · 3.47 KB
/
script.js
File metadata and controls
107 lines (86 loc) · 3.47 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
//function to add to cart
function loadCart() {
let cart = JSON.parse(localStorage.getItem('cart')) || [];
let cartItemsContainer = document.getElementById('cart-items');
let totalPriceContainer = document.getElementById('total-price');
let totalPrice = 0;
cartItemsContainer.innerHTML = ''; // this code literallyClear previous content
cart.forEach((item, index) => {
totalPrice += item.subtotal;
cartItemsContainer.innerHTML += `
<tr>
<td><img src="${item.image}" class="cart-image"></td>
<td>${item.name}</td>
<td>N${item.price.toLocaleString()}</td>
<td>
<input type="number" value="${item.quantity}" min="1"
onchange="updateQuantity(${index}, this.value)">
</td>
<td>N${item.subtotal.toLocaleString()}</td>
<td><button class="delete-btn" onclick="removeItem(${index})">🗑️</button></td>
</tr>
`;
});
totalPriceContainer.innerText = totalPrice.toLocaleString();
}
function updateQuantity(index, newQuantity) {
let cart = JSON.parse(localStorage.getItem('cart')) || [];
if (newQuantity < 1) newQuantity = 1;
cart[index].quantity = parseInt(newQuantity);
cart[index].subtotal = cart[index].quantity * cart[index].price;
localStorage.setItem('cart', JSON.stringify(cart));
loadCart();
}
function removeItem(index) {
let cart = JSON.parse(localStorage.getItem('cart')) || [];
cart.splice(index, 1);
localStorage.setItem('cart', JSON.stringify(cart));
loadCart();
}
function clearCart() {
localStorage.removeItem('cart');
loadCart();
}
document.addEventListener("DOMContentLoaded", loadCart);
// Function to add items to cart and store in local storage
function addToCart(name, price, image) {
let product = {
name: name,
price: price,
image: image,
quantity: 1,
subtotal: price
};
let cart = JSON.parse(localStorage.getItem('cart')) || [];
let existingItem = cart.find(item => item.name === name);
if (existingItem) {
existingItem.quantity += 1;
existingItem.subtotal = existingItem.quantity * price;
} else {
cart.push(product);
}
localStorage.setItem('cart', JSON.stringify(cart));
// Redirect after adding item
window.location.href = "cartPage.html";
}
document.addEventListener("DOMContentLoaded", function () {
loadOrderSummary();
});
function loadOrderSummary() {
let cart = JSON.parse(localStorage.getItem("cart")) || [];
let summaryTable = document.getElementById("order-summary");
let totalPrice = 0;
summaryTable.innerHTML = ""; // Clear previous items
cart.forEach((item) => {
let row = `
<tr>
<td>${item.name}</td>
<td>${item.quantity}</td>
<td>N${item.subtotal.toLocaleString()}</td>
</tr>
`;
summaryTable.innerHTML += row;
totalPrice += item.subtotal;
});
document.getElementById("total-price").textContent = `N${totalPrice.toLocaleString()}`;
}