-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (61 loc) · 1.86 KB
/
app.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
const products = [
{
id: 1,
name: "Gold Necklace",
price: "1000",
image: "images/20220130_182022.jpg",
},
{
id: 2,
name: "Silver Bracelet",
price: "500",
image: "images/silver.jpg.webp",
},
{
id: 3,
name: "Diamond Ring",
price: "2500",
image: "images/diamond.webp",
},
];
const cart = [];
function addToCart(productId) {
const product = products.find((p) => p.id === productId);
cart.push(product);
renderCart();
// Removed updateLivePersonCartAndTitle from here if it was previously called here
}
function renderProducts() {
const productsContainer = document.getElementById("products");
productsContainer.innerHTML = "";
products.forEach((product) => {
productsContainer.innerHTML += `
<div class="product">
<img src="${product.image}" alt="${product.name}" style="width: 100%; max-width: 200px; height: auto;">
<h3>${product.name}</h3>
<p>Price: $${product.price}</p>
<button onclick="addToCart(${product.id})">Add to Cart</button>
</div>
`;
});
}
function renderCart() {
const cartContainer = document.getElementById("cart");
cartContainer.innerHTML = "";
cart.forEach((product) => {
cartContainer.innerHTML += `<li>${product.name} - $${product.price}</li>`;
});
updateCartTotal(); // Ensures cart total is updated before updating LivePerson
updateLivePersonCartAndTitle(); // Ensure this is called after cart is updated
}
function calculateCartTotal() {
return cart.reduce((total, product) => total + parseFloat(product.price), 0);
}
function updateCartTotal() {
const total = calculateCartTotal();
document.getElementById("cartTotal").innerText = `Total Price: $${total}`;
}
// Initial render
document.addEventListener("DOMContentLoaded", (event) => {
renderProducts();
});