/
cart.php
164 lines (162 loc) · 5.55 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
<!-- Credits of layout : https://bootsnipp.com/snippets/featured/responsive-shopping-cart-->
<!DOCTYPE html>
<html>
<head>
<?php include 'reqs.php';?>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<link rel = "stylesheet" href = "cart.css">
<title>Grocery Store</title>
<script>
checklogin();
</script>
</head>
<body>
<?php include 'navbar.php';?>
<div id="container">
<div class="container">
<table id="cart" class="table table-hover table-condensed">
<thead>
<tr>
<th style="width:50%">Product</th>
<th style="width:10%">Price</th>
<th style="width:8%">Quantity</th>
<th style="width:22%" class="text-center">Subtotal</th>
<th style="width:10%"></th>
</tr>
</thead>
<tbody id="items">
</tbody>
<tfoot>
<tr>
<td><a href="index.php" class="btn btn-warning"><i class="fa fa-angle-left"></i> Continue Shopping</a></td>
<td colspan="2" class="hidden-xs"></td>
<td class="hidden-xs text-center" id="total"></td>
<td><a href="placeorder.php" class="btn btn-success btn-block" onclick = "return verifyAddress();">Checkout <i class="fa fa-angle-right"></i></a></td>
</tr>
</tfoot>
</table>
</div>
</div>
</body>
</html>
<script>
function verifyAddress()
{
$.ajax({
type: "POST",
url: "data.php",
data: {
type: "addcnt",
},
cache: false,
success: function(data) {
console.log(data);
obj = JSON.parse(data);
if(obj['cnt'] == 0)
{
$.toaster('Please add address in account page before checking out','Info','info');
$val1 = 1;
}
else
{
window.location = "placeorder.php";
}
}
});
return false;
}
function updateTotal()
{
$sum = 0;
$(".subtotal").each(function(idx){
$sum += parseFloat($(this).text().substring(1));
});
$('#total').html("<strong>Total ₹" + $sum + "</strong>");
}
function updateQ(Product_id)
{
$qtyid = "#qty" + Product_id;
$subid = "#sub" + Product_id;
$priceid = "#price" + Product_id;
$qty = parseInt($($qtyid).val());
if($qty<1) $qty = 1;
$price = parseInt($($priceid).text().substring(1))*$qty;
$.ajax({
type: "POST",
url: "data.php",
data: {
type: "updateCart",
Product_id: Product_id,
qty: $qty
},
cache: false,
success: function(data) {
console.log(data);
obj = JSON.parse(data);
if('err' in obj)
{
$.toaster(obj['err'],'Error','danger');
if('qty' in obj)
{
$qty = parseInt(obj['qty']);
console.log($qty);
$price = parseInt($($priceid).text().substring(1))*$qty;
$($qtyid).val($qty);
$($subid).html("₹"+$price);
updateTotal();
}
}
else
{
$.toaster('Cart updated successfully','Success','success');
$($qtyid).html($qty);
$($subid).html("₹"+$price);
updateTotal();
}
}
});
if($qty < 0) $qty = 0;
}
function deleteQ(Product_id)
{
$remrow = $("#tr"+Product_id);
$.ajax({
type: "POST",
url: "data.php",
data: {
type: "deleteCart",
Product_id: Product_id,
},
cache: false,
success: function(data) {
obj = JSON.parse(data);
if('err' in obj)
{
$.toaster({title:'Error',message:obj['error'],priority:'info'});
}
else
{
$.toaster('Removed from cart successfully','Success','info');
$remrow.remove();
updateTotal();
}
}
});
$remrow.remove();
}
$(document).ready(function(){
$.ajax({
type: "POST",
url: "data.php",
data: {
type: "viewcart"
},
dataType: "html",
cache: false,
success: function(data) {
$("#items").html(data);
updateTotal();
}
});
})
</script>