-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
120 lines (102 loc) · 4.85 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
async function getProducts() {
const productsResponse = await fetch("https://fakestoreapi.com/products");
const productsData = await productsResponse.json();
return productsData;
}
async function productsRender() {
// gelen datayı burda düzenlenmesi bekleyerek products içine atıyoruz ve bu fonksiyonun içinde kullanmaya devam ediyoruz
let products = JSON.parse(localStorage.getItem("cartProducts"));
if (!products) {
products = await getProducts();
localStorage.setItem("cartProducts", JSON.stringify(products));
}
const productTableBody = document.getElementById("productTableBody");
products.forEach(product => {
const productRow = document.createElement("tr");
productRow.classList.add("table-row");
productRow.setAttribute('data-product-id', product.id);
const limitedTitle = product.title.length > 20 ? product.title.substring(0, 20) + '...' : product.title;
productRow.innerHTML = `
<td class="table-product-name">
<button onclick="deleteProduct(${product.id})" class="delete-product-btn">X</button>
<img class="table-product-img" src="${product.image}" alt="${product.title}">
<p class="product-name">${limitedTitle}</p>
</td>
<td class="table-price">$${product.price}</td>
<td class="table-quantity-container">
<p class="table-quantity-value">1</p>
<div class="quantity-buttons">
<button onclick="quantityIncrease(${product.id})" class="quantity-button">
<img src="images/arrow-vector.png">
</button>
<button onclick="quantityDecrease(${product.id})" class="quantity-button">
<img id="quantityDecrease" src="images/arrow-vector.png">
</button>
</div>
</td>
<td class="table-subtotal">$${product.price}</td>
`;
productTableBody.appendChild(productRow);
});
updateTotalSum();
}
function deleteProduct(productId) {
const rowElement = document.querySelector(`tr[data-product-id="${productId}"]`);
rowElement.remove();
let cartProducts = JSON.parse(localStorage.getItem("cartProducts")) || [] ;
cartProducts = cartProducts.filter(product => product.id !== productId);
localStorage.setItem("cartProducts", JSON.stringify(cartProducts));
productsRender();
}
function quantityDecrease (productId) {
const rowElement = document.querySelector(`tr[data-product-id="${productId}"]`);
const quantityValueElement = rowElement.querySelector(".table-quantity-value");
let quantityValue = parseInt(quantityValueElement.innerText);
if (quantityValue > 1) {
quantityValueElement.innerText = quantityValue - 1;
const priceElement = rowElement.querySelector(".table-price");
const price = parseFloat(priceElement.innerText.replace('$', ''));
const totalProductPrice = rowElement.querySelector(".table-subtotal");
totalProductPrice.innerText = `$${((quantityValue - 1) * price).toFixed(2)}`;
updateTotalSum();
}
}
function quantityIncrease (productId) {
const rowElement = document.querySelector(`tr[data-product-id="${productId}"]`);
const quantityValueElement = rowElement.querySelector(".table-quantity-value");
let quantityValue = parseInt(quantityValueElement.innerText);
quantityValueElement.innerText = quantityValue + 1;
const priceElement = rowElement.querySelector(".table-price");
const price = parseFloat(priceElement.innerText.replace('$', ''));
const totalProductPrice = rowElement.querySelector(".table-subtotal");
totalProductPrice.innerText = `$${((quantityValue + 1) * price).toFixed(2)}`;
updateTotalSum();
}
function updateTotalSum () {
const subtotalTable = document.querySelectorAll(".table-subtotal");
let totalSumTable = 0;
subtotalTable.forEach(subtotalElement => {
const subtotal = parseFloat(subtotalElement.innerText.replace('$', ''));
totalSumTable += subtotal;
});
const totalSumProducts = document.getElementById("totalSumProducts");
totalSumProducts.innerText = `$${totalSumTable.toFixed(2)}`;
const totalPrice = document.getElementById("totalPrice");
totalPrice.innerText = `$${totalSumTable.toFixed(2)}`;
}
document.querySelector('.coupon-button').addEventListener('click', function() {
const couponCode = document.getElementById('couponInput').value;
const subtotalElement = document.getElementById('totalSumProducts');
const totalPriceElement = document.getElementById('totalPrice');
const subtotal = parseFloat(subtotalElement.innerText.replace('$', ''));
const discountQuantity = subtotal * 0.20;
if (couponCode === 'COUPON20') {
const discountPrice = subtotal - discountQuantity;
totalPriceElement.innerText = `$${discountPrice.toFixed(2)}`;
document.getElementById('discount').innerText = `$${discountQuantity.toFixed(2)}`;
} else {
totalPriceElement.innerText = `$${subtotal.toFixed(2)}`;
document.getElementById('discount').innerText = `$0`;
}
});
productsRender();