-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.php
More file actions
189 lines (172 loc) · 8.11 KB
/
checkout.php
File metadata and controls
189 lines (172 loc) · 8.11 KB
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
<?php
require_once('config/connect_db.php');
require_once('config/bootstrap.php');
require_once('customer/layouts.php');
session_start();
if(empty($_SESSION['user_id'])) {
header('location: index.php');
}
$user_id = $_SESSION['user_id'];
$query = "SELECT
cart.id AS cart_id,
product.id AS product_id,
prdt_name,
prdt_image,
prdt_sellPrice,
crt_quantity,
prdt_quantity
FROM cart LEFT JOIN product ON cart.crt_product = product.id WHERE crt_user = $user_id";
$result = mysqli_query($conn, $query);
if($result) {
do_html_head('Apple', $bootstrapCSS, $jQueryJS.$bootstrapJS.$fontAwsomeIcons);
do_component_topnav('Apple');
$num_row = mysqli_num_rows($result);
?>
<div class="container mt-5">
<div class="row">
<div class="col-12 col-md-8">
<div class="card border-0 mb-2">
<div class="card-header bg-white">Product List</div>
<div class="card-body">
<ul class="list-group list-group-flush">
<li class="list-group-item bg-light">
<div class="row">
<div class="col-8">Item</div>
<div class="col-2">Price</div>
<div class="col-2 text-right">Quantity</div>
</div>
</li>
<?php
$total_price = 0;
for($i = 0; $i < $num_row; $i++) {
$row = mysqli_fetch_assoc($result);
$total_price += $row['prdt_sellPrice'] * $row['crt_quantity'];
?>
<li class="list-group-item">
<div class="row align-items-center">
<div class="col-8">
<div class="row align-items-center">
<div class="col-3">
<img class="img-fluid" src="<?= $row['prdt_image'] ?>" alt="">
</div>
<input id="product_id" class="product_id" type="hidden" value="<?= $row['product_id'] ?>">
<div class="col-9"><?= $row['prdt_name'] ?></div>
</div>
</div>
<div class="col-3">
RM <?= number_format($row['prdt_sellPrice'], 2) ?>
</div>
<div class="col-1"><?= $row['crt_quantity'] ?></div>
</div>
</li>
<?php
}
?>
</ul>
</div>
</div>
</div>
<div class="col-12 col-md-4">
<div class="card border-0">
<div class="card-body">
<form action="checkout.php" method="post">
<div class="d-flex justify-content-between my-2">
<div class="small text-muted">Subtotal (<?= $num_row ?> items)</div>
<div>RM <?= number_format(floatval($total_price), 2) ?></div>
</div>
<div class="d-flex justify-content-between my-2">
<input id="promo_code" class="form-control" type="text" placeholder="Enter Promo Code">
</div>
<div class="d-flex justify-content-between my-2">
<div class="small text-muted">Total</div>
<div style="color: #ff9326;"><p id="total"><?= number_format(floatval($total_price), 2, '.', '') ?></p></div>
<div class="small text-muted">Discount Total</div>
<div class="text-success">
<p id="discount_total" name="discount_total">0</p>
</div>
</div>
<div class="my-2">
<input id="btn_placeOrder" class="btn btn-warning btn-block" style="background-color: #ff9326;" type="button" value="Place Order">
</div>
</form>
</div>
</div>
<a href="viewCart.php" class="btn-link"><i class="fas fa-arrow-left my-2"></i> Back</a>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
$('#btn_placeOrder').on('click', function() {
discount_total = $('#discount_total').text();
$.ajax({
url: 'functions/customer/placeorder.php',
type: 'POST',
data: {
'user_id' : '<?php echo $_SESSION["user_id"]; ?>',
'position' : '<?php echo $_SESSION["position"]; ?>',
'ord_discount' : discount_total,
},
success: function(data) {
swal({
icon: "success",
title: "Success",
text: "Order Placed Successfully",
timer: 2500,
buttons: false,
}).then(function(){
window.location.assign('index.php');
});
},
error: function(){
swal({
icon: "error",
title: "An error occurred.",
text: "Please try again. Error Code:" + err,
timer: 1500,
buttons: false,
});
}
});
});
$('#promo_code').on('change', function(){
$.ajax({
url: 'functions/checkVoucher.php',
type: 'GET',
data: { 'promo_code' : this.value },
success: function(data){
var parsedData = JSON.parse(data);
var promo_total = 0;
if(parsedData.voucherExists == "1"){
$('#promo_code').attr('class', 'form-control is-valid');
var product_ids =[];
$('.product_id').each(function(value){
var quantity = $(this).parents('.row').find('.col-1').html();
quantity = parseInt(quantity);
product_ids.push([$(this).val(), quantity]);
});
console.log(product_ids);
for($i = 0; $i <product_ids.length; $i++){
if(product_ids[$i][0] == parsedData.promo_prdt){
promo_total += parseInt(parsedData.promo_discount) * product_ids[$i][1];
}
}
$('#discount_total').html(promo_total);
total_value = parseFloat($('#total').html()) - promo_total;
if(total_value < 0){
total_value = 0;
}
$('#total').html(total_value);
}else{
$('#promo_code').attr('class', 'form-control is-invalid');
}
},
error: function(){
}
});
});
</script>
<?php
do_html_end();
}