-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_cart.php
274 lines (233 loc) · 11 KB
/
user_cart.php
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
session_start();
include 'php/config.php';
if (isset($_GET['ID'])) {
$id = $_GET['ID'];
// Delete the item from the cart based on cart_id
$delete_incart = "DELETE FROM cart WHERE cart_id='$id'";
$data = mysqli_query($con, $delete_incart);
// Redirect to the user cart after deletion
if ($data) {
header("location:user_cart.php");
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Khloris</title>
<link rel="stylesheet" href="ui khloris/carthay.css" />
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css" />
</head>
<body>
<?php include 'newheader.php'; ?>
<section class="cart-section">
<div class="cart-container">
<h3>My <span>Cart</span></h3>
<?php
$sql = "SELECT * FROM cart WHERE user_id = " . $_SESSION['ID'];
$result = $con->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo '<div class="cart-orders-item" data-name="' . $row['product_name'] . '" data-price="' . $row['product_price'] . '" data-image="' . $row['image_url'] . '" data-id="' . $row['product_id'] . '">';
echo ' <input type="checkbox" class="item-checkbox" id="item' . $row['product_id'] . '" data-price="' . $row['product_price'] . '" />';
echo ' <img src="' . $row['image_url'] . '" alt="' . $row['product_name'] . '" />';
echo ' <label for="item' . $row['product_id'] . '">' . $row['product_name'] . '</label>';
echo ' <div class="quantity-controls">';
echo ' <button class="minus-btn">-</button>';
echo ' <input type="number" class="quantity" value="' . $row['quantity'] . '" min="1" />';
echo ' <button class="plus-btn">+</button>';
echo ' </div>';
echo ' <div class="price-container">';
echo ' <span class="original-price">P ' . number_format($row['product_price'], 2) . '</span>';
echo ' <span class="updated-price">P ' . number_format($row['product_price'] * $row['quantity'], 2) . '</span>';
echo ' </div>';
echo ' <span class="remove-btn">';
echo ' <a href="user_cart.php?ID=' . $row['cart_id'] . '" onclick="return confirm(\'Are you sure you want to remove this item from your cart?\')">';
echo ' <i class="fa-solid fa-trash-can"></i>';
echo ' </a>';
echo ' </span>';
echo '</div>';
}
} else {
echo "No items in the cart.";
}
?>
<div class="total-container">
<span class="total">Total: <span id="total">P 0.00</span></span>
<p>Note: Shipping is currently available only within Pandi,Bulacan area. We are working to expand our delivery services soon.</p>
<button class="checkout-btn" id="checkoutBtn" disabled>Checkout</button>
</div>
</section>
<footer class="footer">
<div class="ftrcontainer">
<div class="ftrrow">
<div class="footer-col">
<h4 class="logofooter">Khloris<span>.</span></h4>
<ul class="ftrul">
<p>Follow us in Social Media!</p>
<div class="social-links">
<a href="#" class="fa-brands fa-square-facebook"></a>
<a href="#" class="fa-brands fa-square-instagram"></a>
</div>
</ul>
</div>
<div class="footer-col">
<h4>Information</h4>
<ul class="ftrul">
<p>Flower Shop in Pandi,Bulacan <br />Delivery only in Pandi</p>
<li><a href="#">Privacy policy</a></li>
<li><a href="#">Payment Instruction</a></li>
<li><a href="#">Terms and conditions</a></li>
</ul>
</div>
<div class="footer-col">
<h4>Contact us</h4>
<ul class="ftrul">
<p><span>Address:</span>Pandi, Bulacan</p>
<p><span>Mobile:</span>(+63) 0903-2301484/09299750331</p>
<p><span>Telephone:</span>(632) 8881-4173</p>
<p><span>Email:</span>[email protected]</p>
</ul>
</div>
<div class="footer-col">
<h4>Contact Services</h4>
<ul class="ftrul">
<li><a href="#">Contact us</a></li>
<li><a href="#">My account</a></li>
<li><a href="#">Order History</a></li>
</ul>
</div>
</div>
</div>
</footer>
<script>
function confirmLogout() {
return confirm("Are you sure you want to log out?");
}
</script>
<script>
document.addEventListener("DOMContentLoaded", () => {
const checkboxes = document.querySelectorAll(".item-checkbox");
const totalElement = document.getElementById("total");
const quantityInputs = document.querySelectorAll(".quantity");
const minusButtons = document.querySelectorAll(".minus-btn");
const plusButtons = document.querySelectorAll(".plus-btn");
const updatedPrices = document.querySelectorAll(".updated-price");
const checkoutBtn = document.getElementById("checkoutBtn");
function updateTotal() {
let hasCheckedItems = false;
let total = 0;
checkboxes.forEach((checkbox, index) => {
if (checkbox.checked) {
const price = parseFloat(checkbox.getAttribute("data-price"));
const quantity = parseInt(quantityInputs[index].value);
total += price * quantity;
hasCheckedItems = true;
}
});
totalElement.textContent = `P${total.toFixed(2)}`;
checkoutBtn.disabled = !hasCheckedItems;
}
function updateItemPrice(index) {
const price = parseFloat(checkboxes[index].getAttribute("data-price"));
const quantity = parseInt(quantityInputs[index].value);
const updatedPrice = price * quantity;
updatedPrices[index].textContent = `P${updatedPrice.toFixed(2)}`;
}
checkboxes.forEach((checkbox, index) => {
checkbox.addEventListener("change", updateTotal);
});
quantityInputs.forEach((input, index) => {
input.addEventListener("input", () => {
updateItemPrice(index);
updateTotal();
});
});
minusButtons.forEach((button, index) => {
button.addEventListener("click", () => {
const quantityInput = quantityInputs[index];
if (quantityInput.value > 1) {
quantityInput.value--;
updateItemPrice(index);
updateTotal();
}
});
});
plusButtons.forEach((button, index) => {
button.addEventListener("click", () => {
const quantityInput = quantityInputs[index];
quantityInput.value++;
updateItemPrice(index);
updateTotal();
});
});
updateTotal();
checkoutBtn.addEventListener("click", () => {
const cartItems = document.querySelectorAll(".cart-orders-item");
const orderSummary = [];
cartItems.forEach(item => {
if (item.querySelector(".item-checkbox").checked) {
const name = item.getAttribute("data-name");
const price = item.getAttribute("data-price");
const image = item.getAttribute("data-image");
const quantity = item.querySelector(".quantity").value;
orderSummary.push({ name, price, image, quantity });
}
});
localStorage.setItem("orderSummary", JSON.stringify(orderSummary));
window.location.href = "checkout.php";
});
});
</script>
<script>
function saveCart() {
$.ajax({
url: 'customize_bea/save_cart.php',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(Object.values(cart)),
success: function(response) {
console.log('Cart saved:', response);
},
error: function(xhr, status, error) {
console.error('Error saving cart:', error);
}
});
}
function updateItem(productId, productName, productPrice, quantity, imageUrl) {
if (quantity <= 0) {
delete cart[productId];
} else {
cart[productId] = {
product_id: productId,
product_name: productName,
product_price: productPrice,
quantity: quantity,
image_url: imageUrl
};
}
saveCart();
}
$(document).ready(function() {
$('.quantity').change(function() {
const productId = $(this).closest('.cart-orders-item').data('id');
const productName = $(this).closest('.cart-orders-item').data('name');
const productPrice = $(this).closest('.cart-orders-item').data('price');
const quantity = parseInt($(this).val());
const imageUrl = $(this).closest('.cart-orders-item').data('image');
updateItem(productId, productName, productPrice, quantity, imageUrl);
});
// Example of removing an item
$('.remove-btn').click(function() {
const productId = $(this).data('product-id');
updateItem(productId, '', 0, 0, '');
});
});
</script>
</body>
</html>