-
Notifications
You must be signed in to change notification settings - Fork 2
/
cart.php
250 lines (226 loc) · 8.81 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
<?php
session_start();
// including database connection
require('env/database.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Cart</title>
<link rel="icon" type="image/x-icon" href="assets/svg-logo/logo-bg.svg">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://kit.fontawesome.com/db79afedbd.js" crossorigin="anonymous"></script>
<script src="jquery.js"></script>
<script src="main.js"></script>
<script src="main.js"></script>
<script src="assets/js/script.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="assets/css/main.css" />
</head>
<body>
<script>
function remove_item(id) {
$.ajax({
url: "manage_cart.php",
data: "product_id=" + id + "&Remove_Item=",
method: "post",
success: function(response) {
if (response == "item_removed") {
toastr.options.closeButton = true;
toastr.options.progressBar = true;
toastr['warning']('Product removed from cart.');
$("#header").load("cart.php #header");
$("#container").load("cart.php #container", function() {
subTotal()
});
}
}
});
}
function mod_quantity(id, quantity) {
$.ajax({
url: "manage_cart.php",
data: "product_id=" + id + "&Mod_Quantity=" + quantity,
method: "post",
success: function(response) {
if (response == "item_modified") {
toastr.options.closeButton = true;
toastr.options.progressBar = true;
toastr['success']('Product Quantity Modified.');
$("#container").load("cart.php #container", function() {
subTotal()
});
}
}
});
}
</script>
<!-- ----------------------------------------------------Loading Screen-------------------------------------------------------- -->
<div id="loading">
<img src="assets/svg-logo/LOADER.svg" alt="Loading..." />
</div>
<script>
var loader = document.getElementById("loading");
window.addEventListener("load", function() {
loader.style.display = "none";
})
</script>
<!-- including header -->
<?php
include 'header.php';
?>
<!------------------------------------------------- main body----------------------------------------------------------->
<div class="container mb-3" id="container">
<div class="row">
<div class="col-lg-12 text-center border rounded bg-light my-5">
<h1> CART </h1>
</div>
<div class="col-lg-9 table-responsive">
<table class="table">
<!-- table head -->
<thead class="text-center">
<tr>
<th scope="col">Serial No.</th>
<th scope="col">Product Name</th>
<th scope="col">Product Price</th>
<th scope="col">Quantity</th>
<th scope="col">Total</th>
<th scope="col"></th>
</tr>
</thead>
<!-- table body -->
<tbody class="text-center">
<?php
$gtotal = 0;
// checking if cart is empty or not
if (isset($_SESSION['cart'])) {
// if cart is not empty then we will show all the products in cart
foreach ($_SESSION['cart'] as $key => $value) {
// fetching product details from database
$product_id = $value['product_id'];
$sql = "SELECT * FROM product WHERE product_id = '$product_id'";
$result = mysqli_query($conn, $sql);
// checking if product is available in database or not
while ($row = mysqli_fetch_assoc($result)) {
// storing product details in variables
$p_name = $row['product_name'];
$p_price = $row['product_price'];
} // end of while loop
// displaying product details in table
$sr = $key + 1;
echo "
<tr>
<td>$sr</td>
<td>$p_name</td>
<td>$p_price<input type='hidden' class='iprice' value='$p_price'></td>
<td>
<input class='text-center iquantity' name='Mod_Quantity' onchange='mod_quantity($product_id,this.value);' type='number' value='$value[Quantity]' min='1' max='10'>
</td>
<td class='itotal'></td>
<td>
<button onclick='remove_item($product_id)' class='btn btn-sm btn-outline-danger'>REMOVE</button>
</td>
</tr>
";
$total = $p_price * $value['Quantity'];
$gtotal = $gtotal + $total;
} // end of foreach loop
} // end of if condition
else {
// if cart is empty then we will show a message
echo "
<tr>
<td colspan='6' class='text-center'>Cart is Empty!</td>
</tr>";
}
?>
</tbody>
</table>
</div>
<!-- Make purches section -->
<div class="col-lg-3">
<div class="border bg-light rounded p-4">
<h4>Grand Total:</h4>
<h5 class="text-right" id="gtotal"><?= $gtotal ?></h5>
<?php
// checking if cart is empty or not
if (isset($_SESSION['cart']) && count($_SESSION['cart']) > 0) {
?>
<!-- taking user address -->
<form action="payment.php" method="POST">
<div class="mb-3">
<label for="address">Address</label>
<input type="text" class="form-control" id="address" name="address" placeholder="1234 Main St" required>
<div class="invalid-feedback">
Please enter your shipping address.
</div>
</div>
<!-- taking user city -->
<div class="row">
<div class="col-md-6 mb-3">
<label for="country">Country</label>
<select class="custom-select d-block w-100" name="country" id="country" required>
<option value="">Choose...</option>
<option value="India">India</option>
</select>
<div class="invalid-feedback">
Please select a valid country.
</div>
</div>
<div class="col-md-6 mb-3">
<label for="state">State</label>
<select class="custom-select d-block w-100" name="state" id="state" required>
<option value="">Choose...</option>
<option value="West Bengal">West Bengal</option>
</select>
<div class="invalid-feedback">
Please provide a valid state.
</div>
</div>
<div class=" mb-3">
<label for="zip">Zip</label>
<input type="text" class="form-control" id="zip" name="zip" placeholder="" required>
<div class="invalid-feedback">
Zip code required.
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block" name="continue">Continue</button>
</div>
</form>
<?php
} // end of if condition
?>
</div>
</div>
</div>
</div>
<script>
// Set the initial grand total value
var gt = 0;
var iprice = document.getElementsByClassName('iprice');
var iquantity = document.getElementsByClassName('iquantity');
var itotal = document.getElementsByClassName('itotal');
var gtotal = document.getElementById('gtotal');
function subTotal() {
gt = 0;
for (i = 0; i < iprice.length; i++) {
itotal[i].innerText = (iprice[i].value) * (iquantity[i].value); // Set the total price of each product
gt = gt + (iprice[i].value) * (iquantity[i].value); // Set the grand total
}
gtotal.innerText = gt; // Set the grand total
}
subTotal(); // Calling the function
</script>
<!----------- including footer --------------->
<?php
include 'footer.php';
?>
</body>
</html>