forked from ilyasbelfar/ShopMeex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
419 lines (385 loc) · 21 KB
/
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
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
<?php
include 'includes/session.php';
if(!isset($_SESSION['id'])){
header("location: customer/register.php");
exit();
}
$subtotal = $subTotals = $discount = 0;
$messageSub = '';
$messageCoupon = '';
$stmt = $db->prepare("SELECT *, cart.quantity AS cq , cart.id As cartid FROM cart LEFT JOIN products ON products.id=cart.product_id WHERE user_id=:user_id");
$stmt->execute(['user_id'=>$user['id']]);
foreach($stmt as $row) {
$subtotal += $row['price']*$row['cq'];
}
if (isset($_SESSION['cart']['used_coupons'])) {
$usedCoupon = '';
for ($i=0; $i < count($_SESSION['cart']['used_coupons']); $i++) {
$usedCoupon = $_SESSION['cart']['used_coupons'][$i];
$sql = $db->prepare('SELECT * FROM coupons LEFT JOIN (SELECT product_id AS prodIdent, user_id, quantity FROM cart) AS cart ON coupons.product_id = cart.prodIdent LEFT JOIN (SELECT id AS product_ident, price FROM products) AS products ON coupons.product_id = products.product_ident WHERE products.product_ident = cart.prodIdent AND user_id=:userID AND coupon_code=:couponCode');
if ($sql->execute(['userID'=>$_SESSION['id'], 'couponCode'=>$usedCoupon])) {
try {
$results = $sql->fetchAll(PDO::FETCH_ASSOC);
if ($results) {
foreach($results as $key) {
$discount += (($key['price'] * $key['quantity'])*$key['discount'])/100;
$discount = number_format((float)$discount, 2, '.', '');
}
$_SESSION['cart']['total'] -= $discount;
} else {
$discount = 0;
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
}
} else {
$_SESSION['cart']['used_coupons'] = array();
}
if (isset($_POST['apply_coupon']) && !empty($_POST['Coupon'])) {
$sql_2 = $db->prepare("SELECT coupon_code, usage_limit, time_used FROM coupons WHERE coupon_code=?");
$sql_2->bindValue(1, $_POST['Coupon']);
$sql_2->execute();
if ($result=$sql_2->fetch(PDO::FETCH_ASSOC)) {
if ($result['usage_limit'] != $result['time_used']) {
$cpt = 0;
for ($i=0; $i < count($_SESSION['cart']['used_coupons']); $i++) {
if ($_SESSION['cart']['used_coupons'][$i] == $_POST['Coupon']) {
$cpt += 1;
} else {
$cpt += 0;
}
}
if ($cpt == 0) {
$stmt = $db->prepare('SELECT * FROM coupons LEFT JOIN (SELECT product_id AS prodIdent, user_id, quantity FROM cart) AS cart ON coupons.product_id = cart.prodIdent LEFT JOIN (SELECT id AS product_ident, price FROM products) AS products ON coupons.product_id = products.product_ident WHERE products.product_ident = cart.prodIdent AND user_id=:userID AND coupon_code=:couponCode');
if ($stmt->execute(['userID'=>$_SESSION['id'], 'couponCode'=>$_POST['Coupon']])) {
try {
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($results) {
foreach($results as $k) {
$discount += (($k['price'] * $k['quantity'])*$k['discount'])/100;
$discount = number_format((float)$discount, 2, '.', '');
}
$_SESSION['cart']['total'] -= $discount;
array_push($_SESSION['cart']['used_coupons'], $_POST['Coupon']);
$messageCoupon = '<div class="message" role="alert">Coupon has been applied!</div>';
$sql_1 = $db->prepare("UPDATE coupons SET time_used = time_used + 1 WHERE coupon_code=?");
$sql_1->bindValue(1, $_POST['Coupon']);
$sql_1->execute();
} else {
$messageCoupon = '<div class="errors" role="alert">Either coupon code you\'ve entered is invalid or it is for another product!</div>';
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
} else {
$messageCoupon = '<div class="errors" role="alert">Oops! Coupon code you\'ve entered is already used before by your side!</div>';
}
} else {
$messageCoupon = '<div class="errors" role="alert">Oops! Coupon Usage Limit Has Been Reached!</div>';
}
} else {
$messageCoupon = '<div class="errors" role="alert">Either coupon code you\'ve entered is invalid or it is for another product!</div>';
}
}
if (isset($_POST['update_cart'])) {
foreach ($_POST as $k => $v) {
if (strpos($k, 'qty_') !== false) {
$id = str_replace('qty_', '', $k);
$id = (int)$id;
$quantity = (int)$v;
// Always do checks and validation
if ($quantity > 0) {
$stmt = $db->prepare("UPDATE cart SET quantity=? WHERE id=?");
$stmt->bindValue(1,$quantity, PDO::PARAM_INT);
$stmt->bindValue(2,$id, PDO::PARAM_INT);
if($stmt->execute()) {
$messageSub ='<div class="message" role="alert">Products quantities has been updated.</div>';
} else {
$messageSub = '<div class="errors" role="alert">Oops! Something Went Wrong, Please Try Again.</div>';
}
}
if (isset($_SESSION['cart']['used_coupons'])) {
$usedCoupon = '';
for ($i=0; $i < count($_SESSION['cart']['used_coupons']); $i++) {
$usedCoupon = $_SESSION['cart']['used_coupons'][$i];
$sql = $db->prepare('SELECT * FROM coupons LEFT JOIN (SELECT product_id AS prodIdent, user_id, quantity FROM cart) AS cart ON coupons.product_id = cart.prodIdent LEFT JOIN (SELECT id AS product_ident, price FROM products) AS products ON coupons.product_id = products.product_ident WHERE products.product_ident = cart.prodIdent AND user_id=:userID AND coupon_code=:couponCode AND product_id =:identifier');
if ($sql->execute(['userID'=>$_SESSION['id'], 'couponCode'=>$usedCoupon, 'identifier'=>$id])) {
try {
$results = $sql->fetchAll(PDO::FETCH_ASSOC);
if ($results) {
foreach($results as $key) {
$discount += (($key['price'] * $key['quantity'])*$key['discount'])/100;
$discount = number_format((float)$discount, 2, '.', '');
}
$_SESSION['cart']['total'] -= $discount;
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
}
} else {
$_SESSION['cart']['used_coupons'] = array();
}
}
}
echo "<meta http-equiv='refresh' content='0'>";
}
// Delete Item From Shopping Cart Not Working
if(isset($_GET['remove_item'])) {
$ident = $_GET['remove_item'];
if(isset($_SESSION['id'])) {
try {
$stmt = $db->prepare("DELETE FROM cart WHERE id=:id");
$stmt->execute(['id'=>$ident]);
foreach($_SESSION['cart'] as $row) {
if($row['productid'] == $ident){
unset($_SESSION['cart'][$key]);
echo "Delete Product Successfully.";
}
}
unset($_SESSION['cart']['used_coupons']);
echo "Delete Product Successfully.";
}
catch(PDOException $e){
echo "Wrong Message When Delete Product.";
}
}
else {
foreach($_SESSION['cart'] as $row) {
if($row['productid'] == $ident){
unset($_SESSION['cart'][$key]);
echo "Delete Product Successfully.";
}
}
unset($_SESSION['cart']['used_coupons']);
}
header('Location: cart.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Cart | ShopMeex Online Store</title>
<?php include'includes/header.php' ?>
<!-- End Header -->
<!-- Start Cart -->
<div class="breadcrumbs">
<div class="container">
<div class="wrapper">
<div class="col-35">
<div class="bread-inner">
<ul class="bread-list">
<li><a href="index.php">Home<i class="ti-arrow-right"></i></a></li>
<li class="active"><a href="cart.php">Cart</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<section class="shopping">
<div class="container">
<div class="wrapper">
<div class="notices">
<?php echo $messageSub;?>
<?php echo $messageCoupon;?>
</div>
<div class="col-9">
<div class="nothing-found">
<span>Your cart is</span>
<div>Currently empty</div>
</div>
<p class="return-to-shop">
<a class="button wc-backward" href="product.php?product=large-dell-inspiron">Return to shop</a>
</p>
</div>
<form class="cart-form" method="POST">
<div class="col-7">
<table class="shopping-cart">
<thead>
<tr class="main-heading">
<th>PRODUCT</th>
<th>NAME</th>
<th class="text-center">UNIT PRICE</th>
<th class="text-center">QUANTITY</th>
<th class="text-center">TOTAL</th>
<th class="text-center" style="padding-left: 1.5rem;padding-right: 1.5rem"><i class="ti-trash remove-icon"></i></th>
</tr>
</thead>
<tbody>
<?php if(isset($_SESSION['id'])) {
try {
$stmt = $db->prepare("SELECT *, cart.quantity AS cq , cart.id As cartid FROM cart LEFT JOIN products ON products.id=cart.product_id WHERE user_id=:user_id");
$stmt->execute(['user_id'=>$user['id']]);
foreach($stmt as $row) { ?>
<tr>
<td class="image" data-title="Product Picture"><img src="images/items/<?php echo $row['photo'];?>" alt="Product's Picture"></td>
<td class="product-des" data-title="Description">
<p class="product-name"><a href="product.php?product=<?php echo $row['slug'];?>"><?php echo $row['name'];?></a></p>
<p class="product-des"><?php echo $row['description'];?></p>
</td>
<td class="prix" data-title="Price"><span>$<span class="unit-price"><?php echo $row['price'];?></span></span>
</td>
<td class="qty" data-title="Quantity">
<!-- Input Order -->
<div class="input-group mb-3 mb-4">
<div class="input-group-prepend">
<button class="btn btn-light" type="button" id="button-plus"> + </button>
</div>
<input id="quantity" name="qty_<?php echo $row['cartid'];?>" type="text" class="form-control" value="<?php echo $row['cq'];?>">
<div class="input-group-append">
<button class="btn btn-light" type="button" id="button-minus"> − </button>
</div>
</div>
<!--/ End Input Order -->
</td>
<td class="total-amount" data-title="Total"><span>$<span class="amount"><?php echo $row['price']*$row['cq'];?></span></span>
</td>
<td class="action" data-title="Remove"><a
class="delete_cart" rel="<?php echo $row['cartid']; ?>"
href="#"
id="remove-product"><i class="ti-trash remove-icon"></i></a>
</td>
</tr>
<?php $subTotals += $row['price']*$row['cq'];
$subtotal += $row['price']*$row['cq'];
}
} catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
}?>
</tbody>
</table>
</div>
<div class="col-7">
<div class="total-amount">
<div class="row">
<div class="col-6 big">
<div class="left">
<div class="coupon">
<input name="Coupon" placeholder="Enter Your Coupon">
<button type="submit" class="btn" name="apply_coupon" value="Apply">Apply</button>
</div>
</div>
<div class="left">
<div class="coupon">
<button type="submit" class="btn" name="update_cart" value="Update Cart" disabled>Update Cart</button>
</div>
</div>
</div>
<div class="col-6">
<div class="right">
<ul>
<li>Cart Subtotal<span id="subtot">$<?php echo number_format((float)$subTotals, 2, '.', ''); ?></span></li>
<li>Shipping<span>Free</span></li>
<li>Discount<span>$<?php echo $discount; ?></span></li>
<li class="last">You Pay<span>$<?php echo number_format((float)($subTotals - $discount), 2, '.', ''); ?></span></li>
</ul>
<?php $_SESSION['cart']['total'] = $subTotals - $discount;?>
<div class="button5">
<a href="checkout.php" class="btn">Proceed to checkout</a>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</section>
<!-- End Cart -->
<!-- Start NewsLetter -->
<div class="newsletter-area">
<div class="container">
<div class="wrapper">
<div class="newsletter-image">
<img src="images/banners/slide4.jpg">
</div>
<div class="newsletter-form">
<label for="mailing">Subscribe our newsletter</label>
<input type="email" id="mailing" name="email" placeholder="Enter Your E-mail...">
<button type="submit" name="subscribe">
<i class="fa fa-envelope"></i>
<span>Subscribe</span>
</button>
</div>
</div>
</div>
</div>
<!-- End NewsLetter -->
<!-- Start Footer -->
<footer id="main-footer">
<div class="container">
<section class="footer-sections">
<div class="wrapper">
<div class="about-shopmx">
<div class="row">
<div class="logo">
<a href="index.html">
<img src="images/Logo-header.png">
</a>
</div>
<p class="text">Praesent dapibus, neque id cursus ucibus, tortor neque egestas augue, magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus.</p>
<p class="call">Got Question? Call us 24/7<span><a href="tel:123456789">+0123 456 789</a></span></p>
</div>
</div>
<aside>
<h3>Help</h3>
<ul>
<li><a href="#"><i class="fa fa-envelope"></i>Contact Us</a></li>
<li><a href="#"><i class="fas fa-money-check"></i>Money Refund</a></li>
<li><a href="#"><i class="fas fa-info-circle"></i>Order Status</a></li>
<li><a href="#"><i class="fa fa-shopping-bag"></i>Shipping Info</a></li>
<li><a href="#"><i class="fa fa-share"></i>Open Dispute</a></li>
</ul>
</aside>
<aside>
<h3>Account</h3>
<ul>
<li><a href="#"><i class="fa fa-user-circle"></i>User Login</a></li>
<li><a href="#"><i class="fa fa-user-plus"></i>User Register</a></li>
<li><a href="#"><i class="fa fa-cog"></i>Account Settings</a></li>
<li><a href="#"><i class="fa fa-cart-plus"></i>My Orders</a></li>
</ul>
</aside>
<aside>
<h3>Social Media</h3>
<ul class="social-media">
<li>
<li><a href="#"><i class="fab fa-facebook"></i>Facebook</a></li>
<li><a href="#"><i class="fab fa-twitter"></i>Twitter</a></li>
<li><a href="#"><i class="fab fa-instagram"></i>Instagram</a></li>
<li><a href="#"><i class="fa fa-phone"></i>N° Phone</a></li>
</ul>
</aside>
</div>
</section>
</div>
<div class="copyrights-payments">
<div class="container">
<p class="rights">© <a href="#"><span class="span-1">Shop</span><span class="span-2">Meex</span></a>, All Rights Reserved. ®</p>
<div class="payments-bg">
<img src="images/payment.png">
</div>
</div>
</div>
</footer>
<!-- End Footer -->
<div class="gototop">
<a href="#" class="gotop"><i class="fa fa-arrow-up"></i></a>
</div>
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/owl.carousel.min.js"></script>
<script src="js/TweenMax.min.js"></script>
<script src="js/jquery.nice-select.js"></script>
<script src="js/jquery.countdown.min.js"></script>
<script src="js/custom.js"></script>
<script src="https://kit.fontawesome.com/5d49be4ed0.js" crossorigin="anonymous"></script>
</body>
<?php include 'includes/script.php'; ?>
</html>