-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.js
More file actions
81 lines (70 loc) · 2.01 KB
/
cart.js
File metadata and controls
81 lines (70 loc) · 2.01 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
function pushToWrapper() {
let wrapper = document.querySelector("#pro-wrapper");
wrapper.innerHTML = "";
let cartItem = JSON.parse(localStorage.getItem("cart"));
cartItem.forEach((item) => {
console.log(item.name);
console.log(item.price);
let container = `<div class="product">
<h3>${item.name}</h3>
<h3>${item.price}</h3>
<i class="ri-close-line remove"></i>
</div>`;
wrapper.innerHTML += container;
});
}
pushToWrapper();
function removeItem(){
let iTag = document.querySelectorAll(".remove");
iTag.forEach((i)=>{
i.addEventListener("click", ()=>{
let productDiv = i.closest(".product")
let name = productDiv.querySelector("h3:nth-child(1)").innerText
console.log(name)
let cart = JSON.parse(localStorage.getItem("cart"))
let updatedCart = cart.filter(((item)=>{
return name != item.name
}))
console.log(updatedCart)
localStorage.setItem("cart", JSON.stringify(updatedCart));
productDiv.remove();
bill();
})
})
}
removeItem();
function bill(){
let amount = document.querySelector("#order h4")
let data = JSON.parse(localStorage.getItem("cart"))
let total = 0
data.forEach((d)=>{
let price = d.price
let finalPrice = Number(price.replace("Rs. ",""));
console.log(finalPrice)
total += finalPrice;
})
amount.innerText = `Rs. ${total}`
}
bill();
function placeOrder() {
let btn = document.querySelector("#order button");
btn.addEventListener("click", () => {
let input = document.querySelectorAll("#form input");
let toggle = false;
input.forEach((inp) => {
if (inp.value.trim() === "") {
toggle = true
inp.value = "";
}
});
if(!toggle){
localStorage.clear();
alert("Order Placed")
window.location.reload();
}
else{
alert("Fill the form Correctly")
}
});
}
placeOrder();