Repository navigation
Expand file tree
/
Copy pathcart.php
More file actions
324 lines (278 loc) · 15 KB
/
Copy pathcart.php
File metadata and controls
324 lines (278 loc) · 15 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
include_once "includes/header.php";
include_once "config/config.php";
?>
<?php
if (!isset($_SESSION['user'])){
// header("Location: localhost/Freshcery");
echo "<script> window.location.href = 'localhost/Freshcery' </script>";
exit();
}
?>
<?php
// Getting the list of the products added by the user
$sql = "SELECT * FROM card WHERE user_id = :user_id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':user_id', $_SESSION['user']['id']);
$stmt->execute();
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Store the products in the session
$_SESSION['products'] = $products;
?>
<?php
if (isset($_POST['submit'])){
$tl_price = $_POST['total_price'];
// Storing the variable in the session
$_SESSION['price'] = $tl_price;
echo "<script> window.location.href = 'checkout.php';</script>";
exit(); // Stop further script execution
}
?>
<div id="page-content" class="page-content">
<div class="banner">
<div class="jumbotron jumbotron-bg text-center rounded-0" style="background-image: url('assets/img/bg-header.jpg');">
<div class="container">
<h1 class="pt-5">
Your Cart
</h1>
<p class="lead">
Save time and leave the groceries to us.
</p>
</div>
</div>
</div>
<section id="cart">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-striped table-hover mt-4">
<thead>
<tr>
<th width="10%"></th>
<th>Products</th>
<th>Price</th>
<th width="15%">Quantity</th>
<th width="15%">Update</th>
<th>Subtotal</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php if (count($products) > 0): ?>
<?php
$total_price = 0; // Initialize total price variable
foreach ($products as $product):
// Calculate updated price after discount
$updated_price = $product['price'] - ($product['price'] * $product['discount'] / 100);
// Calculate subtotal for the current product
$subtotal = $updated_price * $product['product_quantity'];
// Accumulate the subtotal into the total price
$total_price += $subtotal;
?>
<tr>
<td>
<img
src='<?= URL("assets/img/{$product['image']}") ?>'
width="60"
height="60"
alt="<?= htmlspecialchars($product['name']) ?>"
>
</td>
<td>
<?= htmlspecialchars($product['name']) ?><br>
<small>1000g</small>
</td>
<td class="pro_price">
USD <?= number_format($updated_price, 2, ',', '.') ?>
</td>
<td>
<input
class="pro_qty form-control"
type="number"
min="1"
max="<?= htmlspecialchars($product['stock']) ?>"
value="<?= htmlspecialchars($product['product_quantity']) ?>"
name="quantity"
data-product-id="<?= htmlspecialchars($product['product_id']) ?>"
>
</td>
<td>
<a href="" class="btn btn-primary update-btn">UPDATE</a>
</td>
<td>
USD <?= number_format($subtotal, 2, ',', '.') ?>
</td>
<td>
<a data-product-id="<?=$product['product_id']?>" class="text-danger delete-btn cursor-pointer"><i class="fa fa-times"></i></a>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<?php $total_price = 0;?>
<tr>
<td colspan="7">
<div class="alert alert-warning alert-dismissible fade show" role="alert">
Your cart is empty. Go to the <a href="<?= URL('shop.php') ?>">shopping</a> page.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<div class="col">
<a href="<?php echo URL('shop.php')?>" class="btn btn-default">Continue Shopping</a>
</div>
<div class="col text-right">
<form action="cart.php" method="POST">
<div class="clearfix"></div>
<h6 class="mt-3 total_price">Total: USD <?= number_format($total_price, 2, ',', '.'); ?></h6>
<input type="hidden" name="total_price" value="<?= htmlspecialchars($total_price); ?>">
<?php if (count($products) > 0):?>
<button
type="submit" name="submit"
class="btn btn-lg btn-primary"
>
Checkout
<i class="fa fa-long-arrow-right"></i>
</button>
<?php endif;?>
</form>
</div>
</div>
</div>
</section>
</div>
<footer>
<div class="container">
<div class="row">
<div class="col-md-3">
<h5>About</h5>
<p>Nisi esse dolor irure dolor eiusmod ex deserunt proident cillum eu qui enim occaecat sunt aliqua anim eiusmod qui ut voluptate.</p>
</div>
<div class="col-md-3">
<h5>Links</h5>
<ul>
<li><a href="<?php echo URL('about.php'); ?>">About</a></li>
<li><a href="<?php echo URL('contact.php'); ?>">Contact Us</a></li>
<li><a href="<?php echo URL('faq.php'); ?>">FAQ</a></li>
<li><a href="javascript:void(0)">How it Works</a></li>
<li><a href="<?php echo URL('terms.php'); ?>">Terms</a></li>
<li><a href="<?php echo URL('privacy.php'); ?>">Privacy Policy</a></li>
</ul>
</div>
<div class="col-md-3">
<h5>Contact</h5>
<ul>
<li><a href="tel:+620892738334"><i class="fa fa-phone"></i> 08272367238</a></li>
<li><a href="mailto:hello@domain.com"><i class="fa fa-envelope"></i> hello@domain.com</a></li>
</ul>
<h5>Follow Us</h5>
<ul class="social">
<li><a href="javascript:void(0)" target="_blank"><i class="fab fa-facebook-f"></i></a></li>
<li><a href="javascript:void(0)" target="_blank"><i class="fab fa-instagram"></i></a></li>
<li><a href="javascript:void(0)" target="_blank"><i class="fab fa-youtube"></i></a></li>
</ul>
</div>
<div class="col-md-3">
<h5>Get Our App</h5>
<ul class="mb-0">
<li class="download-app"><a href="#"><img src="<?php echo URL('assets/img/playstore.png'); ?>"></a></li>
<li style="height: 200px">
<div class="mockup">
<img src="<?php echo URL('assets/img/mockup.png'); ?>">
</div>
</li>
</ul>
</div>
</div>
</div>
<p class="copyright">© 2018 Freshcery | Groceries Organic Store. All rights reserved.</p>
</footer>
<script type="text/javascript" src="<?php echo URL('assets/js/jquery.js'); ?>"></script>
<script type="text/javascript" src="<?php echo URL('assets/js/jquery-migrate.js'); ?>"></script>
<script type="text/javascript" src="<?php echo URL('assets/packages/bootstrap/libraries/popper.js'); ?>"></script>
<script type="text/javascript" src="<?php echo URL('assets/packages/bootstrap/bootstrap.js'); ?>"></script>
<script type="text/javascript" src="<?php echo URL('assets/packages/o2system-ui/o2system-ui.js'); ?>"></script>
<script type="text/javascript" src="<?php echo URL('assets/packages/owl-carousel/owl-carousel.js'); ?>"></script>
<script type="text/javascript" src="<?php echo URL('assets/packages/cloudzoom/cloudzoom.js'); ?>"></script>
<script type="text/javascript" src="<?php echo URL('assets/packages/thumbelina/thumbelina.js'); ?>"></script>
<script type="text/javascript" src="<?php echo URL('assets/packages/bootstrap-touchspin/bootstrap-touchspin.js'); ?>"></script>
<script type="text/javascript" src="<?php echo URL('assets/js/theme.js'); ?>"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$(".form-control").keyup(function(){
var value = $(this).val();
value = value.replace(/^(0*)/,"");
$(this).val(1);
});
$('.update-btn').click(function(e){
e.preventDefault();
// Get the row that contains the clicked button
var $row = $(this).closest('tr');
// Get the product ID and quantity from the input field
var product_id = $row.find('input[name="quantity"]').data('product-id');
var quantity = $row.find('input[name="quantity"]').val();
// Send the AJAX request to update the quantity
$.ajax({
url: 'update_cart.php',
method: 'POST',
data: {
product_id: product_id,
quantity: quantity
},
success: function(response) {
// Parse the response
var data = JSON.parse(response);
// Update the subtotal and total price
$row.find('.pro_price').text(data.updated_price);
$row.find('.pro_qty').val(data.quantity);
$row.find('td:nth-child(6)').text('USD ' + data.subtotal);
$('.total_price').text('USD ' + data.total_price);
// Optionally display a success message
alert('Cart updated successfully');
}
});
});
$('.delete-btn').click(function(e){
e.preventDefault();
// Get the row that contains the clicked button
var $row = $(this).closest('tr');
// Get the product ID from the delete button
var product_id = $(this).data('product-id');
// Send the AJAX request to delete the product
$.ajax({
url: 'delete_from_cart.php',
method: 'POST',
data: {
product_id: product_id
},
success: function(response) {
// Parse the response
var data = JSON.parse(response);
if (data.success) {
// Remove the row from the table
$row.remove();
// Update the total price
$('.total_price').text('USD ' + data.total_price);
// Optionally display a success message
alert('Product removed successfully');
// Reloading the page
reload();
} else {
alert('Error removing product from cart');
}
}
});
});
function reload(){
$("body").load("cart.php");
}
})
</script>
</body>
</html>