-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcart.js
135 lines (108 loc) · 3.92 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
const cartBtn = document.getElementById("cartBtn");
const shipCart = document.querySelector(".ship-to-cart-count");
cartBtn.addEventListener("click", function () {
window.location.href = "cart.html";
});
function checkout() {
localStorage.removeItem("cartItems");
location.reload();
}
var cartItems = [];
if (localStorage.getItem("cartItems")) {
cartItems = JSON.parse(localStorage.getItem("cartItems"));
}
function calculateTotalPrice() {
const totalPrice = cartItems
.map((item) => {
const parsedPrice = parseFloat(item.price.replace("$", ""));
return parsedPrice;
})
.reduce((total, price) => total + price, 0);
return totalPrice.toFixed(2);
}
function renderCartItems() {
const cartItemsDiv = document.getElementById("cart-items");
const noCartInfoParagraph = document.querySelector(".no-cart-info");
cartItemsDiv.innerHTML = "";
if (cartItems.length === 0) {
noCartInfoParagraph.classList.remove("d-none");
} else {
noCartInfoParagraph.classList.add("d-none");
cartItems.forEach((item, index) => {
const card = document.createElement("div");
card.classList.add("card");
card.style.width = "18rem";
const image = document.createElement("img");
image.src = item.imageSrc;
image.classList.add("card-img-top");
card.appendChild(image);
const cardBody = document.createElement("div");
cardBody.classList.add("card-body");
const title = document.createElement("h5");
title.classList.add("card-title");
title.textContent = item.name;
cardBody.appendChild(title);
const info = document.createElement("p");
info.classList.add("card-text");
info.textContent = item.info;
cardBody.appendChild(info);
const price = document.createElement("p");
price.classList.add("card-text");
price.textContent = item.price;
cardBody.appendChild(price);
const removeButton = document.createElement("button");
removeButton.classList.add("btn", "btn-danger");
removeButton.textContent = "Remove";
removeButton.addEventListener("click", () => removeFromCart(index));
cardBody.appendChild(removeButton);
card.appendChild(cardBody);
cartItemsDiv.appendChild(card);
const cartValue = document.getElementById("cartValue");
cartValue.innerHTML = cartItems.length;
const totalPrice = calculateTotalPrice();
shipCart.innerHTML = `${cartItems.length} Items ($${totalPrice})`;
});
}
}
function removeFromCart(index) {
cartItems.splice(index, 1);
localStorage.setItem("cartItems", JSON.stringify(cartItems));
renderCartItems();
location.reload();
}
function addToCart(index) {
const productElement = document.querySelectorAll(".bg")[index];
const productName = productElement.querySelector(".product-name").innerText;
const productInfo = productElement.querySelector(".product-info").innerText;
const productPrice = productElement.querySelector(".product-price").innerText;
const productImageSrc = productElement.querySelector(".image").src;
const existingProductIndex = cartItems.findIndex(
(item) => item.name === productName
);
if (existingProductIndex !== -1) {
const confirmation = confirm(
"This product is already in your cart. Do you want to add another one?"
);
if (!confirmation) {
return;
}
}
const product = {
name: productName,
info: productInfo,
price: productPrice,
imageSrc: productImageSrc,
};
cartItems.push(product);
localStorage.setItem("cartItems", JSON.stringify(cartItems));
const addToCartButton = productElement.querySelector(".add-to-cart-btn");
addToCartButton.classList.add("btn", "btn-success");
addToCartButton.textContent = "Added to Cart";
}
document.querySelectorAll(".add-to-cart-btn").forEach(function (button, index) {
button.addEventListener("click", function () {
addToCart(index);
renderCartItems();
});
});
renderCartItems();