-
Notifications
You must be signed in to change notification settings - Fork 0
/
detail-product.php
424 lines (373 loc) · 20.3 KB
/
detail-product.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
if (!isset($_SERVER['HTTP_REFERER'])){
header("Location: http://localhost/Freshcery/index.php");
exit();
}
?>
<?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
if (isset($_GET['id'])){
$id = $_GET['id'];
$sql = "SELECT * FROM products WHERE id = :id";
$stmt = $conn->prepare($sql);
$stmt->execute([':id' => $id]);
$product = $stmt->fetch(PDO::FETCH_ASSOC);
// The price of the product
$price = $product['price'];
// The discounted price
$discount = $product['discount'];
$discounted_price = $price - ($price * $discount / 100);
// Getting the related products (belong to the same category)
$category_id = $product['category_id'];
$sql = "SELECT * FROM products WHERE category_id = :category_id AND id != :id";
$stmt = $conn->prepare($sql);
$stmt->execute([':category_id' => $category_id, ':id' => $product['id']]);
$related_products = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Validating the product
if (isset($_SESSION['user'])){
$user_id = $_SESSION['user']['id'];
$sql = "SELECT * FROM card WHERE user_id = :user_id AND product_id = :product_id";
$stmt = $conn->prepare($sql);
$stmt->execute([':user_id' => $user_id, ':product_id' => $id]);
$product_in_card = $stmt->fetch(PDO::FETCH_ASSOC);
if ($product_in_card){
$product_in_card = true;
}
else {
$product_in_card = false;
}
}
}
else {
echo "<script> window.location.href = ".URL('404.php')." </script>";
exit();
}
// Adding product to the card
if (isset($_POST['submit'])) {
$product_id = $_POST['product_id'];
$product_name = $_POST['product_name'];
$product_description = $_POST['product_description'];
$product_price = $_POST['price'];
$stock = $_POST['stock'];
$quantity = $_POST['quantity'];
$discount = $_POST['discount'];
$discounted_price = $_POST['discounted_price'];
$product_image = $_POST['image'];
$product_sku = $_POST['sku'];
$product_status = $_POST['status'];
$expiration_date = $_POST['expiration_date'];
$category_id = $_POST['category_id'];
$user_id = $_POST['user_id'];
// Inserting to the card table
try {
// SQL query to insert data into the card table
$sql = "INSERT INTO card (
name,
description,
product_id,
user_id,
category_id,
image,
price,
discount,
discounted_price,
stock,
product_quantity,
expiration_date
)
VALUES (
:name,
:description,
:product_id,
:user_id,
:category_id,
:image,
:price,
:discount,
:discounted_price,
:stock,
:product_quantity,
:expiration_date
)";
// Prepare the SQL statement
$stmt = $conn->prepare($sql);
// Bind the parameters
$stmt->bindParam(':name', $product_name);
$stmt->bindParam(':description', $product_description);
$stmt->bindParam(':product_id', $product_id);
$stmt->bindParam(':user_id', $user_id);
$stmt->bindParam(':category_id', $category_id);
$stmt->bindParam(':image', $product_image);
$stmt->bindParam(':price', $product_price);
$stmt->bindParam(':discount', $discount);
$stmt->bindParam(':discounted_price', $discounted_price);
$stmt->bindParam(':stock', $stock);
$stmt->bindParam(':product_quantity', $quantity);
$stmt->bindParam(':expiration_date', $expiration_date);
// Execute the SQL statement
$stmt->execute();
} catch (PDOException $e) {
$msg = "Error: " . $e->getMessage();
}
}
?>
<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">
<?php echo $product['name']?>
</h1>
<p class="lead">
Save time and leave the groceries to us.
</p>
</div>
</div>
</div>
<div class="product-detail">
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="slider-zoom">
<a href="<?php echo URL("assets/img/$product[image]")?>" class="cloud-zoom" rel="transparentImage: 'data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==', useWrapper: false, showTitle: false, zoomWidth:'500', zoomHeight:'500', adjustY:0, adjustX:10" id="cloudZoom">
<img
alt="Detail Zoom thumbs image"
src="<?php echo URL("assets/img/$product[image]")?>"
style="width: 100%; height:500px;"
>
</a>
</div>
</div>
<div class="col-sm-6">
<p>
<strong>Overview</strong><br>
<?php echo $product['description']?>
</p>
<div class="row">
<div class="col-sm-6">
<p>
<strong>Price</strong> (/Pack)<br>
<span class="price">USD <?=number_format($discounted_price, 2, ',', '.');?></span>
<span class="old-price">USD <?=number_format($price, 2, ',', '.');?></span>
</p>
</div>
</div>
<p class="mb-1">
<strong>Quantity</strong>
</p>
<form action="" method="POST" id="form-data">
<div class="row">
<div class="col-sm-5">
<input
class="form-control"
type="number"
min="1"
max="<?=$product['quantity']?>"
data-bts-button-down-class="btn btn-primary"
data-bts-button-up-class="btn btn-primary"
value="<?=@$_POST['quantity']?>"
name="quantity">
</div>
<div class="col-sm-6">
<span class="pt-1 d-inline-block">Pack (1000 gram)</span>
</div>
</div>
<!-- Hidden inputs to store product and user information -->
<input type="hidden" name="product_id" value="<?= htmlspecialchars($product['id']); ?>">
<input type="hidden" name="product_name" value="<?= htmlspecialchars($product['name']); ?>">
<input type="hidden" name="product_description" value="<?= htmlspecialchars($product['description']); ?>">
<input type="hidden" name="stock" value="<?= htmlspecialchars($product['quantity']); ?>">
<input type="hidden" name="price" value="<?= htmlspecialchars($product['price']); ?>">
<input type="hidden" name="discount" value="<?= htmlspecialchars($product['discount']); ?>">
<input type="hidden" name="discounted_price" value="<?= htmlspecialchars($discounted_price); ?>">
<input type="hidden" name="image" value="<?= htmlspecialchars($product['image']); ?>">
<input type="hidden" name="sku" value="<?= htmlspecialchars($product['sku']); ?>">
<input type="hidden" name="status" value="<?= htmlspecialchars($product['is_active']); ?>">
<input type="hidden" name="expiration_date" value="<?= htmlspecialchars($product['expiration_date']); ?>">
<input type="hidden" name="category_id" value="<?= htmlspecialchars($product['category_id']); ?>">
<!-- Hidden input for the user ID -->
<input type="hidden" name="user_id" value="<?= htmlspecialchars(@$_SESSION['user']['id']); ?>">
<?php if (isset($_SESSION['user'])):?>
<?php if ($product_in_card):?>
<button
class="btn-insert mt-4 btn btn-primary btn-lg"
disabled
>
<i class="fa fa-shopping-basket"></i> Product Added
</button>
<?php else:?>
<button
class="btn-insert mt-4 btn btn-primary btn-lg"
type="submit"
name="submit"
>
<i class="fa fa-shopping-basket"></i> Add to Cart
</button>
<?php endif?>
<?php else:?>
<div class="alert alert-warning mt-3" role="alert">
<a href="<?= URL('auth/login.php') ?>">Log in</a> to add this product to your cart
</div>
<?php endif;?>
</form>
</div>
</div>
</div>
</div>
<!-- A section to show the related products -->
<?php if (!empty($related_products)): ?>
<section id="related-product">
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 class="title">Related Products</h2>
<div class="product-carousel owl-carousel">
<?php foreach ($related_products as $product): ?>
<div class="item">
<div class="card card-product">
<div class="card-ribbon">
<div class="card-ribbon-container right">
<span class="ribbon ribbon-primary">SPECIAL</span>
</div>
</div>
<div class="card-badge">
<div class="card-badge-container left">
<span class="badge badge-default">
Until <?= htmlspecialchars(date('Y', strtotime($product['expiration_date']))); ?>
</span>
<span class="badge badge-primary">
<?= htmlspecialchars($product['discount']); ?>% OFF
</span>
</div>
<img
src="assets/img/<?= htmlspecialchars($product['image']); ?>"
alt="<?= htmlspecialchars($product['name']); ?>"
class="card-img-top"
style="height: 250px; width: 100%; object-fit: cover;"
>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="detail-product.php?id=<?= htmlspecialchars($product['id']); ?>"><?= htmlspecialchars($product['name']); ?></a>
</h4>
<div class="card-price">
<span class="discount">USD. <?= number_format($product['price'], 2, ',', '.'); ?></span>
<span class="reguler">USD. <?= number_format($product['price'] * (1 - $product['discount'] / 100), 2, ',', '.'); ?></span>
</div>
<a href="detail-product.php?id=<?= htmlspecialchars($product['id']); ?>" class="btn btn-block btn-primary">
Add to Cart
</a>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</section>
<?php endif; ?>
</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:[email protected]"><i class="fa fa-envelope"></i> [email protected]</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>
$(document).ready(function() {
$(".form-control").keyup(function(){
var value = $(this).val();
value = value.replace(/^(0*)/,"");
$(this).val(1);
});
$(".btn-insert").on("click",function(e){
e.preventDefault();
var form_data = $("#form-data").serialize() + '&submit=submit';
// Ajaxing
$.ajax({
type: "POST",
url: "detail-product.php?id=<?=$product_id?>", // Replace with your PHP script URL
data: form_data,
success: function(response) {
// Handle success response from server
alert("Product added to cart successfully.");
// Optionally, you could update the UI or redirect
$(".btn-insert")
.html("<i class='fa fa-shopping-basket'></i> Product Added")
.prop("disabled",true);
withRef();
},
error: function(xhr, status, error) {
// Handle error response from server
alert("An error occurred: " + error);
}
});
});
function withRef(){
$("body").load("detail-product.php?id=<?=$id?>");
}
})
</script>
</body>
</html>