-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
59 lines (51 loc) · 2.06 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
// Example cart data (you can replace this with dynamic data)
const cartItems = [
{ name: "Painting 1", quantity: 1, price: 500 },
{ name: "Crochet Item", quantity: 2, price: 300 },
];
// DOM elements
const emptyCartMessage = document.getElementById("empty-cart-message");
const cartContent = document.getElementById("cart-content");
const cartItemsContainer = document.getElementById("cart-items");
const cartTotalElement = document.getElementById("cart-total");
const proceedPaymentButton = document.getElementById("proceed-payment");
// Function to calculate total price
function calculateTotal() {
return cartItems.reduce((total, item) => total + item.quantity * item.price, 0);
}
// Function to render the cart
function renderCart() {
if (cartItems.length === 0) {
emptyCartMessage.classList.remove("hidden");
cartContent.classList.add("hidden");
} else {
emptyCartMessage.classList.add("hidden");
cartContent.classList.remove("hidden");
cartItemsContainer.innerHTML = "";
cartItems.forEach((item) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${item.name}</td>
<td>${item.quantity}</td>
<td>₹${item.price * item.quantity}</td>
`;
cartItemsContainer.appendChild(row);
});
cartTotalElement.textContent = calculateTotal();
}
}
// Handle payment button click
proceedPaymentButton.addEventListener("click", () => {
const name = document.getElementById("name").value;
const address = document.getElementById("address").value;
const phone = document.getElementById("phone").value;
if (!name || !address || !phone) {
alert("Please fill in all shipping details before proceeding.");
return;
}
alert(`Order confirmed! Shipping to ${name}, ${address}.`);
// Redirect to payment gateway (mocked here)
window.location.href = "/payment.html";
});
// Render the cart on page load
renderCart();