-
Notifications
You must be signed in to change notification settings - Fork 1
/
cart_details.php
120 lines (108 loc) · 4.38 KB
/
cart_details.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
<?php
include 'includes/session.php';
$conn = $pdo->open();
$output = '';
if(isset($_SESSION['user'])){
if(isset($_SESSION['cart'])){
foreach($_SESSION['cart'] as $row){
$stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM cart WHERE user_id=:user_id AND product_id=:product_id");
$stmt->execute(['user_id'=>$user['id'], 'product_id'=>$row['productid']]);
$crow = $stmt->fetch();
if($crow['numrows'] < 1){
$stmt = $conn->prepare("INSERT INTO cart (user_id, product_id, quantity) VALUES (:user_id, :product_id, :quantity)");
$stmt->execute(['user_id'=>$user['id'], 'product_id'=>$row['productid'], 'quantity'=>$row['quantity']]);
}
else{
$stmt = $conn->prepare("UPDATE cart SET quantity=:quantity WHERE user_id=:user_id AND product_id=:product_id");
$stmt->execute(['quantity'=>$row['quantity'], 'user_id'=>$user['id'], 'product_id'=>$row['productid']]);
}
}
unset($_SESSION['cart']);
}
try{
$total = 0;
$stmt = $conn->prepare("SELECT *, cart.id AS cartid FROM cart LEFT JOIN products ON products.id=cart.product_id WHERE user_id=:user");
$stmt->execute(['user'=>$user['id']]);
foreach($stmt as $row){
$image = (!empty($row['photo'])) ? 'images/'.$row['photo'] : 'images/noimage.jpg';
$subtotal = $row['price']*$row['quantity'];
$total += $subtotal;
$output .= "
<tr>
<td><button type='button' data-id='".$row['cartid']."' class='btn btn-danger btn-flat cart_delete'><i class='fa fa-remove'></i></button></td>
<td><img src='".$image."' width='30px' height='30px'></td>
<td>".$row['name']."</td>
<td>Rp. ".number_format($row['price'], 2)."</td>
<td class='input-group'>
<span class='input-group-btn'>
<button type='button' id='minus' class='btn btn-default btn-flat minus' data-id='".$row['cartid']."'><i class='fa fa-minus'></i></button>
</span>
<input type='text' class='form-control' value='".$row['quantity']."' id='qty_".$row['cartid']."'>
<span class='input-group-btn'>
<button type='button' id='add' class='btn btn-default btn-flat add' data-id='".$row['cartid']."'><i class='fa fa-plus'></i>
</button>
</span>
</td>
<td>Rp. ".number_format($subtotal, 2)."</td>
</tr>
";
}
$output .= "
<tr>
<td colspan='5' align='right'><b>Total</b></td>
<td><b>Rp. ".number_format($total, 2)."</b></td>
<tr>
";
}
catch(PDOException $e){
$output .= $e->getMessage();
}
}
else{
if(count($_SESSION['cart']) != 0){
$total = 0;
foreach($_SESSION['cart'] as $row){
$stmt = $conn->prepare("SELECT *, products.name AS prodname, category.name AS catname FROM products LEFT JOIN category ON category.id=products.category_id WHERE products.id=:id");
$stmt->execute(['id'=>$row['productid']]);
$product = $stmt->fetch();
$image = (!empty($product['photo'])) ? 'images/'.$product['photo'] : 'images/noimage.jpg';
$subtotal = $product['price']*$row['quantity'];
$total += $subtotal;
$output .= "
<tr>
<td><button type='button' data-id='".$row['productid']."' class='btn btn-danger btn-flat cart_delete'><i class='fa fa-remove'></i></button></td>
<td><img src='".$image."' width='30px' height='30px'></td>
<td>".$product['name']."</td>
<td>Rp. ".number_format($product['price'], 2)."</td>
<td class='input-group'>
<span class='input-group-btn'>
<button type='button' id='minus' class='btn btn-default btn-flat minus' data-id='".$row['productid']."'><i class='fa fa-minus'></i></button>
</span>
<input type='text' class='form-control' value='".$row['quantity']."' id='qty_".$row['productid']."'>
<span class='input-group-btn'>
<button type='button' id='add' class='btn btn-default btn-flat add' data-id='".$row['productid']."'><i class='fa fa-plus'></i>
</button>
</span>
</td>
<td>Rp. ".number_format($subtotal, 2)."</td>
</tr>
";
}
$output .= "
<tr>
<td colspan='5' align='right'><b>Total</b></td>
<td><b>Rp. ".number_format($total, 2)."</b></td>
<tr>
";
}
else{
$output .= "
<tr>
<td colspan='6' align='center'>Shopping cart empty</td>
<tr>
";
}
}
$pdo->close();
echo json_encode($output);
?>