-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
118 lines (102 loc) · 4.14 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
// document.querySelectorAll('.increase').forEach(button => {
// button.addEventListener('click', () => {
// const input = button.previousElementSibling;
// input.value = parseInt(input.value) + 1;
// updateCart();
// });
// });
// document.querySelectorAll('.decrease').forEach(button => {
// button.addEventListener('click', () => {
// const input = button.nextElementSibling;
// if (input.value > 1) {
// input.value = parseInt(input.value) - 1;
// updateCart();
// }
// });
// });
// document.querySelectorAll('.remove-item').forEach(button => {
// button.addEventListener('click', () => {
// button.closest('.cart-item').remove();
// updateCart();
// });
// });
// function updateCart() {
// // Update cart total and other details
// }
// cart.js
const cartItemsContainer = document.querySelector('.cart-items');
const cartSummary = document.querySelector('.cart-summary');
const cartTotalElement = document.getElementById('cartTotal');
const ptop = document.getElementById('ptop');
// Function to update the cart total
function updateCartTotal() {
let cartTotal = 0;
const cartItems = cartItemsContainer.querySelectorAll('.cart-item');
cartItems.forEach(item => {
const priceElement = item.querySelector('.item-details .p4:first-of-type');
const quantityElement = item.querySelector('.quantity-control input');
const price = parseFloat(priceElement.textContent.replace('$', ''));
const quantity = parseInt(quantityElement.value);
cartTotal += price * quantity;
});
cartTotalElement.textContent = `$${cartTotal.toFixed(2)}`;
ptop.textContent = `Subtotal: $${cartTotal.toFixed(2)}`;
}
// Function to add an item to the cart
function addToCart(item) {
// Get the product details
const imageSrc = item.querySelector('.product-image').src;
const title = item.querySelector('#des h5').textContent;
const price = parseFloat(item.querySelector('#des p1').textContent.replace('$', ''));
// Create a new cart item element
const cartItem = document.createElement('div');
cartItem.classList.add('cart-item');
cartItem.innerHTML = `
<img src="${imageSrc}" alt="${title}" class="cartitemimg">
<div class="item-details">
<h1>${title}</h1>
<p class="p4">$ ${price.toFixed(2)}</p>
<div class="quantity-control">
<button class="decrease">-</button>
<input type="number" value="1">
<button class="increase">+</button>
</div>
<button class="remove-item">Remove</button>
</div>
`;
// Add the new cart item to the cart container
cartItemsContainer.appendChild(cartItem);
// Add event listeners to quantity control buttons
const decreaseButton = cartItem.querySelector('.decrease');
const increaseButton = cartItem.querySelector('.increase');
decreaseButton.addEventListener('click', () => {
const quantityElement = cartItem.querySelector('.quantity-control input');
const quantity = parseInt(quantityElement.value);
if (quantity > 1) {
quantityElement.value = quantity - 1;
}
updateCartTotal();
});
increaseButton.addEventListener('click', () => {
const quantityElement = cartItem.querySelector('.quantity-control input');
const quantity = parseInt(quantityElement.value);
quantityElement.value = quantity + 1;
updateCartTotal();
});
// Add event listener to remove item button
const removeButton = cartItem.querySelector('.remove-item');
removeButton.addEventListener('click', () => {
cartItemsContainer.removeChild(cartItem);
updateCartTotal();
});
console.log('addToCart called with item:', item);
// Update the cart total
updateCartTotal();
}
// Add event listeners to "Add to Cart" buttons in the shop page
const addToCartButtons = document.querySelectorAll('#addToCartBtn');
addToCartButtons.forEach(button => {
button.addEventListener('click', () => {
addToCart(button.closest('.product-item'));
});
});