Your Cart
| Product |
Price (FCFA) |
Quantity |
Subtotal (FCFA) |
Remove |
Cart Total
Subtotal:
$0.00
Final Total:
$0.00
Your cart is empty!
${formatCurrency(product.price)} |
|
${formatCurrency(product.price * product.quantity)} |
|
`;
cartItems.appendChild(row);
});
}
updateCartTotal();
}
// Function to update the quantity of a product in the cart
function updateQuantity(productId, quantity) {
let cart = JSON.parse(localStorage.getItem('cart')) || [];
const product = cart.find(p => p.id === productId);
if (product) {
product.quantity = parseInt(quantity);
localStorage.setItem('cart', JSON.stringify(cart));
updateCartItems();
}
}
// Function to remove a product from the cart
function removeFromCart(productId) {
let cart = JSON.parse(localStorage.getItem('cart')) || [];
cart = cart.filter(p => p.id !== productId);
localStorage.setItem('cart', JSON.stringify(cart));
updateCartCounter();
updateCartItems();
}
// Format currency for FCFA
function formatCurrency(amount) {
if (typeof amount !== 'number' || isNaN(amount)) return 'N/A';
return amount.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) + ' FCFA';
}
// Function to update the cart total
function updateCartTotal() {
const cart = JSON.parse(localStorage.getItem('cart')) || [];
const cartSubtotal = document.getElementById('cartSubtotal');
const cartTotal = document.getElementById('cartTotal');
const subtotal = cart.reduce((total, product) => total + product.price * product.quantity, 0);
cartSubtotal.textContent = formatCurrency(subtotal);
cartTotal.textContent = formatCurrency(subtotal);
}
// Coupon code logic (simple example)
function applyCoupon() {
const code = document.getElementById('couponCode').value.trim();
const couponError = document.getElementById('couponError');
// Example: 10% off for code 'SAVE10'
let cart = JSON.parse(localStorage.getItem('cart')) || [];
let subtotal = cart.reduce((total, product) => total + product.price * product.quantity, 0);
if (code === 'SAVE10') {
const discount = subtotal * 0.10;
const newTotal = subtotal - discount;
document.getElementById('cartTotal').textContent = formatCurrency(newTotal);
couponError.classList.add('hidden');
} else {
couponError.classList.remove('hidden');
updateCartTotal();
}
}
// Proceed to checkout (placeholder)
function proceedToCheckout() {
alert('Proceeding to checkout... (implement checkout logic)');
}
// Call updateCartItems on page load
document.addEventListener('DOMContentLoaded', () => {
updateCartCounter();
updateCartItems();
});