-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcart.php
130 lines (120 loc) · 5.06 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
<?php
session_start();
require_once "functions/database_functions.php";
require_once "functions/cart_functions.php";
// book_isbn got from form post method, change this place later.
if(isset($_POST['bookisbn'])){
$book_isbn = $_POST['bookisbn'];
}
if(isset( $_SESSION['id'])){
$userId = $_SESSION['id'];
}
if(!isset($_SESSION['cart']) && isset($userId)) {
$_SESSION['cart'] = array();
$cart = loadCart($userId);
foreach ($cart as $row) {
$_SESSION['cart'][$row["book_isbn_fk"]] = $row['quantity'];
}
}
if(isset($book_isbn)){
if(!isset($_SESSION['cart'][$book_isbn])){
$_SESSION['cart'][$book_isbn] = 1;
} elseif(isset($_POST['cart'])){
$_SESSION['cart'][$book_isbn]++;
unset($_POST);
}
}
if(isset($_POST['save_change'])){
foreach($_SESSION['cart'] as $isbn =>$qty) {
if ($_POST[$isbn] == '0') {
unset($_SESSION['cart']["$isbn"]);
deleteBookFromCart($isbn, $userId);
} else{
$conn =db_connect();
$availQuantity = getBookQuantityFromInventory($conn, $isbn);
$book = mysqli_fetch_assoc(getBookByIsbn($conn, $isbn));
if ($availQuantity-$_POST[$isbn] < 0) {
echo '<div id="errorMsg" style="color:red;">The quantity specified of the book "'.$book['book_title'].'" exceeds the amount we have available </div>';
}
}
}
foreach($_SESSION['cart'] as $isbn =>$qty) {
$_SESSION['cart']["$isbn"] = $_POST["$isbn"];
insertOrUpdateBookQuantityInCart($isbn,$_SESSION['cart'][$isbn],$userId);
}
}
if (isset($_GET['deleteBook']) && isset( $_SESSION['id']))
{
$userId = $_SESSION['id'];
$isbn=$_GET['deleteBook'];
deleteBookFromCart($isbn, $userId);
unset($_SESSION['cart'][$isbn]);
}
// print out header here
$title = "Your shopping cart";
require "./template/menu.php";
if(isset($_SESSION['id'])) {
if (isset($_SESSION['cart']) && (array_count_values($_SESSION['cart']))) {
$_SESSION['total_price'] = total_price($_SESSION['cart']);
$_SESSION['total_items'] = total_items($_SESSION['cart']);
?>
<h1>My Cart</h1>
<form action="cart.php" method="post">
<table class="table">
<tr>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th> </th>
</tr>
<?php
foreach ($_SESSION['cart'] as $isbn => $qty) {
$conn = db_connect();
$book = mysqli_fetch_assoc(getBookByIsbn($conn, $isbn));
?>
<tr>
<td><?php echo $book['book_title'] . " by " . $book['book_author']; ?><br>
<?php
$availQuantity=getBookQuantityFromInventory($conn ,$isbn);
if ($availQuantity == '0') {
echo '<span style="color:red">The book is Out Of Stock</span>';
} else {
echo '<span style="color:limegreen"> Only ', $availQuantity, ' book(s) left in stock </span>';
}
?>
</td>
<td><?php echo "$" . $book['book_price']; ?></td>
<td><input type="text" value="<?php echo $qty; ?>" size="2" name="<?php echo $isbn; ?>">
</td>
<td><?php echo "$" . $qty * $book['book_price']; ?></td>
<td><a href="?deleteBook=<?php echo $isbn?>">
<i class="fa fa-trash" aria-hidden="true"></i>
</a>
</td>
</tr>
<?php } ?>
<tr>
<th> </th>
<th> </th>
<th><?php echo $_SESSION['total_items']; ?></th>
<th><?php echo "$" . $_SESSION['total_price']; ?></th>
<th> </th>
</tr>
</table>
<input type="submit" class="btn btn-primary" name="save_change" value="Save Changes" onclick="validateQuantityOfProducts();">
</form>
<br/><br/>
<a href="checkout.php" class="btn btn-primary">Go To Checkout</a>
<a href="listofbooks.php" class="btn btn-primary">Continue Shopping</a>
<?php
} else {
echo "<p class=\"text-warning\"><span style=\"color:red\">Your cart is empty! Please make sure you add some books in it!</span></p>";
}
} else {
header('Location:login.php');
}
if(isset($conn)){ mysqli_close($conn); }
//require_once "./template/footer.php";
// if save change button is clicked , change the qty of each bookisbn
?>