// Track product views
function trackProductView(productId) {
let viewCounts = JSON.parse(localStorage.getItem('viewCounts') || '{}');
viewCounts[productId] = (viewCounts[productId] || 0) + 1;
localStorage.setItem('viewCounts', JSON.stringify(viewCounts));
}
// Track add to cart events
function trackAddToCart(productId) {
let addToCartCounts = JSON.parse(localStorage.getItem('addToCartCounts') || '{}');
addToCartCounts[productId] = (addToCartCounts[productId] || 0) + 1;
localStorage.setItem('addToCartCounts', JSON.stringify(addToCartCounts));
}
document.addEventListener('DOMContentLoaded', function() {
// Function to get cart items from local storage
function getCartItems() {
const cartItems = localStorage.getItem('cart');
return cartItems ? JSON.parse(cartItems) : [];
}
// Function to set cart items in local storage
function setCartItems(cartItems) {
localStorage.setItem('cart', JSON.stringify(cartItems));
}
// Function to add an item to the cart
function addToCart(product) {
trackAddToCart(product.id); // Track add to cart event
let cartItems = getCartItems();
const existingItem = cartItems.find(item => item.id === product.id);
if (existingItem) {
existingItem.quantity += 1;
} else {
product.quantity = 1;
cartItems.push(product);
}
setCartItems(cartItems);
updateCartUI();
}
function addToCart(product) {
trackAddToCart('product123'); // Track add to cart event
let cartItems = getCartItems();
const existingItem = cartItems.find(item => item.id === product.id);
if (existingItem) {
existingItem.quantity += 1;
} else {
product.quantity = 1;
cartItems.push(product);
}
setCartItems(cartItems);
updateCartUI();
}
function addToCart(product) {
let cartItems = getCartItems();
const existingItem = cartItems.find(item => item.id === product.id);
if (existingItem) {
existingItem.quantity += 1;
} else {
product.quantity = 1;
cartItems.push(product);
}
setCartItems(cartItems);
updateCartUI();
}
// Function to update item quantity in the cart
function updateQuantity(productId, quantity) {
let cartItems = getCartItems();
const itemIndex = cartItems.findIndex(item => item.id === productId);
if (itemIndex !== -1) {
cartItems[itemIndex].quantity = quantity;
if (cartItems[itemIndex].quantity <= 0) {
cartItems.splice(itemIndex, 1); // Remove item if quantity is 0 or less
}
setCartItems(cartItems);
updateCartUI();
}
updateCartUI(); // Call updateCartUI to update the order summary
}
// Function to remove an item from the cart
function removeItem(productId) {
let cartItems = getCartItems();
const itemIndex = cartItems.findIndex(item => item.id === productId);
if (itemIndex !== -1) {
cartItems.splice(itemIndex, 1);
setCartItems(cartItems);
updateCartUI();
}
}
// Function to calculate cart totals
function calculateTotals() {
let cartItems = getCartItems();
let subtotal = 0;
cartItems.forEach(item => {
subtotal += item.price * item.quantity;
});
const shipping = 5.00;
const total = subtotal + shipping;
return { subtotal, shipping, total };
}
// Function to display cart items in cart.html
function displayCartItems() {
let cartItems = getCartItems();
const cartItemsContainer = document.getElementById('cart-items');
if (cartItemsContainer) {
cartItemsContainer.innerHTML = ''; // Clear existing items
cartItems.forEach(item => {
const cartItemDiv = document.createElement('div');
cartItemDiv.classList.add('flex', 'items-center', 'border', 'border-gray-300', 'rounded-md', 'p-4');
cartItemDiv.innerHTML = `
$${item.price.toFixed(2)}