-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduct.html
More file actions
177 lines (154 loc) · 6.85 KB
/
Copy pathproduct.html
File metadata and controls
177 lines (154 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Farmers Market - Products</title>
<link rel="stylesheet" href="products.css">
</head>
<body>
<header>
<h1 class="heading">AgriBaazarConnect</h1>
</header>
<div class="main-container">
<nav class="side-nav">
<ul>
<li>
<a href="login.html"><img src="src/img/login.png" alt="Login"></a>
</li>
<li>
<a href="index.html"><img src="src/img/home.png" alt="Home"></a>
</li>
<li>
<a href="product.html"><img src="src/img/products.png" alt="Products"></a>
</li>
<li>
<a href="userProfile.html"><img src="src/img/profile.png" alt="Profile"></a>
</li>
<li>
<a href="contacts.html"><img src="src/img/contact and support.png" alt="Contact"></a>
</li>
</ul>
</nav>
<main class="content">
<div class="container">
<header class="product-header">
<h2>Available Products</h2>
<button class="filter-btn" onclick="location.href='filter.html'">Filter Options</button>
<button class="filter-btn" onclick="location.href='cart.html'">My Cart</button>
</header>
<div class="product-list" id="added-product-list">
<!-- Product cards will be rendered here -->
</div>
<div class="product-list">
<!-- Product cards here -->
<article class="product-card" id="added-product-list">
<img src="src/img/Rice.jpg" alt="Rice">
<h3>Rice</h3>
<p>₹ 25/Kg</p>
<div class="quantity-section">
<label for="quantity-rice">Quantity:</label>
<input type="number" id="quantity-rice" name="quantity" min="1" value="1">
</div>
<div class="btn" onclick="addToCart('Rice', 25, 'quantity-rice')">Add To Cart</div>
</article>
<article class="product-card">
<img src="src/img/Paddy.jpg" alt="Paddy">
<h3>Paddy</h3>
<p>₹ 50/Kg</p>
<div class="quantity-section">
<label for="quantity-paddy">Quantity:</label>
<input type="number" id="quantity-paddy" name="quantity" min="1" value="1">
</div>
<div class="btn" onclick="addToCart('Paddy', 50, 'quantity-paddy')">Add To Cart</div>
</article>
<!-- Repeat the same pattern for other products -->
</div>
</div>
</main>
</div>
<footer>
<p>© 2024 AgriBaazarConnect. All rights reserved.</p>
</footer>
<script>
// Fetch products from the backend
async function fetchProducts() {
try {
const response = await fetch('http://localhost:5000/api/products/retrieve');
const products = await response.json();
// Render products
renderProducts(products);
} catch (error) {
console.error('Error fetching products:', error);
}
}
// Render products as cards
function renderProducts(products) {
const productList = document.getElementById('added-product-list');
productList.innerHTML = '';
products.forEach(product => {
const productCard = document.createElement('article');
productCard.classList.add('product-card');
productCard.innerHTML = `
<img src="src/img/${product.name}.jpg" alt="${product.name}">
<h3>${product.name}</h3>
<p>₹ ${product.price}/Kg</p>
<div class="quantity-section">
<label for="quantity-${product._id}">Quantity:</label>
<input type="number" id="quantity-${product._id}" name="quantity" min="1" value="1">
</div>
<div class="btn" onclick="addToCart('${product.name}', ${product.price}, 'quantity-${product._id}')">Add To Cart</div>
`;
productList.appendChild(productCard);
});
}
// Function to add items to the cart
function addToCart(name, price, quantityId) {
const quantity = document.getElementById(quantityId).value;
const cart = JSON.parse(localStorage.getItem('cart')) || [];
// Check if the item already exists in the cart
const existingItem = cart.find(item => item.name === name);
if (existingItem) {
existingItem.quantity = parseInt(existingItem.quantity) + parseInt(quantity);
} else {
cart.push({ name: name, price: price, quantity: parseInt(quantity) });
}
localStorage.setItem('cart', JSON.stringify(cart));
alert(`${name} has been added to your cart!`);
}
// Function to load the cart items
function loadCart() {
const cart = JSON.parse(localStorage.getItem('cart')) || [];
const cartItems = document.getElementById('cart-items');
if (cartItems) {
cartItems.innerHTML = '';
cart.forEach((item, index) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${item.name}</td>
<td>₹${item.price}</td>
<td>${item.quantity}</td>
<td>₹${(item.price * item.quantity).toFixed(2)}</td>
<td><button onclick="removeFromCart(${index})">Remove</button></td>
`;
cartItems.appendChild(row);
});
}
}
// Function to clear the cart
function clearCart() {
localStorage.removeItem('cart');
loadCart();
}
// Function to remove an item from the cart
function removeFromCart(index) {
let cart = JSON.parse(localStorage.getItem('cart')) || [];
cart.splice(index, 1);
localStorage.setItem('cart', JSON.stringify(cart));
loadCart();
}
// Load the cart when the page loads
document.addEventListener('DOMContentLoaded', loadCart);
</script>
</body>
</html>