-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcart.php
147 lines (135 loc) · 5.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
<?php include "template.php";
/**
* Shopping Cart.
* Displays (and allows edits) of the items that the user has entered into their cart.
* On submit, writes it to the orderDetails table.
* Additionally, updates messaging table to send message to admin to indicates order has been made.
*
* "Defines" the conn variable, removing the undefined variable errors.
* @var $conn
*/
?>
<title>Cart</title>
<link rel="stylesheet" href="css/orderForm.css">
<?php
// Debug Purposes
//echo '<pre>'; print_r($_SESSION["ShoppingCart"]); echo '</pre>';
if (isset($_SESSION["FirstName"])) {
date_default_timezone_set("Australia/Sydney");
$status = "";
if (isset($_POST['action']) && $_POST['action'] == "remove") {
if (!empty($_SESSION["ShoppingCart"])) {
foreach ($_SESSION["ShoppingCart"] as $key => $value) {
if ($_POST["code"] == $key) {
unset($_SESSION["ShoppingCart"][$key]);
$status = "<div class='box' style='color:red;'>Product is removed from your cart!</div>";
}
if (empty($_SESSION["ShoppingCart"]))
unset($_SESSION["ShoppingCart"]);
}
}
}
//This code runs when the quantity changes for a row
if (isset($_POST['action']) && $_POST['action'] == "change") {
foreach ($_SESSION["ShoppingCart"] as &$value) {
if ($value['code'] === $_POST["code"]) {
$value['quantity'] = $_POST["quantity"];
break; // Stop the loop after we've found the product
}
}
}
?>
<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>
<div class="cart">
<?php
if (isset($_SESSION["ShoppingCart"])) {
$total_price = 0;
?>
<div class="container-fluid">
<div class="row">
<div class="col-2"></div>
<div class="col-2">ITEM NAME</div>
<div class="col-2">QUANTITY</div>
<div class="col-2">UNIT PRICE</div>
<div class="col-2">ITEMS TOTAL</div>
</div>
<?php
foreach ($_SESSION["ShoppingCart"] as $product) {
?>
<div class="row">
<div class="col-2">
<img src='images/productImages/<?php echo $product["image"]; ?>' width="50" height="40"/>
</div>
<div class="col-2"><?php echo $product["productName"]; ?> <br>
<form method='post' action=''>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>"/>
<input type='hidden' name='action' value="remove"/>
<button type='submit' class='remove'>Remove Item</button>
</form>
</div>
<div class="col-2">
<form method='post' action=''>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>"/>
<input type='hidden' name='action' value="change"/>
<select name='quantity' class='quantity' onChange="this.form.submit()">
<option <?php if ($product["quantity"] == 1) echo "selected"; ?>
value="1">1
</option>
<option <?php if ($product["quantity"] == 2) echo "selected"; ?>
value="2">2
</option>
<option <?php if ($product["quantity"] == 3) echo "selected"; ?>
value="3">3
</option>
<option <?php if ($product["quantity"] == 4) echo "selected"; ?>
value="4">4
</option>
<option <?php if ($product["quantity"] == 5) echo "selected"; ?>
value="5">5
</option>
</select>
</form>
</div>
<div class="col-2">
<!--Individual product price-->
<?php echo "$" . $product["price"]; ?>
</div>
<div class="col-2">
<!-- Subtotal for product-->
<?php echo "$" . $product["price"] * $product["quantity"]; ?>
</div>
</div>
<?php
$total_price += ($product["price"] * $product["quantity"]);
}
?>
<div class="row">
<div class="col-12" align="right">
<div class="badge bg-primary text-wrap fs-5">TOTAL: <?php echo "$". $total_price; ?></div>
</div>
</div>
</div>
<form method="post">
<input type="submit" name="orderProducts" value="Order Now"/>
</form>
<?php
}
if(isset($_POST['orderProducts'])) {
// Writing the order to the database
$orderNumber = "ORDER" . substr(md5(uniqid(mt_rand(), true)), 0, 8);
foreach ($_SESSION["ShoppingCart"] as $row) {
$customerID = $_SESSION["CustomerID"];
$productID = $row['id'];
$quantity = $row['quantity'];
$orderDate = date("Y-m-d h:i:sa");
// Write to the Db.
$conn->exec("INSERT INTO Orders (OrderNumber,CustomerID, ProductID, OrderDate, quantity) VALUES('$orderNumber','$customerID','$productID','$orderDate', '$quantity')");
}
$_SESSION["ShoppingCart"] = [];
}
} else {
header("Location:index.php");
}
?>