Shopping cart page added
This commit is contained in:
parent
233708e402
commit
c56dc9340f
166
cart.php
Normal file
166
cart.php
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Shopping Cart - The Cozy Corner Cafe</title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||||
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700&family=Lato:wght@400;700&display=swap" rel="stylesheet">
|
||||||
|
<style>
|
||||||
|
:root {
|
||||||
|
--primary-color: #6b7b62;
|
||||||
|
--secondary-color: #8e9b83;
|
||||||
|
--background-color: #8e9b83;
|
||||||
|
--surface-color: #a2b199;
|
||||||
|
--text-color: #424c3b;
|
||||||
|
--heading-font: 'Playfair Display', serif;
|
||||||
|
--body-font: 'Lato', sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
font-family: var(--body-font);
|
||||||
|
color: var(--text-color);
|
||||||
|
background-color: var(--background-color);
|
||||||
|
padding-top: 70px; /* For fixed navbar */
|
||||||
|
}
|
||||||
|
|
||||||
|
h1, h2, h3, h4, h5, h6 {
|
||||||
|
font-family: var(--heading-font);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navbar {
|
||||||
|
background-color: var(--background-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
footer {
|
||||||
|
background-color: var(--text-color);
|
||||||
|
color: var(--secondary-color);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<header>
|
||||||
|
<nav class="navbar navbar-expand-lg navbar-light fixed-top shadow-sm">
|
||||||
|
<div class="container">
|
||||||
|
<a class="navbar-brand" href="index.php">The Cozy Corner</a>
|
||||||
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
|
||||||
|
<span class="navbar-toggler-icon"></span>
|
||||||
|
</button>
|
||||||
|
<div class="collapse navbar-collapse" id="navbarNav">
|
||||||
|
<ul class="navbar-nav ms-auto">
|
||||||
|
<li class="nav-item"><a class="nav-link" href="index.php#home">Home</a></li>
|
||||||
|
<li class="nav-item"><a class="nav-link" href="index.php#menu">Menu</a></li>
|
||||||
|
<li class="nav-item"><a class="nav-link" href="index.php#about">About</a></li>
|
||||||
|
<li class="nav-item"><a class="nav-link" href="index.php#contact">Contact</a></li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="cart.php">
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-cart" viewBox="0 0 16 16">
|
||||||
|
<path d="M0 1.5A.5.5 0 0 1 .5 1H2a.5.5 0 0 1 .485.379L2.89 3H14.5a.5.5 0 0 1 .491.592l-1.5 8A.5.5 0 0 1 13 12H4a.5.5 0 0 1-.491-.408L2.01 3.607 1.61 2H.5a.5.5 0 0 1-.5-.5zM3.102 4l1.313 7h8.17l1.313-7H3.102zM5 12a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm7 0a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm-7 1a1 1 0 1 1 0 2 1 1 0 0 1 0-2zm7 0a1 1 0 1 1 0 2 1 1 0 0 1 0-2z"/>
|
||||||
|
</svg>
|
||||||
|
<span class="badge bg-primary rounded-pill" id="cart-count">0</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main class="py-5">
|
||||||
|
<div class="container">
|
||||||
|
<h1 class="text-center mb-5">Shopping Cart</h1>
|
||||||
|
<div id="cart-container"></div>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<footer class="py-4 text-center mt-auto">
|
||||||
|
<div class="container">
|
||||||
|
<p>© 2025 The Cozy Corner Cafe. All Rights Reserved.</p>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
|
<script>
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
const cartContainer = document.getElementById('cart-container');
|
||||||
|
let cart = JSON.parse(localStorage.getItem('cart')) || {};
|
||||||
|
|
||||||
|
updateCartCount();
|
||||||
|
|
||||||
|
if (Object.keys(cart).length === 0) {
|
||||||
|
cartContainer.innerHTML = `
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-body text-center">
|
||||||
|
<p>Your cart is currently empty.</p>
|
||||||
|
<a href="index.php#menu" class="btn btn-primary">Continue Shopping</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
} else {
|
||||||
|
let table = `
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-body">
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Item</th>
|
||||||
|
<th>Price</th>
|
||||||
|
<th>Quantity</th>
|
||||||
|
<th>Total</th>
|
||||||
|
<th></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
`;
|
||||||
|
|
||||||
|
let grandTotal = 0;
|
||||||
|
for (const name in cart) {
|
||||||
|
const item = cart[name];
|
||||||
|
const total = item.price * item.quantity;
|
||||||
|
grandTotal += total;
|
||||||
|
table += `
|
||||||
|
<tr>
|
||||||
|
<td>${name}</td>
|
||||||
|
<td>${item.price.toFixed(2)}</td>
|
||||||
|
<td>${item.quantity}</td>
|
||||||
|
<td>${total.toFixed(2)}</td>
|
||||||
|
<td><button class="btn btn-danger btn-sm" onclick="removeFromCart('${name}')">Remove</button></td>
|
||||||
|
</tr>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
table += `
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<hr>
|
||||||
|
<div class="text-end">
|
||||||
|
<h4>Grand Total: ${grandTotal.toFixed(2)}</h4>
|
||||||
|
<button class="btn btn-primary">Proceed to Checkout</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
cartContainer.innerHTML = table;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function removeFromCart(name) {
|
||||||
|
let cart = JSON.parse(localStorage.getItem('cart')) || {};
|
||||||
|
if (cart[name]) {
|
||||||
|
delete cart[name];
|
||||||
|
}
|
||||||
|
localStorage.setItem('cart', JSON.stringify(cart));
|
||||||
|
location.reload(); // Reload to update the cart display
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateCartCount() {
|
||||||
|
let cart = JSON.parse(localStorage.getItem('cart')) || {};
|
||||||
|
let count = Object.values(cart).reduce((sum, item) => sum + item.quantity, 0);
|
||||||
|
document.getElementById('cart-count').innerText = count;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
103
index.php
103
index.php
@ -89,7 +89,7 @@
|
|||||||
<li class="nav-item"><a class="nav-link" href="#about">About</a></li>
|
<li class="nav-item"><a class="nav-link" href="#about">About</a></li>
|
||||||
<li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
|
<li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link" href="#" data-bs-toggle="modal" data-bs-target="#cartModal">
|
<a class="nav-link" href="cart.php">
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-cart" viewBox="0 0 16 16">
|
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-cart" viewBox="0 0 16 16">
|
||||||
<path d="M0 1.5A.5.5 0 0 1 .5 1H2a.5.5 0 0 1 .485.379L2.89 3H14.5a.5.5 0 0 1 .491.592l-1.5 8A.5.5 0 0 1 13 12H4a.5.5 0 0 1-.491-.408L2.01 3.607 1.61 2H.5a.5.5 0 0 1-.5-.5zM3.102 4l1.313 7h8.17l1.313-7H3.102zM5 12a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm7 0a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm-7 1a1 1 0 1 1 0 2 1 1 0 0 1 0-2zm7 0a1 1 0 1 1 0 2 1 1 0 0 1 0-2z"/>
|
<path d="M0 1.5A.5.5 0 0 1 .5 1H2a.5.5 0 0 1 .485.379L2.89 3H14.5a.5.5 0 0 1 .491.592l-1.5 8A.5.5 0 0 1 13 12H4a.5.5 0 0 1-.491-.408L2.01 3.607 1.61 2H.5a.5.5 0 0 1-.5-.5zM3.102 4l1.313 7h8.17l1.313-7H3.102zM5 12a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm7 0a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm-7 1a1 1 0 1 1 0 2 1 1 0 0 1 0-2zm7 0a1 1 0 1 1 0 2 1 1 0 0 1 0-2z"/>
|
||||||
</svg>
|
</svg>
|
||||||
@ -184,31 +184,6 @@
|
|||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<!-- Cart Modal -->
|
|
||||||
<div class="modal fade" id="cartModal" tabindex="-1" aria-labelledby="cartModalLabel" aria-hidden="true">
|
|
||||||
<div class="modal-dialog modal-lg">
|
|
||||||
<div class="modal-content">
|
|
||||||
<div class="modal-header">
|
|
||||||
<h5 class="modal-title" id="cartModalLabel">Shopping Cart</h5>
|
|
||||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
||||||
</div>
|
|
||||||
<div class="modal-body">
|
|
||||||
<div id="cart-items">
|
|
||||||
<!-- Cart items will be injected here by JavaScript -->
|
|
||||||
</div>
|
|
||||||
<hr>
|
|
||||||
<div class="text-end">
|
|
||||||
<h5>Total: $<span id="cart-total">0.00</span></h5>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="modal-footer">
|
|
||||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
||||||
<button type="button" class="btn btn-primary">Checkout</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<footer class="py-4 text-center">
|
<footer class="py-4 text-center">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<p>© 2025 The Cozy Corner Cafe. All Rights Reserved.</p>
|
<p>© 2025 The Cozy Corner Cafe. All Rights Reserved.</p>
|
||||||
@ -227,74 +202,28 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Shopping Cart Logic
|
|
||||||
let cart = [];
|
|
||||||
|
|
||||||
function addToCart(name, price) {
|
function addToCart(name, price) {
|
||||||
// Check if item is already in cart
|
let cart = JSON.parse(localStorage.getItem('cart')) || {};
|
||||||
const existingItem = cart.find(item => item.name === name);
|
if (cart[name]) {
|
||||||
if (existingItem) {
|
cart[name].quantity++;
|
||||||
existingItem.quantity++;
|
|
||||||
} else {
|
} else {
|
||||||
cart.push({ name, price, quantity: 1 });
|
cart[name] = {
|
||||||
|
price: price,
|
||||||
|
quantity: 1
|
||||||
|
};
|
||||||
}
|
}
|
||||||
renderCart();
|
localStorage.setItem('cart', JSON.stringify(cart));
|
||||||
|
updateCartCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeFromCart(name) {
|
function updateCartCount() {
|
||||||
const itemIndex = cart.findIndex(item => item.name === name);
|
let cart = JSON.parse(localStorage.getItem('cart')) || {};
|
||||||
if (itemIndex > -1) {
|
let count = Object.values(cart).reduce((sum, item) => sum + item.quantity, 0);
|
||||||
cart.splice(itemIndex, 1);
|
document.getElementById('cart-count').innerText = count;
|
||||||
}
|
|
||||||
renderCart();
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateQuantity(name, quantity) {
|
|
||||||
const item = cart.find(item => item.name === name);
|
|
||||||
if (item) {
|
|
||||||
item.quantity = parseInt(quantity);
|
|
||||||
if (item.quantity <= 0) {
|
|
||||||
removeFromCart(name);
|
|
||||||
} else {
|
|
||||||
renderCart();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderCart() {
|
|
||||||
const cartItemsContainer = document.getElementById('cart-items');
|
|
||||||
const cartTotalEl = document.getElementById('cart-total');
|
|
||||||
const cartCountEl = document.getElementById('cart-count');
|
|
||||||
let total = 0;
|
|
||||||
let totalItems = 0;
|
|
||||||
|
|
||||||
cartItemsContainer.innerHTML = '';
|
|
||||||
|
|
||||||
if (cart.length === 0) {
|
|
||||||
cartItemsContainer.innerHTML = '<p>Your cart is empty.</p>';
|
|
||||||
}
|
|
||||||
|
|
||||||
cart.forEach(item => {
|
|
||||||
total += item.price * item.quantity;
|
|
||||||
totalItems += item.quantity;
|
|
||||||
cartItemsContainer.innerHTML += `
|
|
||||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
||||||
<div>
|
|
||||||
<h6 class="my-0">${item.name}</h6>
|
|
||||||
</div>
|
|
||||||
<div class="d-flex align-items-center">
|
|
||||||
<input type="number" class="form-control form-control-sm" value="${item.quantity}" onchange="updateQuantity('${item.name}', this.value)" style="width: 60px; margin-right: 10px;">
|
|
||||||
<span class="text-muted">${(item.price * item.quantity).toFixed(2)}</span>
|
|
||||||
<button class="btn btn-sm btn-danger ms-2" onclick="removeFromCart('${item.name}')">Remove</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
});
|
|
||||||
|
|
||||||
cartTotalEl.textContent = total.toFixed(2);
|
|
||||||
cartCountEl.textContent = totalItems;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update cart count on page load
|
||||||
|
document.addEventListener('DOMContentLoaded', updateCartCount);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user