forked from broxzhang/groceryApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoppingcart.php
197 lines (175 loc) · 7.68 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping Cart</title>
</head>
<style>
.item-container {
box-sizing: content-box;
box-shadow: 3px 2px 2px blue;
border: solid #000000 5px !important;
margin: 20px;
}
.info-container {
min-height: 50vh;
text-align: center;
}
.item-image {
width: 100px;
height: 100px;
}
.item-image:hover {
filter: grayscale(0) contrast(100%);
}
</style>
<body>
<div class="d-flex flex-column col-12">
<?php include('html/header.php') ?>
<div class="d-flex flex-column col-10" style="margin: 80px auto">
<h3>My Cart (<?php echo $_SESSION['numberOfItems'] ?> products)</h3>
<div class="d-flex flex-row flex-wrap">
<div class="d-flex flex-column col-7 bg-white " style="min-width: 750px;">
<div class="container">
<div class="row">
<div class="col-12">
<table class="table table-image">
<tbody>
<?php
$cart = $_SESSION['Cart'];
$ids = array();
foreach ($cart as $item) {
if (array_key_exists($item->itemId, $ids)) {
$ids[$item->itemId] = (intval($ids[$item->itemId]) + intval($item->numberOfItem));
} else {
$ids[$item->itemId] = intval($item->numberOfItem);
}
}
$total = 0;
$itemStr = '';
$xml = simplexml_load_file('database/products.xml') or die('cannot load xml');
foreach ($xml->product as $item) {
foreach ($ids as $key => $value) {
if ($item['id'] == $key) {
echo '<tr>
<td class="w-25">
<img src="', $item->photo, '" class="img-fluid img-thumbnail" alt="Sheep">
</td>
<td class=" text-center">', $item->productName, '</td>
<td class=" text-center d-flex flex-row">
<button class="btn btn-primary" onclick="addOne(', $item['id'], ')">+</button>
<input id="item-', $item['id'], '" class=" text-center" style="width: 80px; margin: 0 20px" value="', $value, '" onchange="changeQuantity(', $item['id'], ')"/>
<button class="btn btn-primary" onclick="minusOne(', $item['id'], ')">-</button>
</td>
<td class=" text-center" style="margin-top: 5px" >$', $item->price, '</td>
<td><button class="btn btn-primary" onclick="removeItem(', $item['id'], ')">
<i class="fas fa-recycle" style="overflow: unset;"></i>
</button></td>
</tr>';
$total += $item->price * $value;
$itemStr .= $item->productName . ' (qty): ' . $value . ';';
}
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="d-flex flex-column col-3">
<div class="d-flex flex-column justify-content-evenly info-container">
<h4>Cart Summary</h4>
<div>Subtotal: <?php echo $total ?></div>
<div>GST: <?php echo round($total * .05, 2) ?></div>
<div>QST: <?php echo round($total * .0998, 2) ?></div>
<div>Estimated Total: <?php echo round($total * 1.1498, 2) ?></div>
<div>
<button class="btn btn-primary" onclick="checkout(<?php echo $_SESSION['username'] ?>,'<?php echo $itemStr ?>',<?php echo round($total * 1.1498, 2) ?>)">Check Out</button>
</div>
<div>
<a class="btn btn-primary" href="index.php">Continue Shopping</a>
</div>
</div>
</div>
</div>
</div>
<?php include('html/footer.html') ?>
</div>
</body>
<script>
function checkout(username, items, price) {
console.log(items);
let post = {
id: 100,
customer: username,
products: items,
totalprice: price
}
$.ajax({
url: "backstoreOrder/add.php",
type: "post",
data: post,
success: function(res) {
console.log(res);
}
})
}
function addOne(id) {
const idstring = "#item-" + id;
input = document.querySelector(idstring);
console.log(input.value);
input.value = Number(input.value) + 1;
changeQuantity(id);
}
function changeQuantity(id) {
const idstring = "#item-" + id;
input = document.querySelector(idstring);
value = Number(input.value);
let obj = {
itemId: id,
numberOfItem: value
}
$.ajax({
url: "php/modify-cart.php",
type: "post",
data: obj,
success: function(res) {
location.reload();
console.log(res);
}
})
}
function minusOne(id) {
const idstring = "#item-" + id;
input = document.querySelector(idstring);
console.log(input.value);
if (input.value > 1) {
input.value = Number(input.value) - 1;
}
if (input.value == 1) {
removeItem(id);
return;
}
changeQuantity(id);
}
function removeItem(id) {
let obj = {
itemId: id,
numberOfItem: 0
}
$.ajax({
url: "php/modify-cart.php",
type: "post",
data: obj,
success: function(res) {
location.reload();
console.log(res);
}
})
}
</script>
</html>