bytebazaar / cart.html
Clings's picture
Add the functionalities
327ffe3 verified
Raw History Blame Contribute Delete
10.6 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Cart - ByteBazaar</title>
<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/feather-icons"></script>
</head>
<body class="bg-gray-50">
<!-- Navigation -->
<nav class="bg-white shadow-sm">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-16">
<div class="flex">
<div class="flex-shrink-0 flex items-center">
<a href="index.html" class="flex items-center">
<i data-feather="shopping-cart" class="text-primary h-6 w-6"></i>
<span class="ml-2 text-xl font-bold text-gray-900">ByteBazaar</span>
</a>
</div>
<div class="hidden sm:ml-6 sm:flex sm:space-x-8">
<a href="index.html" class="border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700 inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium">Home</a>
<a href="#" class="border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700 inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium">Explore</a>
<a href="#" class="border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700 inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium">Categories</a>
<a href="#" class="border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700 inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium">Sell</a>
<a href="cart.html" class="border-primary text-gray-900 inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium">Cart</a>
</div>
</div>
<div class="hidden sm:ml-6 sm:flex sm:items-center">
<div class="relative">
<input type="text" class="border border-gray-300 rounded-full py-2 px-4 pl-10 focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent" placeholder="Search products...">
<i data-feather="search" class="absolute left-3 top-2.5 text-gray-400"></i>
</div>
<button class="ml-4 bg-primary text-white px-4 py-2 rounded-md text-sm font-medium hover:bg-primary-600 transition duration-150 ease-in-out">Sign In</button>
<button class="ml-4 bg-secondary text-white px-4 py-2 rounded-md text-sm font-medium hover:bg-secondary-600 transition duration-150 ease-in-out">Sign Up</button>
</div>
<div class="-mr-2 flex items-center sm:hidden">
<button type="button" class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-primary">
<i data-feather="menu"></i>
</button>
</div>
</div>
</div>
</nav>
<!-- Cart Section -->
<div class="max-w-7xl mx-auto py-12 px-4 sm:px-6 lg:px-8">
<h1 class="text-3xl font-bold text-gray-900 mb-8">Your Shopping Cart</h1>
<div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
<!-- Cart Items -->
<div class="lg:col-span-2">
<div id="cart-items" class="bg-white shadow-sm rounded-lg divide-y divide-gray-200">
<!-- Items will be populated by JavaScript -->
<div class="p-6 text-center text-gray-500">
Your cart is empty. Start shopping now!
</div>
</div>
</div>
<!-- Order Summary -->
<div>
<div class="bg-white shadow-sm rounded-lg p-6">
<h2 class="text-lg font-medium text-gray-900 mb-4">Order Summary</h2>
<div class="space-y-4">
<div class="flex justify-between">
<span class="text-gray-600">Subtotal</span>
<span id="subtotal" class="text-gray-900">$0.00</span>
</div>
<div class="flex justify-between">
<span class="text-gray-600">Tax</span>
<span id="tax" class="text-gray-900">$0.00</span>
</div>
<div class="flex justify-between border-t border-gray-200 pt-4">
<span class="text-lg font-medium text-gray-900">Total</span>
<span id="total" class="text-lg font-medium text-gray-900">$0.00</span>
</div>
</div>
<button id="checkout-btn" class="mt-6 w-full bg-primary text-white py-3 rounded-md text-base font-medium hover:bg-primary-600 transition duration-150 ease-in-out">
Proceed to Checkout
</button>
<p class="mt-4 text-sm text-gray-500">
or <a href="index.html" class="text-primary font-medium hover:underline">continue shopping</a>
</p>
</div>
</div>
</div>
</div>
<script>
// Initialize cart from localStorage
let cart = JSON.parse(localStorage.getItem('cart')) || [];
// Update cart display
function updateCartDisplay() {
const cartItemsContainer = document.getElementById('cart-items');
const subtotalElement = document.getElementById('subtotal');
const taxElement = document.getElementById('tax');
const totalElement = document.getElementById('total');
if (cart.length === 0) {
cartItemsContainer.innerHTML = `
<div class="p-6 text-center text-gray-500">
Your cart is empty. Start shopping now!
</div>
`;
subtotalElement.textContent = '$0.00';
taxElement.textContent = '$0.00';
totalElement.textContent = '$0.00';
return;
}
// Calculate totals
const subtotal = cart.reduce((sum, item) => sum + item.price, 0);
const tax = subtotal * 0.08; // 8% tax
const total = subtotal + tax;
// Update summary
subtotalElement.textContent = `$${subtotal.toFixed(2)}`;
taxElement.textContent = `$${tax.toFixed(2)}`;
totalElement.textContent = `$${total.toFixed(2)}`;
// Render cart items
cartItemsContainer.innerHTML = '';
cart.forEach((item, index) => {
const itemElement = document.createElement('div');
itemElement.className = 'p-6 flex items-start';
itemElement.innerHTML = `
<div class="flex-shrink-0 h-20 w-20 rounded-md overflow-hidden bg-gray-200">
<img src="http://static.photos/technology/200x200/${index + 1}" alt="${item.name}" class="h-full w-full object-cover">
</div>
<div class="ml-4 flex-1">
<div class="flex justify-between text-base font-medium text-gray-900">
<h3>${item.name}</h3>
<p class="ml-4">$${item.price.toFixed(2)}</p>
</div>
<div class="mt-2 flex justify-between text-sm text-gray-500">
<div>
<button class="text-primary hover:text-primary-600" onclick="removeItem(${index})">Remove</button>
</div>
<div class="flex items-center">
<button class="text-gray-400 hover:text-gray-600" onclick="updateQuantity(${index}, -1)">
<i data-feather="minus" class="h-4 w-4"></i>
</button>
<span class="mx-2">1</span>
<button class="text-gray-400 hover:text-gray-600" onclick="updateQuantity(${index}, 1)">
<i data-feather="plus" class="h-4 w-4"></i>
</button>
</div>
</div>
</div>
`;
cartItemsContainer.appendChild(itemElement);
});
feather.replace();
}
// Remove item from cart
function removeItem(index) {
cart.splice(index, 1);
localStorage.setItem('cart', JSON.stringify(cart));
updateCartDisplay();
updateCartCount();
}
// Update item quantity (currently just 1 per item)
function updateQuantity(index, change) {
// In a real app, you would update the quantity here
// For now, we'll just show an alert
alert('Quantity adjustment functionality coming soon!');
}
// Update cart count in header
function updateCartCount() {
const cartCount = document.getElementById('cart-count');
if (cartCount) {
cartCount.textContent = cart.length;
cartCount.style.display = cart.length > 0 ? 'flex' : 'none';
}
}
// Checkout button handler
document.getElementById('checkout-btn').addEventListener('click', () => {
if (cart.length === 0) {
alert('Your cart is empty!');
return;
}
alert('Proceeding to checkout!');
// In a real app, this would redirect to a checkout page
});
// Initialize on page load
document.addEventListener('DOMContentLoaded', () => {
updateCartDisplay();
updateCartCount();
});
</script>
</body>
</html>