-
Notifications
You must be signed in to change notification settings - Fork 0
/
shoppingCart.php
320 lines (270 loc) · 12.3 KB
/
shoppingCart.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
<?php
include 'incs/header.php';
include 'classes/dbh.php';
include 'classes/user.php';
$dbh = new Dbh();
if (isset($_SESSION['user_id'])) {
$user = new User($firstname, $lastname, $email, $streetname, $housenumber, $postalcode, $city, $password);
}
if (isset($_POST['cart'])) {
//check if product id is already in the cart and if so, add the amount to the existing amount
if (isset($_SESSION['cart'])) {
$cartId = array_column($_SESSION['cart'], 'product_id');
if (in_array($_GET['id'], $cartId)) {
foreach ($_SESSION['cart'] as $key => $value) {
if ($value['product_id'] == $_GET['id']) {
$_SESSION['cart'][$key]['product_quantity'] += $_POST['amount'];
}
}
} else {
$cart = array(
'product_id' => $_GET['id'],
'product_name' => $_POST['name'],
'product_price' => $_POST['price'],
'product_quantity' => $_POST['amount'],
'product_image' => $_POST['image']
);
$_SESSION['cart'][] = $cart;
}
} else {
$cart = array(
'product_id' => $_GET['id'],
'product_name' => $_POST['name'],
'product_price' => $_POST['price'],
'product_quantity' => $_POST['amount'],
'product_image' => $_POST['image']
);
$_SESSION['cart'][] = $cart;
}
}
if (isset($_GET['action'])) {
if ($_GET['action'] == 'delete') {
foreach ($_SESSION['cart'] as $key => $value) {
if ($value['product_id'] == $_GET['id']) {
unset($_SESSION['cart'][$key]);
}
}
}
if ($_GET['action'] == 'plus') {
foreach ($_SESSION['cart'] as $key => $value) {
if ($value['product_id'] == $_GET['id']) {
$_SESSION['cart'][$key]['product_quantity'] += 1;
}
}
}
if ($_GET['action'] == 'minus') {
foreach ($_SESSION['cart'] as $key => $value) {
if ($value['product_id'] == $_GET['id']) {
$_SESSION['cart'][$key]['product_quantity'] -= 1;
}
}
}
}
if (!empty($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
if ($value['product_quantity'] < 1) {
unset($_SESSION['cart'][$key]);
}
}
}
// if is submit and session is set, make variable for user_id and timestamp
if (isset($_POST['submit']) && isset($_SESSION['userId'])) {
$user_id = $_SESSION['userId'];
$timestamp = date("Y-m-d H:i:s");
// insert into orders table
$sql = "INSERT INTO orders (user_id, orders_timestamp) VALUES (?, ?)";
$stmt = $dbh->connection()->prepare($sql);
$stmt->execute([$user_id, $timestamp]);
// select order with same timestamp and user_id;
$sql = "SELECT * FROM orders WHERE user_id = ? AND orders_timestamp = ?";
$stmt = $dbh->connection()->prepare($sql);
$stmt->execute([$user_id, $timestamp]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$last_id = $result['orders_id'];
// insert into order_product table
foreach ($_SESSION['cart'] as $key => $value) {
$sql = "INSERT INTO order_product (orders_id, product_id, order_product_quantity) VALUES (?, ?, ?)";
$stmt = $dbh->connection()->prepare($sql);
$stmt->execute([$last_id, $value['product_id'], $value['product_quantity']]);
}
// remove quantity from product table quantity
foreach ($_SESSION['cart'] as $key => $value) {
$sql = "UPDATE product SET product_quantity = product_quantity - ? WHERE product_id = ?";
$stmt = $dbh->connection()->prepare($sql);
$stmt->execute([$value['product_quantity'], $value['product_id']]);
}
// unset session cart
unset($_SESSION['cart']);
// redirect to orderConfirm page
header("Location: orderConfirm.php?orderId=$last_id");
}
// if is submit and session is not set, make variable for timestamp
if (isset($_POST['submit']) && !isset($_SESSION['userId'])) {
$timestamp = date("Y-m-d H:i:s");
// insert into orders table
$sql = "INSERT INTO orders (orders_timestamp) VALUES (?)";
$stmt = $dbh->connection()->prepare($sql);
$stmt->execute([$timestamp]);
// select order with same timestamp and user_id;
$sql = "SELECT * FROM orders WHERE orders_timestamp = ?";
$stmt = $dbh->connection()->prepare($sql);
$stmt->execute([$timestamp]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$last_id = $result['orders_id'];
// insert into order_product table
foreach ($_SESSION['cart'] as $key => $value) {
$sql = "INSERT INTO order_product (orders_id, product_id, order_product_quantity) VALUES (?, ?, ?)";
$stmt = $dbh->connection()->prepare($sql);
$stmt->execute([$last_id, $value['product_id'], $value['product_quantity']]);
}
// remove quantity from product table quantity
foreach ($_SESSION['cart'] as $key => $value) {
$sql = "UPDATE product SET product_quantity = product_quantity - ? WHERE product_id = ?";
$stmt = $dbh->connection()->prepare($sql);
$stmt->execute([$value['product_quantity'], $value['product_id']]);
}
// insert address in order_adress table database
$sql = "INSERT INTO order_address (orders_id, order_address_streetname, order_address_housenumber, order_address_postalcode, order_address_city) VALUES (?, ?, ?, ?, ?)";
$stmt = $dbh->connection()->prepare($sql);
$stmt->execute([$last_id, $_POST['streetname'], $_POST['housenumber'], $_POST['postalcode'], $_POST['city']]);
// insert user firtsname and lastname and email in order_user table database
$sql = "INSERT INTO order_user (orders_id, order_user_firstname, order_user_lastname, order_user_email) VALUES (?, ?, ?, ?)";
$stmt = $dbh->connection()->prepare($sql);
$stmt->execute([$last_id, $_POST['firstname'], $_POST['lastname'], $_POST['email']]);
// unset session cart
unset($_SESSION['cart']);
// redirect to orderConfirm page
header("Location: orderConfirm.php?orderId=$last_id");
}
?>
<body>
<?php
include 'incs/navBar.php';
?>
<div class="wrapper">
<h1>Winkelwagen</h1>
<div class="project">
<div class="shop">
<?php
$total = 0;
if (!empty($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
echo "
<div class=\"box\">
<img src=\"imgs/$value[product_image]\">
<div class=\"content\">
<h3>$value[product_name]</h3>
<h4>€$value[product_price]</h4>
<p class=\"unit\">Aantal:
<a href=\"shoppingCart.php?action=minus&id=$value[product_id]\"><i class=\"fas fa-minus\"></i></a>
$value[product_quantity]
<a href=\"shoppingCart.php?action=plus&id=$value[product_id]\"><i class=\"fas fa-plus\"></i></a>
</p>
<a href = \"shoppingCart.php?action=delete&id=$value[product_id]\" class=\"btn-area\"><i aria-hidden=\"true\" class=\"fa fa-trash\"></i></a>
</div>
</div>
";
$total = $total + ($value['product_price'] * $value['product_quantity']);
$totalPrice = number_format($total, 2);
}
$totalPrice = $totalPrice;
if (!isset($_SESSION['userId'])) {
echo "
</div>
<div class=\"right-bar\">
<p><span>Totaal:</span>€$totalPrice<span></span></p>
<hr>
<form class=\"login-form\" method=\"post\" action=\"\">
<div class=\"form-control\">
<input type=\"text\" name=\"firstname\" placeholder=\"Voornaam\" required>
<i class=\"fas fa-id-card\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"lastname\" placeholder=\"Achternaam\" required>
<i class=\"fas fa-id-card\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"email\" placeholder=\"E-mail\" required>
<i class=\"fas fa-at\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"streetname\" placeholder=\"Straatnaam\" required>
<i class=\"fas fa-road\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"housenumber\" placeholder=\"Huisnummer\" required>
<i class=\"fas fa-hashtag\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"postalcode\" placeholder=\"Postcode\" required>
<i class=\"fas fa-map-marker-alt\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"city\" placeholder=\"Stad\" required>
<i class=\"fas fa-city\"></i>
</div>
<button name=\"submit\" type=\"submit\" id=\"submit\" class=\"submit-cart\"><i class=\"fa fa-shopping-cart\"></i>Bestellen</button>
</form>
</div>
</div>
</div>
";
} else {
echo "
</div>
<div class=\"right-bar\">
<p><span>Totaal:</span>€$totalPrice<span></span></p>
<hr>
<form class=\"login-form\" method=\"post\" action=\"\">
<div class=\"form-control\">
<input type=\"text\" name=\"firstname\" placeholder=\"Voornaam\" value=\"$userFirstname\" required disabled>
<i class=\"fas fa-id-card\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"lastname\" placeholder=\"Achternaam\" value=\"$userLastname\" required disabled>
<i class=\"fas fa-id-card\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"email\" placeholder=\"E-mail\" value=\"$userEmail\" required disabled>
<i class=\"fas fa-at\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"streetname\" placeholder=\"Straatnaam\" value=\"$userStreetName\" required disabled>
<i class=\"fas fa-road\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"housenumber\" placeholder=\"Huisnummer\" value=\"$userHouseNumber\" required disabled>
<i class=\"fas fa-hashtag\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"postalcode\" placeholder=\"Postcode\" value=\"$userPostalCode\" required disabled>
<i class=\"fas fa-map-marker-alt\"></i>
</div>
<div class=\"form-control\">
<input type=\"text\" name=\"city\" placeholder=\"Stad\" value=\"$userCity\" required disabled>
<i class=\"fas fa-city\"></i>
</div>
<button name=\"submit\" type=\"submit\" id=\"submit\" class=\"submit-cart\"><i class=\"fa fa-shopping-cart\"></i>Bestellen</button>
</form>
</div>
</div>
</div>
";
}
} else {
echo "<h3 class= \"empty-cart-text\">Winkelwagen is leeg</h3>";
}
?>
<!-- button that unsets session cart -->
<!-- <form action="shoppingCart.php" method="post">
<input type="submit" name="empty" value="Empty Cart">
</form>
<?php
if (isset($_POST['empty'])) {
unset($_SESSION['cart']);
header("Location: shoppingCart.php");
}
?> -->
<?php
include 'incs/footer.php';
?>