-
Notifications
You must be signed in to change notification settings - Fork 19
/
cart.php
254 lines (241 loc) · 9.96 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
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
<!DOCTYPE html>
<html>
<head>
<title>Add to cart functionality in php and mysql</title>
<link rel="stylesheet" href="../library/css/bootstrap.min.css">
<script src="../library/js/jquery-3.2.1.min.js"></script>
<script src="../library/js/bootstrap.js"></script>
</head>
<style type="text/css">
#itemCount,.glyphicon {
font-size: 18px;
}
.alert, #loader {
display: none;
}
</style>
<body>
<div class="container">
<div class="text-center">
<h2 style="margin-top: 0px; padding-top: 0; padding-left: 5px; ">Update your seats</h2>
</div>
<hr>
<?php
session_start();
require_once('db/DbConnect.php');
$db = new DbConnect();
$conn = $db->connect();
require 'classes/cart.class.php';
$objCart = new cart($conn);
$objCart->setCid($_SESSION['cid']);
$cartItems = $objCart->getAllCartItems();
$cartCss = 'display: none';
$emptyCss = 'display: block';
if (count($cartItems) > 0) {
$cartCss = 'display: block';
$emptyCss = 'display: none';
}
?>
<div class="row">
<div class="col-md-12 text-right">
<a href="cart.php"><span class="glyphicon glyphicon-shopping-cart"></span><sup id="itemCount"><?php echo count($cartItems); ?></sup></a>
</div>
</div>
<div class="col-md-10 col-md-offset-1">
<div class="alert alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">x</button>
<div id="result"></div>
</div>
<center><img src="images/loader.gif" id="loader"></center>
</div>
<div id="fullCart" class="row" style="<?=$cartCss?>">
<div class="col-sm-12 col-md-10 col-md-offset-1">
<table class="table table-hover">
<thead>
<tr>
<th>Workshop</th>
<th>Seats</th>
<th class="text-center">Price</th>
<th class="text-center">Total</th>
<td>
<button id="clearItems" type="button" class="btn btn-danger">
<span class="glyphicon glyphicon-trash"></span> Clear
</button>
</td>
</tr>
</thead>
<tbody>
<?php
$subTotal = 0;
$quantity = 0;
$tax = 10;
foreach ($cartItems as $key => $cartItem) {
$subTotal += $cartItem['totalAmount'];
$quantity += $cartItem['quantity'];
?>
<tr id="item_<?= $cartItem['id']; ?>">
<td class="col-sm-8 col-md-6">
<div class="media">
<a class="thumbnail pull-left" href="#"> <img class="media-object" src="images/<?= $cartItem['image']; ?>" style="width: 72px; height: 72px;"> </a>
<div style="padding-left: 10px;" class="media-body">
<h4 class="media-heading"><a href="#"><?= $cartItem['title']; ?></a></h4>
<p><?= substr( $cartItem['description'], 0, 60) . '...'; ?></p>
</div>
</div>
</td>
<td class="col-sm-1 col-md-1" style="text-align: center">
<select onchange="updateCart(<?= $cartItem['pid']; ?>, <?= $cartItem['id']; ?>)" class="form-control" id="seat_<?= $cartItem['id']; ?>">
<?php
for ($i=1; $i < 11; $i++) {
?>
<option value="<?= $i; ?>" <?php echo ($i == $cartItem['quantity']) ? "selected" : ''; ?>><?= $i; ?></option>
<?php } ?>
</select>
</td>
<td class="col-sm-1 col-md-1 text-center">
<strong><span style="font-size: 18px;">₹</span><span id="price"><?= number_format( $cartItem['price'], 2 ); ?></span>
</strong>
</td>
<td class="col-sm-1 col-md-1 text-center">
<strong><span style="font-size: 18px;">₹</span><span id="totalPrice_<?= $cartItem['id']; ?>"><?= number_format( $cartItem['totalAmount'], 2 ); ?></span>
</strong>
</td>
<td class="col-sm-1 col-md-1">
<button type="button" class="btn btn-danger" onclick="removeItem(<?= $cartItem['id']; ?>)">
<span class="glyphicon glyphicon-remove"></span> Remove
</button>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="4" align="right">Subtotal</td>
<td class="text-right">
<strong><span style="font-size: 18px;">₹</span>
<span id="subTotal"><?= number_format( $subTotal, 2 ); ?></span>
</strong>
</td>
</tr>
<tr>
<td colspan="4" align="right">Taxes</td>
<td class="text-right">
<strong><span style="font-size: 18px;">₹</span>
<span id="taxes"><?= number_format( $tax * $quantity, 2 ); ?></span>
</strong>
</td>
</tr>
<tr>
<td colspan="4" align="right">Total</td>
<td class="text-right">
<strong><span style="font-size: 18px;">₹</span>
<span id="finalPrice"><?= number_format( $subTotal+($tax * $quantity), 2 ); ?></span>
</strong>
</td>
</tr>
<tr>
<td colspan="4" align="right">
<a href="index.php" class="btn btn-default">
<span class="glyphicon glyphicon-shopping-cart"></span> Book workshop
</a>
</td>
<td >
<a href="checkout.php" class="btn btn-success">
Checkout <span class="glyphicon glyphicon-play"></span>
</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="emptyCart" class="row" style="<?=$emptyCss?>">
<div class="col-md-12 text-center">
<p><strong>Your cart is empty. <a href="index.php">Click here</a> to book your seat.</strong></p>
</div>
</div>
<div style="position: fixed; bottom: 10px; right: 10px; color: green;">
<strong>
Durgesh Sahani
</strong>
</div>
</body>
<script type="text/javascript">
function updateCart(pId, cartId) {
console.log($('#seat_'+cartId).val())
$('#loader').show();
$.ajax({
url: "action.php",
data: "wId=" + pId + "&action=update&quantity="+$('#seat_'+cartId).val(),
method: "post"
}).done(function(response) {
console.log(response)
var data = JSON.parse(response);
$('#loader').hide();
$('.alert').show();
if(data.status == 0) {
$('.alert').addClass('alert-danger');
$('#result').html(data.msg);
} else {
$('.alert').addClass('alert-success');
$('#result').html(data.msg);
$('#totalPrice_'+cartId).text( data.data.totalPrice );
$('#subTotal').text( data.data.subTotal);
$('#taxes').text( data.data.taxes);
$('#finalPrice').text( data.data.finalPrice);
}
})
}
function removeItem(cartId) {
$('#loader').show();
$.ajax({
url: "action.php",
data: "cartId=" + cartId + "&action=remove",
method: "post"
}).done(function(response) {
console.log(response);
var data = JSON.parse(response);
$('#loader').hide();
$('.alert').show();
if(data.status == 0) {
$('.alert').addClass('alert-danger');
$('#result').html(data.msg);
} else {
$('.alert').addClass('alert-success');
$('#result').html(data.msg);
$('#item_'+cartId).remove();
$('#itemCount').text( data.data.itemCount);
if (data.data.itemCount == 0.00) {
$('#fullCart').hide();
$('#emptyCart').show();
} else {
$('#subTotal').text( data.data.subTotal);
$('#taxes').text( data.data.taxes);
$('#finalPrice').text( data.data.finalPrice);
}
}
})
}
$('#clearItems').click(function(){
$('#loader').show();
$.ajax({
url: "action.php",
data: "action=clear",
method: "post"
}).done(function(response) {
console.log(response);
var data = JSON.parse(response);
$('#loader').hide();
$('.alert').show();
if(data.status == 0) {
$('.alert').addClass('alert-danger');
$('#result').html(data.msg);
} else {
$('.alert').addClass('alert-success');
$('#result').html(data.msg);
$('#itemCount').text( 0 );
$('#fullCart').hide();
$('#emptyCart').show();
}
})
})
</script>
</html>