-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
169 lines (145 loc) · 4.08 KB
/
script.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
let sepetler = document.querySelectorAll(".sepete-ekle");
let urunler = [
{
name: 'Moza Racing R5 Set',
tag: 'moza1',
price: 20700,
inCart: 0,
id: 1
},
{
name: 'Moza FSR Wheel',
tag: 'mozafsr',
price: 20420,
inCart: 0
},
{
name: 'Moza GS Wheel',
tag: 'moza3',
price: 16800,
inCart: 0
},
{
name: 'Ortombo DD1 Stand',
tag: 'ortombo-dd1',
price: 4300,
inCart: 0
},
{
name: 'Ortombo Phase 1 Hybrid',
tag: 'ortombo-phase1',
price: 16300,
inCart: 0
},
{
name: 'Ortombo Podium Formula',
tag: 'ortombo-formula-podium',
price: 20700,
inCart: 0
},
{
name: 'Moza R9 Base',
tag: 'mozabase',
price: 15000,
inCart: 0
},
{
name: 'Thrustmaster th8a vites',
tag: 'th8ashifter',
price: 8000,
inCart: 0
},
];
for (let i = 0; i < sepetler.length; i++) {
sepetler[i].addEventListener('click' , () => {
sepetSayisi(urunler[i]);
toplamFiyat(urunler[i]);
})
}
function onLoadSepetSayisi() {
let urunSayisi = localStorage.getItem('sepetSayisi');
if(urunSayisi) {
document.querySelector('.sepet span').textContent = urunSayisi;
}
}
function sepetSayisi(urun) {
let urunSayisi = localStorage.getItem('sepetSayisi');
urunSayisi = parseInt(urunSayisi);
if (urunSayisi) {
localStorage.setItem('sepetSayisi' , urunSayisi + 1);
document.querySelector('.sepet span').textContent = urunSayisi + 1;
} else {
localStorage.setItem('sepetSayisi' , 1);
document.querySelector('.sepet span').textContent = 1;
}
setItems(urun)
}
function setItems(urun) {
let sepetUrunleri = localStorage.getItem('sepettekiUrunler');
sepetUrunleri = JSON.parse(sepetUrunleri);
if (sepetUrunleri != null) {
if(sepetUrunleri[urun.tag] == undefined){
sepetUrunleri = {
...sepetUrunleri,
[urun.tag]: urun
}
}
sepetUrunleri[urun.tag].inCart += 1;
} else {
urun.inCart = 1;
sepetUrunleri = {
[urun.tag]: urun
}
}
localStorage.setItem("sepettekiUrunler", JSON.stringify(sepetUrunleri));
}
function toplamFiyat(urun) {
// console.log("Ürünlerin fiyatı", urun.price);
let sepetFiyati = localStorage.getItem('toplamFiyat');
console.log("Sepetimin tutarı ", sepetFiyati);
console.log(typeof sepetFiyati);
if(sepetFiyati != null) {
sepetFiyati = parseInt(sepetFiyati);
localStorage.setItem("toplamFiyat", sepetFiyati + urun.price);
}else {
localStorage.setItem("toplamFiyat", urun.price);
}
}
function displayCart () {
let cartItems = localStorage.getItem("sepettekiUrunler");
cartItems = JSON.parse(cartItems);
let productContainer = document.querySelector(".products");
let sepetFiyati = localStorage.getItem('toplamFiyat');
let sepetinToplami = document.querySelector(".sepetinToplami");
let sepetYazdir = `
Sepetin Toplamı: ${sepetFiyati} TL
`;
console.log(cartItems);
if(cartItems && productContainer) {
productContainer.innerHTML = '';
Object.values(cartItems).map(item => {
productContainer.innerHTML += `
<div class="product">
<img style="width:200px; height:200px;" src="./img/${item.tag}.png">
<span>${item.name}</span>
</div>
<div class="price">
${item.price} TL
</div>
<div class="quantity">
<span>${item.inCart}</span>
</div>
<div class="total">
${item.inCart * item.price} TL
</div>
`
document.querySelector(".yazartik").innerHTML = sepetYazdir;
});
}
}
function temizle() {
localStorage.clear();
location.reload();
}
onLoadSepetSayisi();
displayCart();