-
Notifications
You must be signed in to change notification settings - Fork 2
/
cart.js
141 lines (96 loc) · 3.76 KB
/
cart.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var cartThings = JSON.parse(localStorage.getItem("cartthings")) || [];
let Tprice=0;
reLoadM(cartThings);
function reLoadM(cartThings){
creatcart(cartThings);
function creatcart(cartThings) {
document.querySelector("#container").textContent = "";
cartThings.map(function (data, index) {
var div = document.createElement("div")
document.querySelector("#container").append(div)
// inside div creating image and image_url
var image = document.createElement("img");
image.src=data.image_link;
div.append(image);
var btn1 = document.createElement("button")
btn1.setAttribute("id", "btnwish")
btn1.textContent = "Wish"
btn1.style.display = "block"
div.append(btn1)
var name = document.createElement("p");
name.style.height = "35px"
name.textContent = data.name;
name.style.marginTop = "3%"
div.append(name);
var box = document.createElement("div")
box.textContent = data.box
box.setAttribute("id", "boxer")
div.append(box)
var p1 = document.createElement("p");
check0(data.MSRP);
function check0(val){
if(val!=undefined){
p1.innerHTML = `MSRP: <s>$${data.MSRP}</s>`;
}
}
p1.style.marginTop = "5%"
p1.style.color = "gray"
var p2 = document.createElement("p")
p2.textContent = `$${data.price}`;
Tprice=Tprice+Number(data.price);
p2.style.marginTop = "1%"
p2.style.fontSize = "24px"
p2.style.fontWeight = "bold"
div.append(p1, p2)
var btn2 = document.createElement("button")
btn2.setAttribute("id", "btnbuy");
btn2.textContent = "Delete Item"
div.append(btn2);
btn2.addEventListener("click", function () {
deletItems(index,data)
});
});
}
function deletItems(index,data) {
Tprice=Tprice-Number(data.price);
totalPrice.innerHTML=Tprice;
cartThings.splice(index, 1)
localStorage.setItem("cartthings", JSON.stringify(cartThings));
alert("Item Deleted");
reLoadM(cartThings);
window.location.reload();
}
let totalPrice= document.getElementById("totalPrice");
totalPrice.innerHTML=Tprice;
// ==============================================================================================================================================
var cartLength =JSON.parse(localStorage.getItem("cartthings"))
document.querySelector("#cardAval").innerHTML=`There are currently ${cartLength.length} items in your cart.`;
document.querySelector("#cartLen").innerHTML=`${cartLength.length}`;
var total = cartLength.reduce(function (a,b) {
return a+Number(b.price)
},0)
document.querySelector("#totalPrice").innerHTML=` $${total.toFixed(2)}`;
}
function navSlideOut(){
document.querySelector("#nav-link").style.left="-16px"
}
function navSlideIn(){
document.querySelector("#nav-link").style.left="-2000px"
}
function Tsort(){
var valT=document.querySelector("#sortT").value
console.log(valT)
if(valT==""){
reLoadM(cartThings);
}else if(valT=="high"){
cartThings.sort(function(a,b){
return (Number(b.price)-Number(a.price))
})
reLoadM(cartThings);
}else{
cartThings.sort(function(a,b){
return (Number(a.price)-Number(b.price));
})
reLoadM(cartThings);
}
}