-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
83 lines (71 loc) · 1.71 KB
/
scripts.js
File metadata and controls
83 lines (71 loc) · 1.71 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
let cartCount = 0;
let totalAmount = 0;
function addToCart(price) {
cartCount++;
totalAmount += price;
document.getElementById("cart-count").innerText = cartCount;
document.getElementById("total").innerText = totalAmount;
}
let cart = JSON.parse(localStorage.getItem("cart")) || [];
// ADD TO CART
function addToCart(name, price) {
let item = cart.find(p => p.name === name);
if (item) {
item.qty++;
} else {
cart.push({ name, price, qty: 1 });
}
localStorage.setItem("cart", JSON.stringify(cart));
alert("Added to cart");
}
// LOAD CART
function loadCart() {
let html = "";
let total = 0;
cart.forEach((item, i) => {
total += item.price * item.qty;
html += `
<p>
${item.name} - ₹${item.price} x ${item.qty}
<button onclick="increase(${i})">+</button>
<button onclick="decrease(${i})">-</button>
<button onclick="removeItem(${i})">❌</button>
</p>
`;
});
document.getElementById("cart").innerHTML = html;
document.getElementById("total").innerText = total;
}
// INCREASE
function increase(i) {
cart[i].qty++;
updateCart();
}
// DECREASE
function decrease(i) {
if (cart[i].qty > 1) cart[i].qty--;
updateCart();
}
// REMOVE
function removeItem(i) {
cart.splice(i, 1);
updateCart();
}
// UPDATE
function updateCart() {
localStorage.setItem("cart", JSON.stringify(cart));
loadCart();
}
// LOGIN
function login() {
let user = document.getElementById("user").value;
localStorage.setItem("user", user);
alert("Login Successful");
window.location.href = "index.html";
}
// ORDER
function order() {
localStorage.removeItem("cart");
alert("Order Placed Successfully 🎉");
window.location.href = "index.html";
}