-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcart.php
186 lines (180 loc) · 8.18 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
<section class="py-5">
<div class="container">
<div class="row">
<div class="col d-flex justify-content-end mb-2">
<button class="btn btn-outline-dark btn-flat btn-sm" type="button" id="empty_cart">Empty Cart</button>
</div>
</div>
<div class="card rounded-0">
<div class="card-body">
<h3><b>Cart List</b></h3>
<hr class="border-dark">
<?php
$qry = $conn->query("SELECT c.*,p.name,i.price,p.id as pid,cc.category, b.name as `brand`,i.variant from `cart` c inner join `inventory` i on i.id=c.inventory_id inner join products p on p.id = i.product_id inner join brands b on p.brand_id = b.id inner join categories cc on p.category_id = cc.id where c.client_id = ".$_settings->userdata('id'));
while($row= $qry->fetch_assoc()):
$upload_path = base_app.'/uploads/product_'.$row['pid'];
$img = "";
foreach($row as $k=> $v){
$row[$k] = trim(stripslashes($v));
}
if(is_dir($upload_path)){
$fileO = scandir($upload_path);
if(isset($fileO[2]))
$img = "uploads/product_".$row['pid']."/".$fileO[2];
// var_dump($fileO);
}
?>
<div class="d-flex w-100 justify-content-between mb-2 py-2 border-bottom cart-item">
<div class="d-flex align-items-center col-8">
<span class="mr-2"><a href="javascript:void(0)" class="btn btn-sm btn-outline-danger rem_item" data-id="<?php echo $row['id'] ?>"><i class="fa fa-trash"></i></a></span>
<img src="<?php echo validate_image($img) ?>" loading="lazy" class="cart-prod-img mr-2 mr-sm-2" alt="">
<div>
<p class="mb-1 mb-sm-1"><?php echo $row['name'] ?></p>
<small class="text-muted"><?php echo ($row['brand']) ?></small><br>
<small class="text-muted"><?php echo ($row['category']) ?></small><br>
<small class="text-muted"><?php echo ($row['variant']) ?></small><br>
<p class="mb-1 mb-sm-1"><small><b>Price:</b> <span class="price"><?php echo format_num($row['price']) ?></span></small></p>
<div>
<div class="input-group" style="width:130px !important">
<div class="input-group-prepend">
<button class="btn btn-sm btn-outline-secondary min-qty" type="button" id="button-addon1"><i class="fa fa-minus"></i></button>
</div>
<input type="number" class="form-control form-control-sm qty text-center cart-qty" placeholder="" aria-label="Example text with button addon" value="<?php echo $row['quantity'] ?>" aria-describedby="button-addon1" data-id="<?php echo $row['id'] ?>" readonly>
<div class="input-group-append">
<button class="btn btn-sm btn-outline-secondary plus-qty" type="button" id="button-addon1"><i class="fa fa-plus"></i></button>
</div>
</div>
</div>
</div>
</div>
<div class="col text-right align-items-center d-flex justify-content-end">
<h4><b class="total-amount"><?php echo format_num($row['price'] * $row['quantity']) ?></b></h4>
</div>
</div>
<?php endwhile; ?>
<div class="d-flex w-100 justify-content-between mb-2 py-2 border-bottom">
<div class="col-8 d-flex justify-content-end"><h4>Grand Total:</h4></div>
<div class="col d-flex justify-content-end"><h4 id="grand-total">-</h4></div>
</div>
</div>
</div>
<div class="d-flex w-100 justify-content-end">
<a href="./?p=checkout" class="btn btn-sm btn-flat btn-dark">Checkout</a>
</div>
</div>
</section>
<script>
function calc_total(){
var total = 0
$('.total-amount').each(function(){
amount = $(this).text()
amount = amount.replace(/\,/g,'')
amount = parseFloat(amount)
total += amount
})
$('#grand-total').text(parseFloat(total).toLocaleString('en-US'))
}
function qty_change($type,_this){
var qty = _this.closest('.cart-item').find('.cart-qty').val()
var price = _this.closest('.cart-item').find('.price').text()
price = price.replace(/,/g,'')
console.log(price)
var cart_id = _this.closest('.cart-item').find('.cart-qty').attr('data-id')
var new_total = 0
start_loader();
if($type == 'minus'){
qty = parseInt(qty) - 1
}else{
qty = parseInt(qty) + 1
}
price = parseFloat(price)
// console.log(qty,price)
new_total = parseFloat(qty * price).toLocaleString('en-US')
_this.closest('.cart-item').find('.cart-qty').val(qty)
_this.closest('.cart-item').find('.total-amount').text(new_total)
calc_total()
$.ajax({
url:'classes/Master.php?f=update_cart_qty',
method:'POST',
data:{id:cart_id, quantity: qty},
dataType:'json',
error:err=>{
console.log(err)
alert_toast("an error occured", 'error');
end_loader()
},
success:function(resp){
if(!!resp.status && resp.status == 'success'){
end_loader()
}else{
alert_toast("an error occured", 'error');
end_loader()
}
}
})
}
function rem_item(id){
$('.modal').modal('hide')
var _this = $('.rem_item[data-id="'+id+'"]')
var id = _this.attr('data-id')
var item = _this.closest('.cart-item')
start_loader();
$.ajax({
url:'classes/Master.php?f=delete_cart',
method:'POST',
data:{id:id},
dataType:'json',
error:err=>{
console.log(err)
alert_toast("an error occured", 'error');
end_loader()
},
success:function(resp){
if(!!resp.status && resp.status == 'success'){
location.reload()
}else{
alert_toast("an error occured", 'error');
end_loader()
}
}
})
}
function empty_cart(){
start_loader();
$.ajax({
url:'classes/Master.php?f=empty_cart',
method:'POST',
data:{},
dataType:'json',
error:err=>{
console.log(err)
alert_toast("an error occured", 'error');
end_loader()
},
success:function(resp){
if(!!resp.status && resp.status == 'success'){
location.reload()
}else{
alert_toast("an error occured", 'error');
end_loader()
}
}
})
}
$(function(){
calc_total()
$('.min-qty').click(function(){
qty_change('minus',$(this))
})
$('.plus-qty').click(function(){
qty_change('plus',$(this))
})
$('#empty_cart').click(function(){
// empty_cart()
_conf("Are you sure to empty your cart list?",'empty_cart',[])
})
$('.rem_item').click(function(){
_conf("Are you sure to remove the item in cart list?",'rem_item',[$(this).attr('data-id')])
})
})
</script>