Spaces:
Running
Running
Implemented image replacement, dynamic product page, cart functionality, checkout, confirmation, and product tracking.
#24
by
smolSWE
- opened
- cart.html +1 -0
- checkout.html +23 -2
- confirmation.html +24 -2
- data.js +24 -0
- product.html +46 -6
- script.js +117 -260
cart.html
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="en">
|
3 |
<head>
|
|
|
1 |
+
|
2 |
<!DOCTYPE html>
|
3 |
<html lang="en">
|
4 |
<head>
|
checkout.html
CHANGED
@@ -36,8 +36,29 @@
|
|
36 |
<input type="text" id="address" name="address" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
37 |
</div>
|
38 |
<div class="mb-4">
|
39 |
-
<label for="
|
40 |
-
<input type="text" id="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
</div>
|
42 |
<button type="submit" class="bg-yellow-500 text-gray-800 px-4 py-2 rounded-md hover:bg-yellow-400">Place Order</button>
|
43 |
</form>
|
|
|
36 |
<input type="text" id="address" name="address" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
37 |
</div>
|
38 |
<div class="mb-4">
|
39 |
+
<label for="city" class="block text-gray-700 text-sm font-bold mb-2">City:</label>
|
40 |
+
<input type="text" id="city" name="city" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
41 |
+
</div>
|
42 |
+
<div class="mb-4">
|
43 |
+
<label for="state" class="block text-gray-700 text-sm font-bold mb-2">State:</label>
|
44 |
+
<input type="text" id="state" name="state" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
45 |
+
</div>
|
46 |
+
<div class="mb-4">
|
47 |
+
<label for="zip" class="block text-gray-700 text-sm font-bold mb-2">Zip Code:</label>
|
48 |
+
<input type="text" id="zip" name="zip" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
49 |
+
</div>
|
50 |
+
<h2 class="text-xl font-bold mb-2">Payment Information</h2>
|
51 |
+
<div class="mb-4">
|
52 |
+
<label for="credit_card" class="block text-gray-700 text-sm font-bold mb-2">Credit Card Number:</label>
|
53 |
+
<input type="text" id="credit_card" name="credit_card" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
54 |
+
</div>
|
55 |
+
<div class="mb-4">
|
56 |
+
<label for="expiration_date" class="block text-gray-700 text-sm font-bold mb-2">Expiration Date:</label>
|
57 |
+
<input type="text" id="expiration_date" name="expiration_date" placeholder="MM/YY" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
58 |
+
</div>
|
59 |
+
<div class="mb-4">
|
60 |
+
<label for="cvv" class="block text-gray-700 text-sm font-bold mb-2">CVV:</label>
|
61 |
+
<input type="text" id="cvv" name="cvv" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
62 |
</div>
|
63 |
<button type="submit" class="bg-yellow-500 text-gray-800 px-4 py-2 rounded-md hover:bg-yellow-400">Place Order</button>
|
64 |
</form>
|
confirmation.html
CHANGED
@@ -23,8 +23,30 @@
|
|
23 |
|
24 |
<main class="container mx-auto mt-8">
|
25 |
<h1 class="text-2xl font-bold mb-4">Order Confirmation</h1>
|
26 |
-
<
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
</main>
|
29 |
|
30 |
<footer class="bg-gray-800 p-4 text-white text-center mt-8">
|
|
|
23 |
|
24 |
<main class="container mx-auto mt-8">
|
25 |
<h1 class="text-2xl font-bold mb-4">Order Confirmation</h1>
|
26 |
+
<div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
|
27 |
+
<h2 class="text-xl font-bold mb-4">Thank you for your order!</h2>
|
28 |
+
<div class="mb-4">
|
29 |
+
<h3 class="text-lg font-bold mb-2">Shipping Address:</h3>
|
30 |
+
<p>Name: <span id="shipping-name"></span></p>
|
31 |
+
<p>Address: <span id="shipping-address"></span></p>
|
32 |
+
<p>City: <span id="shipping-city"></span></p>
|
33 |
+
<p>State: <span id="shipping-state"></span></p>
|
34 |
+
<p>Zip Code: <span id="shipping-zip"></span></p>
|
35 |
+
</div>
|
36 |
+
<div class="mb-4">
|
37 |
+
<h3 class="text-lg font-bold mb-2">Payment Information:</h3>
|
38 |
+
<p>Credit Card: <span id="payment-credit-card">XXXX-XXXX-XXXX-1234</span></p>
|
39 |
+
<p>Expiration Date: <span id="payment-expiration-date">12/24</span></p>
|
40 |
+
<p>CVV: <span id="payment-cvv">123</span></p>
|
41 |
+
</div>
|
42 |
+
<div>
|
43 |
+
<h3 class="text-lg font-bold mb-2">Order Details:</h3>
|
44 |
+
<ul id="order-items">
|
45 |
+
<!-- Order items will be dynamically added here -->
|
46 |
+
</ul>
|
47 |
+
<p class="font-bold">Total: <span id="order-total">$0.00</span></p>
|
48 |
+
</div>
|
49 |
+
</div>
|
50 |
</main>
|
51 |
|
52 |
<footer class="bg-gray-800 p-4 text-white text-center mt-8">
|
data.js
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
const products = [
|
3 |
+
{
|
4 |
+
id: "1",
|
5 |
+
name: "Product 1",
|
6 |
+
description: "This is the first product.",
|
7 |
+
price: 25.00,
|
8 |
+
image: "https://placehold.co/600x400/png"
|
9 |
+
},
|
10 |
+
{
|
11 |
+
id: "2",
|
12 |
+
name: "Product 2",
|
13 |
+
description: "This is the second product.",
|
14 |
+
price: 50.00,
|
15 |
+
image: "https://placehold.co/600x400/png"
|
16 |
+
},
|
17 |
+
{
|
18 |
+
id: "3",
|
19 |
+
name: "Product 3",
|
20 |
+
description: "This is the third product.",
|
21 |
+
price: 75.00,
|
22 |
+
image: "https://placehold.co/600x400/png"
|
23 |
+
}
|
24 |
+
];
|
product.html
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="en">
|
3 |
<head>
|
@@ -36,9 +37,9 @@
|
|
36 |
</div>
|
37 |
</div>
|
38 |
<div>
|
39 |
-
<h1 class="text-2xl font-bold">Product Title</h1>
|
40 |
-
<p class="text-gray-600">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
|
41 |
-
<p class="text-xl font-semibold">$99.99</p>
|
42 |
<div class="flex items-center mb-4">
|
43 |
<i class="fas fa-star text-yellow-500"></i>
|
44 |
<i class="fas fa-star text-yellow-500"></i>
|
@@ -47,7 +48,7 @@
|
|
47 |
<i class="fas fa-star-half-alt text-yellow-500"></i>
|
48 |
<span class="text-gray-600 ml-2">4.5</span>
|
49 |
</div>
|
50 |
-
<button class="bg-yellow-500 text-gray-800 px-4 py-2 rounded-md hover:bg-yellow-400">Add to Cart</button>
|
51 |
</div>
|
52 |
</div>
|
53 |
</main>
|
@@ -57,5 +58,44 @@
|
|
57 |
</footer>
|
58 |
|
59 |
<script src="script.js"></script>
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
<!DOCTYPE html>
|
3 |
<html lang="en">
|
4 |
<head>
|
|
|
37 |
</div>
|
38 |
</div>
|
39 |
<div>
|
40 |
+
<h1 class="text-2xl font-bold" id="productName">Product Title</h1>
|
41 |
+
<p class="text-gray-600" id="productDescription">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
|
42 |
+
<p class="text-xl font-semibold" id="productPrice">$99.99</p>
|
43 |
<div class="flex items-center mb-4">
|
44 |
<i class="fas fa-star text-yellow-500"></i>
|
45 |
<i class="fas fa-star text-yellow-500"></i>
|
|
|
48 |
<i class="fas fa-star-half-alt text-yellow-500"></i>
|
49 |
<span class="text-gray-600 ml-2">4.5</span>
|
50 |
</div>
|
51 |
+
<button class="bg-yellow-500 text-gray-800 px-4 py-2 rounded-md hover:bg-yellow-400" id="addToCartButton">Add to Cart</button>
|
52 |
</div>
|
53 |
</div>
|
54 |
</main>
|
|
|
58 |
</footer>
|
59 |
|
60 |
<script src="script.js"></script>
|
61 |
+
<script src="data.js"></script>
|
62 |
+
|
63 |
+
<script>
|
64 |
+
document.addEventListener('DOMContentLoaded', function() {
|
65 |
+
const product = products[0];
|
66 |
+
trackProductView(product.id);
|
67 |
+
});
|
68 |
+
</script>
|
69 |
+
|
70 |
+
|
71 |
+
<script>
|
72 |
+
document.addEventListener('DOMContentLoaded', function() {
|
73 |
+
trackProductView('product123');
|
74 |
+
});
|
75 |
+
</script>
|
76 |
+
|
77 |
+
<script>
|
78 |
+
const productNameElement = document.getElementById('productName');
|
79 |
+
const productDescriptionElement = document.getElementById('productDescription');
|
80 |
+
const productPriceElement = document.getElementById('productPrice');
|
81 |
+
const productImageElement = document.getElementById('productImage');
|
82 |
+
const addToCartButton = document.getElementById('addToCartButton');
|
83 |
+
|
84 |
+
if (products && products.length > 0) {
|
85 |
+
const product = products[0];
|
86 |
+
|
87 |
+
if (productNameElement) productNameElement.textContent = product.name;
|
88 |
+
if (productDescriptionElement) productDescriptionElement.textContent = product.description;
|
89 |
+
if (productPriceElement) productPriceElement.textContent = '$' + product.price.toFixed(2);
|
90 |
+
if (productImageElement) productImageElement.src = product.image;
|
91 |
+
if (productImageElement) productImageElement.alt = product.name;
|
92 |
+
} else {
|
93 |
+
console.error('No products found in data.js');
|
94 |
+
}
|
95 |
+
|
96 |
+
addToCartButton.addEventListener('click', () => {
|
97 |
+
addToCart(products[0]);
|
98 |
+
});
|
99 |
+
</script>
|
100 |
+
</body>
|
101 |
+
</html>
|
script.js
CHANGED
@@ -1,4 +1,18 @@
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
document.addEventListener('DOMContentLoaded', function() {
|
3 |
// Function to get cart items from local storage
|
4 |
function getCartItems() {
|
@@ -11,229 +25,50 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
11 |
localStorage.setItem('cart', JSON.stringify(cartItems));
|
12 |
}
|
13 |
|
|
|
|
|
14 |
// Function to add an item to the cart
|
15 |
-
function addToCart(
|
16 |
-
|
17 |
-
|
|
|
18 |
|
19 |
if (existingItem) {
|
20 |
-
existingItem.quantity +=
|
21 |
} else {
|
22 |
-
|
|
|
23 |
}
|
24 |
|
25 |
setCartItems(cartItems);
|
26 |
updateCartUI();
|
27 |
}
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
const
|
33 |
-
|
34 |
-
if (itemIndex !== -1) {
|
35 |
-
cartItems[itemIndex].quantity = quantity;
|
36 |
-
if (cartItems[itemIndex].quantity <= 0) {
|
37 |
-
cartItems.splice(itemIndex, 1); // Remove item if quantity is 0 or less
|
38 |
-
}
|
39 |
-
setCartItems(cartItems);
|
40 |
-
updateCartUI();
|
41 |
-
}
|
42 |
-
}
|
43 |
-
|
44 |
-
// Function to remove an item from the cart
|
45 |
-
function removeItem(productId) {
|
46 |
-
const cartItems = getCartItems();
|
47 |
-
const itemIndex = cartItems.findIndex(item => item.productId === productId);
|
48 |
-
|
49 |
-
if (itemIndex !== -1) {
|
50 |
-
cartItems.splice(itemIndex, 1);
|
51 |
-
setCartItems(cartItems);
|
52 |
-
updateCartUI();
|
53 |
-
}
|
54 |
-
}
|
55 |
-
|
56 |
-
// Function to calculate cart totals
|
57 |
-
function calculateTotals() {
|
58 |
-
const cartItems = getCartItems();
|
59 |
-
let subtotal = 0;
|
60 |
-
|
61 |
-
// Dummy product prices (replace with actual product data)
|
62 |
-
const productPrices = {
|
63 |
-
"1": 20.00,
|
64 |
-
"2": 30.00,
|
65 |
-
"3": 40.00
|
66 |
-
};
|
67 |
-
|
68 |
-
cartItems.forEach(item => {
|
69 |
-
const productId = item.productId;
|
70 |
-
const quantity = item.quantity;
|
71 |
-
const price = productPrices[productId] || 0; // Get price from productPrices, default to 0
|
72 |
-
subtotal += price * quantity;
|
73 |
-
});
|
74 |
-
|
75 |
-
const shipping = 5.00;
|
76 |
-
const total = subtotal + shipping;
|
77 |
-
|
78 |
-
return { subtotal, shipping, total };
|
79 |
-
}
|
80 |
-
|
81 |
-
// Function to display cart items in cart.html
|
82 |
-
function displayCartItems() {
|
83 |
-
const cartItems = getCartItems();
|
84 |
-
const cartItemsContainer = document.getElementById('cart-items');
|
85 |
-
|
86 |
-
if (cartItemsContainer) {
|
87 |
-
cartItemsContainer.innerHTML = ''; // Clear existing items
|
88 |
-
|
89 |
-
cartItems.forEach(item => {
|
90 |
-
// Dummy product data (replace with actual product data)
|
91 |
-
const productId = item.productId;
|
92 |
-
const product = {
|
93 |
-
id: productId,
|
94 |
-
title: `Product ${productId}`,
|
95 |
-
price: 20.00,
|
96 |
-
image: `https://via.placeholder.com/100`
|
97 |
-
};
|
98 |
-
const cartItemDiv = document.createElement('div');
|
99 |
-
cartItemDiv.classList.add('flex', 'items-center', 'border', 'border-gray-300', 'rounded-md', 'p-4');
|
100 |
-
|
101 |
-
cartItemDiv.innerHTML = `
|
102 |
-
<img src="${product.image}" alt="Product Image" class="w-24 h-24 mr-4">
|
103 |
-
<div>
|
104 |
-
<h2 class="text-lg font-bold">${product.title}</h2>
|
105 |
-
<p class="text-gray-600">$${product.price.toFixed(2)}</p>
|
106 |
-
<div class="flex items-center mt-2">
|
107 |
-
<button class="bg-gray-200 px-2 py-1 rounded-md decrease-quantity" data-product-id="${product.id}">-</button>
|
108 |
-
<input type="number" value="${item.quantity}" class="w-16 text-center border border-gray-300 rounded-md mx-2 quantity-input" data-product-id="${product.id}">
|
109 |
-
<button class="bg-gray-200 px-2 py-1 rounded-md increase-quantity" data-product-id="${product.id}">+</button>
|
110 |
-
<button class="text-red-500 hover:text-red-700 ml-4 remove-item" data-product-id="${product.id}">Remove</button>
|
111 |
-
</div>
|
112 |
-
</div>
|
113 |
-
`;
|
114 |
-
|
115 |
-
cartItemsContainer.appendChild(cartItemDiv);
|
116 |
-
});
|
117 |
-
|
118 |
-
// Add event listeners to quantity buttons and remove buttons
|
119 |
-
const decreaseButtons = document.querySelectorAll('.decrease-quantity');
|
120 |
-
decreaseButtons.forEach(button => {
|
121 |
-
button.addEventListener('click', function() {
|
122 |
-
const productId = this.dataset.productId;
|
123 |
-
const quantityInput = this.parentNode.querySelector('.quantity-input');
|
124 |
-
let quantity = parseInt(quantityInput.value);
|
125 |
-
quantity = Math.max(1, quantity - 1); // Ensure quantity is not less than 1
|
126 |
-
quantityInput.value = quantity;
|
127 |
-
updateQuantity(productId, quantity);
|
128 |
-
});
|
129 |
-
});
|
130 |
-
|
131 |
-
const increaseButtons = document.querySelectorAll('.increase-quantity');
|
132 |
-
increaseButtons.forEach(button => {
|
133 |
-
button.addEventListener('click', function() {
|
134 |
-
const productId = this.dataset.productId;
|
135 |
-
const quantityInput = this.parentNode.querySelector('.quantity-input');
|
136 |
-
let quantity = parseInt(quantityInput.value);
|
137 |
-
quantity++;
|
138 |
-
quantityInput.value = quantity;
|
139 |
-
updateQuantity(productId, quantity);
|
140 |
-
});
|
141 |
-
});
|
142 |
-
|
143 |
-
const removeButtons = document.querySelectorAll('.remove-item');
|
144 |
-
removeButtons.forEach(button => {
|
145 |
-
button.addEventListener('click', function() {
|
146 |
-
const productId = this.dataset.productId;
|
147 |
-
removeItem(productId);
|
148 |
-
});
|
149 |
-
});
|
150 |
-
}
|
151 |
-
}
|
152 |
-
|
153 |
-
// Function to update the order summary in cart.html and checkout.html
|
154 |
-
function updateOrderSummary() {
|
155 |
-
const { subtotal, shipping, total } = calculateTotals();
|
156 |
-
|
157 |
-
const subtotalElements = document.querySelectorAll('#subtotal');
|
158 |
-
subtotalElements.forEach(element => element.textContent = `$${subtotal.toFixed(2)}`);
|
159 |
-
|
160 |
-
const shippingElements = document.querySelectorAll('#shipping');
|
161 |
-
shippingElements.forEach(element => element.textContent = `$${shipping.toFixed(2)}`);
|
162 |
-
|
163 |
-
const totalElements = document.querySelectorAll('#total');
|
164 |
-
totalElements.forEach(element => element.textContent = `$${total.toFixed(2)}`);
|
165 |
-
}
|
166 |
-
|
167 |
-
// Function to handle checkout
|
168 |
-
function handleCheckout() {
|
169 |
-
const checkoutForm = document.getElementById('checkout-form');
|
170 |
-
if (checkoutForm) {
|
171 |
-
checkoutForm.addEventListener('submit', function(event) {
|
172 |
-
event.preventDefault(); // Prevent form submission
|
173 |
-
|
174 |
-
// Simulate order processing
|
175 |
-
const orderId = Math.floor(Math.random() * 1000000);
|
176 |
-
|
177 |
-
// Store order ID in local storage
|
178 |
-
localStorage.setItem('orderId', orderId);
|
179 |
-
|
180 |
-
// Clear the cart
|
181 |
-
setCartItems([]);
|
182 |
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
// Function to display order confirmation details
|
190 |
-
function displayOrderConfirmation() {
|
191 |
-
const orderId = localStorage.getItem('orderId');
|
192 |
-
const orderIdElement = document.getElementById('order-id');
|
193 |
-
|
194 |
-
if (orderIdElement) {
|
195 |
-
orderIdElement.textContent = orderId;
|
196 |
}
|
197 |
-
}
|
198 |
-
|
199 |
-
// Initialize the cart page
|
200 |
-
if (window.location.pathname === '/cart.html') {
|
201 |
-
displayCartItems();
|
202 |
-
updateOrderSummary();
|
203 |
-
}
|
204 |
-
|
205 |
-
// Initialize the checkout page
|
206 |
-
if (window.location.pathname === '/checkout.html') {
|
207 |
-
updateOrderSummary();
|
208 |
-
handleCheckout();
|
209 |
-
}
|
210 |
-
|
211 |
-
// Initialize the confirmation page
|
212 |
-
if (window.location.pathname === '/confirmation.html') {
|
213 |
-
displayOrderConfirmation();
|
214 |
-
}
|
215 |
-
});
|
216 |
|
217 |
-
|
218 |
-
|
219 |
-
const cartItems = localStorage.getItem('cart');
|
220 |
-
return cartItems ? JSON.parse(cartItems) : [];
|
221 |
-
}
|
222 |
-
|
223 |
-
// Function to set cart items in local storage
|
224 |
-
function setCartItems(cartItems) {
|
225 |
-
localStorage.setItem('cart', JSON.stringify(cartItems));
|
226 |
}
|
227 |
|
228 |
-
|
229 |
-
|
230 |
-
const
|
231 |
-
const existingItem = cartItems.find(item => item.productId === productId);
|
232 |
|
233 |
if (existingItem) {
|
234 |
-
existingItem.quantity +=
|
235 |
} else {
|
236 |
-
|
|
|
237 |
}
|
238 |
|
239 |
setCartItems(cartItems);
|
@@ -242,8 +77,8 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
242 |
|
243 |
// Function to update item quantity in the cart
|
244 |
function updateQuantity(productId, quantity) {
|
245 |
-
|
246 |
-
const itemIndex = cartItems.findIndex(item => item.
|
247 |
|
248 |
if (itemIndex !== -1) {
|
249 |
cartItems[itemIndex].quantity = quantity;
|
@@ -253,12 +88,13 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
253 |
setCartItems(cartItems);
|
254 |
updateCartUI();
|
255 |
}
|
|
|
256 |
}
|
257 |
|
258 |
// Function to remove an item from the cart
|
259 |
function removeItem(productId) {
|
260 |
-
|
261 |
-
const itemIndex = cartItems.findIndex(item => item.
|
262 |
|
263 |
if (itemIndex !== -1) {
|
264 |
cartItems.splice(itemIndex, 1);
|
@@ -269,21 +105,11 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
269 |
|
270 |
// Function to calculate cart totals
|
271 |
function calculateTotals() {
|
272 |
-
|
273 |
let subtotal = 0;
|
274 |
|
275 |
-
// Dummy product prices (replace with actual product data)
|
276 |
-
const productPrices = {
|
277 |
-
"1": 20.00,
|
278 |
-
"2": 30.00,
|
279 |
-
"3": 40.00
|
280 |
-
};
|
281 |
-
|
282 |
cartItems.forEach(item => {
|
283 |
-
|
284 |
-
const quantity = item.quantity;
|
285 |
-
const price = productPrices[productId] || 0; // Get price from productPrices, default to 0
|
286 |
-
subtotal += price * quantity;
|
287 |
});
|
288 |
|
289 |
const shipping = 5.00;
|
@@ -294,39 +120,38 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
294 |
|
295 |
// Function to display cart items in cart.html
|
296 |
function displayCartItems() {
|
297 |
-
|
298 |
const cartItemsContainer = document.getElementById('cart-items');
|
299 |
|
300 |
if (cartItemsContainer) {
|
301 |
cartItemsContainer.innerHTML = ''; // Clear existing items
|
302 |
|
303 |
cartItems.forEach(item => {
|
304 |
-
// Dummy product data (replace with actual product data)
|
305 |
-
const productId = item.productId;
|
306 |
-
const product = {
|
307 |
-
id: productId,
|
308 |
-
title: `Product ${productId}`,
|
309 |
-
price: 20.00,
|
310 |
-
image: `https://via.placeholder.com/100`
|
311 |
-
};
|
312 |
const cartItemDiv = document.createElement('div');
|
313 |
cartItemDiv.classList.add('flex', 'items-center', 'border', 'border-gray-300', 'rounded-md', 'p-4');
|
314 |
|
315 |
cartItemDiv.innerHTML = `
|
316 |
-
<img src="${
|
317 |
<div>
|
318 |
-
<h2 class="text-lg font-bold">${
|
319 |
-
<p class="text-gray-600">$${
|
320 |
<div class="flex items-center mt-2">
|
321 |
-
<button class="bg-gray-200 px-2 py-1 rounded-md decrease-quantity" data-product-id="${
|
322 |
-
<input type="number" value="${item.quantity}" class="w-16 text-center border border-gray-300 rounded-md mx-2 quantity-input" data-product-id="${
|
323 |
-
<button class="bg-gray-200 px-2 py-1 rounded-md increase-quantity" data-product-id="${
|
324 |
-
<button class="text-red-500 hover:text-red-700 ml-4 remove-item" data-product-id="${
|
325 |
</div>
|
326 |
</div>
|
327 |
`;
|
328 |
|
329 |
cartItemsContainer.appendChild(cartItemDiv);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
330 |
});
|
331 |
|
332 |
// Add event listeners to quantity buttons and remove buttons
|
@@ -353,14 +178,6 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
353 |
updateQuantity(productId, quantity);
|
354 |
});
|
355 |
});
|
356 |
-
|
357 |
-
const removeButtons = document.querySelectorAll('.remove-item');
|
358 |
-
removeButtons.forEach(button => {
|
359 |
-
button.addEventListener('click', function() {
|
360 |
-
const productId = this.dataset.productId;
|
361 |
-
removeItem(productId);
|
362 |
-
});
|
363 |
-
});
|
364 |
}
|
365 |
}
|
366 |
|
@@ -385,14 +202,21 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
385 |
checkoutForm.addEventListener('submit', function(event) {
|
386 |
event.preventDefault(); // Prevent form submission
|
387 |
|
388 |
-
//
|
389 |
-
const
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
396 |
|
397 |
// Redirect to confirmation page
|
398 |
window.location.href = '/confirmation.html';
|
@@ -401,16 +225,42 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
401 |
}
|
402 |
|
403 |
// Function to display order confirmation details
|
404 |
-
function
|
405 |
-
const
|
406 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
407 |
|
408 |
-
|
409 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
410 |
}
|
411 |
}
|
412 |
|
413 |
-
document.addEventListener('DOMContentLoaded', function() {
|
414 |
// Initialize the cart page
|
415 |
if (window.location.pathname === '/cart.html') {
|
416 |
displayCartItems();
|
@@ -425,6 +275,13 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
425 |
|
426 |
// Initialize the confirmation page
|
427 |
if (window.location.pathname === '/confirmation.html') {
|
428 |
-
|
429 |
}
|
430 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
// Track product views
|
3 |
+
function trackProductView(productId) {
|
4 |
+
let viewCounts = JSON.parse(localStorage.getItem('viewCounts') || '{}');
|
5 |
+
viewCounts[productId] = (viewCounts[productId] || 0) + 1;
|
6 |
+
localStorage.setItem('viewCounts', JSON.stringify(viewCounts));
|
7 |
+
}
|
8 |
+
|
9 |
+
// Track add to cart events
|
10 |
+
function trackAddToCart(productId) {
|
11 |
+
let addToCartCounts = JSON.parse(localStorage.getItem('addToCartCounts') || '{}');
|
12 |
+
addToCartCounts[productId] = (addToCartCounts[productId] || 0) + 1;
|
13 |
+
localStorage.setItem('addToCartCounts', JSON.stringify(addToCartCounts));
|
14 |
+
}
|
15 |
+
|
16 |
document.addEventListener('DOMContentLoaded', function() {
|
17 |
// Function to get cart items from local storage
|
18 |
function getCartItems() {
|
|
|
25 |
localStorage.setItem('cart', JSON.stringify(cartItems));
|
26 |
}
|
27 |
|
28 |
+
|
29 |
+
|
30 |
// Function to add an item to the cart
|
31 |
+
function addToCart(product) {
|
32 |
+
trackAddToCart(product.id); // Track add to cart event
|
33 |
+
let cartItems = getCartItems();
|
34 |
+
const existingItem = cartItems.find(item => item.id === product.id);
|
35 |
|
36 |
if (existingItem) {
|
37 |
+
existingItem.quantity += 1;
|
38 |
} else {
|
39 |
+
product.quantity = 1;
|
40 |
+
cartItems.push(product);
|
41 |
}
|
42 |
|
43 |
setCartItems(cartItems);
|
44 |
updateCartUI();
|
45 |
}
|
46 |
|
47 |
+
function addToCart(product) {
|
48 |
+
trackAddToCart('product123'); // Track add to cart event
|
49 |
+
let cartItems = getCartItems();
|
50 |
+
const existingItem = cartItems.find(item => item.id === product.id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
if (existingItem) {
|
53 |
+
existingItem.quantity += 1;
|
54 |
+
} else {
|
55 |
+
product.quantity = 1;
|
56 |
+
cartItems.push(product);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
setCartItems(cartItems);
|
60 |
+
updateCartUI();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
}
|
62 |
|
63 |
+
function addToCart(product) {
|
64 |
+
let cartItems = getCartItems();
|
65 |
+
const existingItem = cartItems.find(item => item.id === product.id);
|
|
|
66 |
|
67 |
if (existingItem) {
|
68 |
+
existingItem.quantity += 1;
|
69 |
} else {
|
70 |
+
product.quantity = 1;
|
71 |
+
cartItems.push(product);
|
72 |
}
|
73 |
|
74 |
setCartItems(cartItems);
|
|
|
77 |
|
78 |
// Function to update item quantity in the cart
|
79 |
function updateQuantity(productId, quantity) {
|
80 |
+
let cartItems = getCartItems();
|
81 |
+
const itemIndex = cartItems.findIndex(item => item.id === productId);
|
82 |
|
83 |
if (itemIndex !== -1) {
|
84 |
cartItems[itemIndex].quantity = quantity;
|
|
|
88 |
setCartItems(cartItems);
|
89 |
updateCartUI();
|
90 |
}
|
91 |
+
updateCartUI(); // Call updateCartUI to update the order summary
|
92 |
}
|
93 |
|
94 |
// Function to remove an item from the cart
|
95 |
function removeItem(productId) {
|
96 |
+
let cartItems = getCartItems();
|
97 |
+
const itemIndex = cartItems.findIndex(item => item.id === productId);
|
98 |
|
99 |
if (itemIndex !== -1) {
|
100 |
cartItems.splice(itemIndex, 1);
|
|
|
105 |
|
106 |
// Function to calculate cart totals
|
107 |
function calculateTotals() {
|
108 |
+
let cartItems = getCartItems();
|
109 |
let subtotal = 0;
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
cartItems.forEach(item => {
|
112 |
+
subtotal += item.price * item.quantity;
|
|
|
|
|
|
|
113 |
});
|
114 |
|
115 |
const shipping = 5.00;
|
|
|
120 |
|
121 |
// Function to display cart items in cart.html
|
122 |
function displayCartItems() {
|
123 |
+
let cartItems = getCartItems();
|
124 |
const cartItemsContainer = document.getElementById('cart-items');
|
125 |
|
126 |
if (cartItemsContainer) {
|
127 |
cartItemsContainer.innerHTML = ''; // Clear existing items
|
128 |
|
129 |
cartItems.forEach(item => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
const cartItemDiv = document.createElement('div');
|
131 |
cartItemDiv.classList.add('flex', 'items-center', 'border', 'border-gray-300', 'rounded-md', 'p-4');
|
132 |
|
133 |
cartItemDiv.innerHTML = `
|
134 |
+
<img src="${item.image}" alt="Product Image" class="w-24 h-24 mr-4">
|
135 |
<div>
|
136 |
+
<h2 class="text-lg font-bold">${item.name}</h2>
|
137 |
+
<p class="text-gray-600">$${item.price.toFixed(2)}</p>
|
138 |
<div class="flex items-center mt-2">
|
139 |
+
<button class="bg-gray-200 px-2 py-1 rounded-md decrease-quantity" data-product-id="${item.id}">-</button>
|
140 |
+
<input type="number" value="${item.quantity}" class="w-16 text-center border border-gray-300 rounded-md mx-2 quantity-input" data-product-id="${item.id}">
|
141 |
+
<button class="bg-gray-200 px-2 py-1 rounded-md increase-quantity" data-product-id="${item.id}">+</button>
|
142 |
+
<button class="text-red-500 hover:text-red-700 ml-4 remove-item" data-product-id="${item.id}">Remove</button>
|
143 |
</div>
|
144 |
</div>
|
145 |
`;
|
146 |
|
147 |
cartItemsContainer.appendChild(cartItemDiv);
|
148 |
+
|
149 |
+
// Add event listener to remove button
|
150 |
+
const removeButton = cartItemDiv.querySelector('.remove-item');
|
151 |
+
removeButton.addEventListener('click', function() {
|
152 |
+
const productId = this.dataset.productId;
|
153 |
+
removeItem(productId);
|
154 |
+
});
|
155 |
});
|
156 |
|
157 |
// Add event listeners to quantity buttons and remove buttons
|
|
|
178 |
updateQuantity(productId, quantity);
|
179 |
});
|
180 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
}
|
182 |
}
|
183 |
|
|
|
202 |
checkoutForm.addEventListener('submit', function(event) {
|
203 |
event.preventDefault(); // Prevent form submission
|
204 |
|
205 |
+
// Get form data
|
206 |
+
const name = document.getElementById('name').value;
|
207 |
+
const address = document.getElementById('address').value;
|
208 |
+
const city = document.getElementById('city').value;
|
209 |
+
const state = document.getElementById('state').value;
|
210 |
+
const zip = document.getElementById('zip').value;
|
211 |
+
const credit_card = document.getElementById('credit_card').value;
|
212 |
+
const expiration_date = document.getElementById('expiration_date').value;
|
213 |
+
const cvv = document.getElementById('cvv').value;
|
214 |
+
|
215 |
+
// Store form data in local storage
|
216 |
+
const shippingInfo = { name, address, city, state, zip };
|
217 |
+
const paymentInfo = { credit_card, expiration_date, cvv };
|
218 |
+
localStorage.setItem('shippingInfo', JSON.stringify(shippingInfo));
|
219 |
+
localStorage.setItem('paymentInfo', JSON.stringify(paymentInfo));
|
220 |
|
221 |
// Redirect to confirmation page
|
222 |
window.location.href = '/confirmation.html';
|
|
|
225 |
}
|
226 |
|
227 |
// Function to display order confirmation details
|
228 |
+
function displayConfirmationDetails() {
|
229 |
+
const shippingInfo = localStorage.getItem('shippingInfo');
|
230 |
+
const paymentInfo = localStorage.getItem('paymentInfo');
|
231 |
+
const cartItems = getCartItems();
|
232 |
+
|
233 |
+
if (shippingInfo && paymentInfo && cartItems) {
|
234 |
+
const shipping = JSON.parse(shippingInfo);
|
235 |
+
const payment = JSON.parse(paymentInfo);
|
236 |
+
|
237 |
+
document.getElementById('shipping-name').textContent = shipping.name;
|
238 |
+
document.getElementById('shipping-address').textContent = shipping.address;
|
239 |
+
document.getElementById('shipping-city').textContent = shipping.city;
|
240 |
+
document.getElementById('shipping-state').textContent = shipping.state;
|
241 |
+
document.getElementById('shipping-zip').textContent = shipping.zip;
|
242 |
|
243 |
+
// Payment information is dummy data in confirmation.html
|
244 |
+
|
245 |
+
const orderItemsList = document.getElementById('order-items');
|
246 |
+
let orderTotal = 0;
|
247 |
+
|
248 |
+
cartItems.forEach(item => {
|
249 |
+
const listItem = document.createElement('li');
|
250 |
+
listItem.textContent = `${item.name} (${item.quantity}) - $${(item.price * item.quantity).toFixed(2)}`;
|
251 |
+
orderItemsList.appendChild(listItem);
|
252 |
+
orderTotal += item.price * item.quantity;
|
253 |
+
});
|
254 |
+
|
255 |
+
document.getElementById('order-total').textContent = `$${orderTotal.toFixed(2)}`;
|
256 |
+
|
257 |
+
// Clear cart and shipping info from local storage
|
258 |
+
localStorage.removeItem('cart');
|
259 |
+
localStorage.removeItem('shippingInfo');
|
260 |
+
localStorage.removeItem('paymentInfo');
|
261 |
}
|
262 |
}
|
263 |
|
|
|
264 |
// Initialize the cart page
|
265 |
if (window.location.pathname === '/cart.html') {
|
266 |
displayCartItems();
|
|
|
275 |
|
276 |
// Initialize the confirmation page
|
277 |
if (window.location.pathname === '/confirmation.html') {
|
278 |
+
displayConfirmationDetails();
|
279 |
}
|
280 |
});
|
281 |
+
|
282 |
+
function updateCartUI() {
|
283 |
+
if (window.location.pathname === '/cart.html') {
|
284 |
+
displayCartItems();
|
285 |
+
updateOrderSummary();
|
286 |
+
}
|
287 |
+
}
|