/
cart.php
177 lines (144 loc) · 7.71 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
<?php
session_start(); // Session starts
require("lib/db.php"); // Connection to database
$session_id = session_id();
$customer_id = $_SESSION['logged_id'];
// checks if Cart is used in code before.
if (!class_exists("Base")) {
require("lib/class.base.php");
}
if(!class_exists("Cart")) {
require("lib/class.cart.php");
}
$title = 'cart';
?>
<html>
<head>
</head>
<body>
<ul class="cart-list">
<?php
// while loop shows all the added products based on session_id.
$sum = 0;
$taxes = 0;
// takes all the products from cart
$cart_products = $sql->query("
SELECT cart.id, cart.product_id, cart.product_size, cart.pcs, products.name, products.price, products.tax
FROM cart
JOIN products ON cart.product_id = products.id
WHERE cart.session_id = '$session_id' AND cart.pcs > 0
");
while ($cart_row = $cart_products->fetch_assoc()) {
?>
<li><input type="hidden" id="session_id" value="<?= $session_id ?>">
<input type="hidden" id="product_id<?= $cart_row['id'] ?>" value="<?= $cart_row['product_id'] ?>"><a href="product.php?id=<?=$cart_row['product_id']?>"><?= $cart_row['name']; ?>,
<input type="hidden" id="product_size<?= $cart_row['id'] ?>" value="<?= $cart_row['product_size'] ?>"><?php echo $cart_row['product_size']; ?>,
<b><?= $cart_row['price'] ?> €</b>,
<input type="hidden" id="pcs<?= $cart_row['id'] ?>" value="<?= $cart_row['pcs'] ?>">
<p id="pcs_answer<?= $cart_row['id'] ?>"><?= $cart_row['pcs']; ?> pcs</p></a>
<a type="button" class="plus" id="plus<?= $cart_row['id'] ?>"><i class="fa fa-plus-square-o" aria-hidden="true"></i></a>
<a type="button" class="minus" id="minus<?= $cart_row['id'] ?>"><i class="fa fa-minus-square-o" aria-hidden="true"></i></a>
<input type="hidden" id="id<?= $cart_row['id'] ?>" value="<?= $cart_row['id'] ?>">
<a class="delete-item" type="button" id="delete-item<?= $cart_row['id'] ?>" name="delete-btn"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
</li>
<hr>
<?php
$sum = ($sum + $cart_row['price']) * $cart_row['pcs'];
$taxes = ($taxes + $cart_row['tax']) * $cart_row['pcs'];
?>
<script>
var deleteItem = document.getElementById("delete-item" + <?= $cart_row['id']; ?>);
deleteItem.onclick = function() {
var id = document.getElementById("id" + <?= $cart_row['id']; ?>).value;
fetch('delete_cart_ajax.php', {
method: 'POST', // Send as POST
headers: { // Tells headers to the server
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: id
}) // Sending JSON-data to server
}).then(function(response) {
// when then-promise has been succesful parse to json
return response.json();
}).then(function(myJson) {
// when then-promise has been succesful prints json
console.log(myJson);
});
}
var plus = document.getElementById("plus" + <?= $cart_row['id']; ?>);
plus.onclick = function() {
var pcs_answer = document.getElementById("pcs_answer" + <?= $cart_row['id']; ?>);
var pcs = document.getElementById("pcs" + <?= $cart_row['id']; ?>).value;
var product_id = document.getElementById("product_id" + <?= $cart_row['id']; ?>).value;
var session_id = document.getElementById("session_id").value;
var product_size = document.getElementById("product_size" + <?= $cart_row['id']; ?>).value;
fetch('plus_ajax.php', {
method: 'POST', // Send as POST
headers: { // Tells headers to the server
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
product_id: product_id,
session_id: session_id,
product_size: product_size
}) // Sending JSON-data to server
}).then(function(response) {
// when then-promise has been succesful parse to json
return response.json();
}).then(function(myJson) {
// when then-promise has been succesful prints json
pcs_answer.innerHTML = myJson;
total = total + 1;
});
}
var minus = document.getElementById("minus" + <?= $cart_row['id']; ?>);
minus.onclick = function() {
var pcs_answer2 = document.getElementById("pcs_answer" + <?= $cart_row['id']; ?>);
var pcs2 = document.getElementById("pcs" + <?= $cart_row['id']; ?>).value;
var product_id2 = document.getElementById("product_id" + <?= $cart_row['id']; ?>).value;
var session_id2 = document.getElementById("session_id").value;
var product_size2 = document.getElementById("product_size" + <?= $cart_row['id']; ?>).value;
fetch('minus_ajax.php', {
method: 'POST', // Send as POST
headers: { // Tells headers to the server
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
product_id2: product_id2,
session_id2: session_id2,
product_size2: product_size2
}) // Sending JSON-data to server
}).then(function(response) {
// when then-promise has been succesful parse to json
return response.json();
}).then(function(myJson) {
// when then-promise has been succesful prints json
pcs_answer2.innerHTML = myJson;
total = total - 1;
});
}
// updates cart using ajax and deleting "answer" in the product-card
$("#delete-item" + <?= $cart_row['id'] ?>).click(function() {
$("#cartcontent").load("cart.php");
$("#answer" + <?= $cart_row['product_id']; ?>).empty(); // cleans the answer-div
});
// updates cart-button number
$("#plus" + <?= $cart_row['id'] ?>).click(function() {
$("#cartcontent").load("cart.php");
});
// updates cart-button number
$("#minus" + <?= $cart_row['id'] ?>).click(function() {
$("#cartcontent").load("cart.php");
});
</script>
<?php
}
?>
<b>Total sum: <?= $sum ?> € </b> (Taxes: <?= $taxes ?> €)
</ul>
</body>
</html>