-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
305 lines (227 loc) · 8.47 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
<!DOCTYPE html>
<html lang="en">
<?php
include('header.php');
?>
<?php
include('navigation.php');
?>
<?php
//======================================== Cart Functions ============================================
function display_cart()
{
$total = 0;
$item_quantity = 0;
//operation on session variable (associative array) => $name = session name, $value = session data
foreach ($_SESSION as $name => $value) {
if ($value > 0) { //preventing multiple data to be shown
//grab the session that we need
//start at 0th character till 8th
if (substr($name, 0, 8) == "product_") {
//pull product_id(session id) out of session
$length = strlen($name) - 8;
$id = substr($name, 8, $length);
//$value contains the quantity of the item added i.e. (1,2,3,..)
$stmt = query("SELECT * FROM food_items WHERE food_id={$id}");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
//find sub-total of items
$sub = $row['food_price'] * $value;
//find total no.of items
$item_quantity += $value;
echo "
<tr class='text-center'>
<td class='product-remove'>
<a href='cart.php?delete={$row['food_id']}'><span class='icon-close'></span></a>
</td>
<td class='image-prod'>
<div class='img'><img class='img-responsive' width='90' src='admin/{$row['food_image']}' alt='320x150'></div>
</td>
<td class='product-name'>
<h3>{$row['food_name']}</h3>
<p>{$row['short_description']}</p>
</td>
<td class='price'>৳ {$row['food_price']}</td>
<td class='price'>{$value}</td>
<td class='total'>৳ {$sub}</td>
<td class='product-remove'>
<a href='cart.php?add={$row['food_id']}'><span class='icon-add'></span></a>
<a href='cart.php?remove={$row['food_id']}'><span class='icon-minus'></span></a>
</td>
</tr>
";
}
//grab the sub-totals to calculate total amount
$total += $sub;
$_SESSION['item_total'] = $total;
//grab the total no.of items
$_SESSION['item_quantity'] = $item_quantity;
}
}
}
}
function cart_system()
{
//Detect product_id from url
if (isset($_GET['add'])) {
//--------ADD ITEM TO CART----------
$stmt = query("SELECT * FROM food_items WHERE food_id={$_GET['add']}");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
//if we have item available, then add it to cart
if ($row['quantity'] != $_SESSION['product_' . $_GET['add']]) { //create dynamic session - using id
//join product_id to session, and
//increment item
$_SESSION['product_' . $_GET['add']] += 1;
redirect("cart.php?success=item_add");
} else {
//if we dont have the item available (quantity=0), prevent adding to cart
redirect("cart.php?error=not_available");
}
}
}
if (isset($_GET['remove'])) {
//--------Remove ITEM FROM CART----------
//if nothing is in cart, remove won't work
if ($_SESSION['product_' . $_GET['remove']] < 1) {
redirect("cart.php?error=cart_empty");
} else {
//item is in cart, remove item
$_SESSION['product_' . $_GET['remove']] -= 1;
if ($_SESSION['product_' . $_GET['remove']] < 1) {
//when cart becomes empty - unset cart values (sessions)
unset($_SESSION['item_total']);
unset($_SESSION['item_quantity']);
}
redirect("cart.php?success=item_removed");
}
}
if (isset($_GET['delete'])) {
//--------DELETE ITEM FROM CART----------
$_SESSION['product_' . $_GET['delete']] = '0'; //'0' has to be string
//unset cart values (sessions) - when cart becomes empty
unset($_SESSION['item_total']);
unset($_SESSION['item_quantity']);
redirect("cart.php?success=item_deleted");
}
display_cart();
}
//fixed variables
$Total_amount = 0;
$Discount = 0;
$Delivery_charge = 50;
$Vat = 100;
?>
<section class="ftco-section ftco-cart">
<div class="container">
<div class="row">
<div class="col-md-12 ftco-animate">
<div class="cart-list">
<table class="table">
<thead class="thead-primary">
<tr class="text-center">
<th> </th>
<th> </th>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th> </th>
<th> </th>
</tr>
</thead>
<tbody>
<?php cart_system(); ?>
</tbody>
</table>
</div>
</div>
</div>
<div class="row justify-content-end">
<div class="mt-5 col col-lg-3 col-md-6 cart-wrap ftco-animate">
<div class="mb-3 cart-total">
<h3>Cart Totals</h3>
<p class="d-flex">
<span>Subtotal</span>
<span>৳
<?php
//get total amount of cart --- stored in a session
if (isset($_SESSION['item_total'])) {
$item_total = $_SESSION['item_total'];
} else {
$item_total = $_SESSION['item_total'] = "0";
}
echo $item_total;
?></span>
</p>
<p class="d-flex">
<span>Quantity Bought</span>
<span>
<?php
//get total number of items in cart --- stored in a session
if (isset($_SESSION['item_quantity'])) {
$item_quantity = $_SESSION['item_quantity'];
} else {
$item_quantity = $_SESSION['item_quantity'] = "0";
}
echo $item_quantity;
?></span>
</p>
<p class="d-flex">
<span>Discount</span>
<span>৳<?php echo " " . $Discount; ?></span>
</p>
<hr>
<p class="d-flex total-price">
<span>Total</span>
<span>৳
<?php
$Total_amount = $item_total + $Delivery_charge - $Discount + $Vat;
echo $item_total;
$_SESSION["total_amount"] = $Total_amount;
?></span>
</p>
</div>
<?php if (is_logged_in() == true) : ?>
<p class="text-center"><a href="checkout.php" class="px-4 py-3 btn btn-primary">Proceed to Checkout</a></p>
<?php endif; ?>
<?php if (is_logged_in() == false) : ?>
<p class="text-center"><a href="#" class="px-4 py-3 btn btn-danger">Login to Add Items to Cart!</a></p>
<?php endif; ?>
</div>
</div>
</div>
</section>
<?php
include('footer.php');
?>
<div id="ftco-loader" class="show fullscreen"><svg class="circular" width="48px" height="48px">
<circle class="path-bg" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke="#eeeeee" />
<circle class="path" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke-miterlimit="10" stroke="#F96D00" />
</svg></div>
<script src="js/jquery.min.js"></script>
<script src="js/jquery-migrate-3.0.1.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.easing.1.3.js"></script>
<script src="js/jquery.waypoints.min.js"></script>
<script src="js/jquery.stellar.min.js"></script>
<script src="js/owl.carousel.min.js"></script>
<script src="js/jquery.magnific-popup.min.js"></script>
<script src="js/aos.js"></script>
<script src="js/jquery.animateNumber.min.js"></script>
<script src="js/bootstrap-datepicker.js"></script>
<script src="js/jquery.timepicker.min.js"></script>
<script src="js/scrollax.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBVWaKrjvy3MaE7SQ74_uJiULgl1JY0H2s&sensor=false"></script>
<script src="js/google-map.js"></script>
<script src="js/main.js"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-23581568-13"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'UA-23581568-13');
</script>
</body>
</html>