-
Notifications
You must be signed in to change notification settings - Fork 0
/
shopping_cart.php
143 lines (124 loc) · 4.94 KB
/
shopping_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
<?php
session_start();
include('includes/catalog.php');
include('includes/footer.php');
$cart = array();
$quantity = array();
$total = 0;
$cartItemCount = count($_SESSION['cart']);
if (isset($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $isbn) {
if (isset($quantity[$isbn])) {
$quantity[$isbn]++;
} else {
$quantity[$isbn] = 1;
foreach ($catalog as $genre => $products) {
foreach ($products as $product) {
if ($product['isbn'] == $isbn) {
$cart[] = $product;
}
}
}
}
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['empty_cart'])) {
// Clearing the cart by unsetting the cart session variable
$_SESSION['cart'] = array();
// Resetting the cart and quantity variables
$cart = array();
$quantity = array();
$total = 0;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_item'])) {
$isbn_to_remove = $_POST['remove_item'];
if (isset($quantity[$isbn_to_remove])) {
$total -= $cart[array_search($isbn_to_remove, array_column($cart, 'isbn'))]['price'];
$quantity[$isbn_to_remove] = 0;
$_SESSION['cart'] = array_diff($_SESSION['cart'], [$isbn_to_remove]);
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_quantity'])) {
$isbn_to_update = $_POST['isbn'];
$new_quantity = (int)$_POST['new_quantity'];
if (isset($quantity[$isbn_to_update])) {
$old_quantity = $quantity[$isbn_to_update];
if ($new_quantity > $old_quantity) {
// Increase in the quantity
$quantity_difference = $new_quantity - $old_quantity;
$_SESSION['cart'] = array_merge($_SESSION['cart'], array_fill(0, $quantity_difference, $isbn_to_update));
}
elseif ($new_quantity < $old_quantity) {
// Decrease in the quantity
$quantity_difference = $old_quantity - $new_quantity;
$keys = array_keys($_SESSION['cart'], $isbn_to_update);
for ($i = 0; $i < $quantity_difference; $i++) {
unset($_SESSION['cart'][$keys[$i]]);
}
}
$quantity[$isbn_to_update] = max(0, $new_quantity);
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['purchase_order'])) {
// Clearing the cart after purchase
$_SESSION['cart'] = array();
$cart = array();
$quantity = array();
$total = 0;
header("Location: index.php");
exit();
}
include('includes/header.php');
?>
<div class="container mt-5">
<h1 class="text-center">Shopping Cart</h1>
<p><?php echo "Total Number of Items in cart: $cartItemCount (reload page to get accurate number)"; ?></p>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Price</th>
<th>Quantity</th>
<th>Update</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
<?php foreach ($cart as $item): ?>
<tr>
<td><?php echo $item['title']; ?></td>
<td><?php echo $item['author']; ?></td>
<td>$<?php echo $item['price'] * $quantity[$item['isbn']]; ?></td>
<td>
<form action="" method="post">
<input type="hidden" name="isbn" value="<?php echo $item['isbn']; ?>">
<input type="number" name="new_quantity" value="<?php echo $quantity[$item['isbn']]; ?>" min="0">
<input type="submit" name="update_quantity" class="btn btn-primary" value="Update">
</form>
</td>
<td>
<form action="" method="post">
<input type="hidden" name="remove_item" value="<?php echo $item['isbn']; ?>">
<input type="submit" class="btn btn-danger" value="Remove (click twice)">
</form>
</td>
</tr>
<?php $total += $item['price'] * $quantity[$item['isbn']];
endforeach; ?>
</tbody>
<tfoot>
<tr>
<td colspan="2"><strong>Total:</strong></td>
<td><strong>$<?php echo $total; ?></strong></td>
</tr>
</tfoot>
</table>
</div>
<a href="index.php" class="btn btn-primary" style="position: absolute; top: 10px; left: 100px;">Home/Continue Shopping</a>
<form action="" method="post">
<input type="submit" name="empty_cart" class="btn btn-danger" style="position: absolute; top: 10px; right: 100px;" value="Empty Cart">
</form>
<form action="" method="post">
<input type="submit" name="purchase_order" class="btn btn-success" style="position: absolute; top: 10px; right: 250px;" value="Purchase Order">
</form>