-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshopping-cart.php
226 lines (201 loc) · 9.33 KB
/
shopping-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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
session_start();
if (isset($_POST['add_to_cart'])) {
// If user has already add product to the cart
if (isset($_SESSION['cart'])) {
$products_array_ids = array_column($_SESSION['cart'], "product_id");
// If product has already added to cart or not
if (!in_array($_POST['product_id'], $products_array_ids)) {
$product_id = $_POST['product_id'];
$product_array = array(
'product_id' => $_POST['product_id'],
'product_name' => $_POST['product_name'],
'product_price' => $_POST['product_price'],
'product_image' => $_POST['product_image'],
'product_quantity' => $_POST['product_quantity']
);
$_SESSION['cart'][$product_id] = $product_array;
// Product has already been added
} else {
echo '<script>alert("Product was already added to the cart")</script>';
}
// If user the first add product to the cart
} else {
$product_id = $_POST['product_id'];
$product_name = $_POST['product_name'];
$product_price = $_POST['product_price'];
$product_image = $_POST['product_image'];
$product_quantity = $_POST['product_quantity'];
$product_array = array(
'product_id' => $product_id,
'product_name' => $product_name,
'product_price' => $product_price,
'product_image' => $product_image,
'product_quantity' => $product_quantity
);
$_SESSION['cart'][$product_id] = $product_array;
}
// Calculate total
calculateTotalCart();
// Remove product from the cart
} else if (isset($_POST['remove_product'])) {
$product_id = $_POST['product_id'];
unset($_SESSION['cart'][$product_id]);
// Calculate total
calculateTotalCart();
// Codingan baru
} else if (isset($_POST['edit_quantity'])) {
// We get the id from the form
$product_id = $_POST['product_id'];
$product_quantity = $_POST['product_quantity'];
// We get product array from the session
$product_array = $_SESSION['cart'][$product_id];
// Update the product quantity
$product_array['product_quantity'] = $product_quantity;
// Return array back its place
$_SESSION['cart'][$product_id] = $product_array;
// Calculate total
calculateTotalCart();
} else {
//header('location: index.php');
}
function calculateTotalCart()
{
$total_price = 0;
$total_quantity = 0;
foreach ($_SESSION['cart'] as $key => $value) {
$product = $_SESSION['cart'][$key];
$price = $product['product_price'];
$quantity = $product['product_quantity'];
$total_price = $total_price + ($price * $quantity);
$total_quantity = $total_quantity + $quantity;
}
$_SESSION['total'] = $total_price;
$_SESSION['quantity'] = $total_quantity;
}
$kurs_dollar = 15722;
function setRupiah($price)
{
$result = "Rp".number_format($price, 0, ',', '.');
return $result;
}
?>
<?php
include('layouts/header.php');
?>
<!-- Breadcrumb Section Begin -->
<section class="breadcrumb-option">
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="breadcrumb__text">
<h4>Shopping Cart</h4>
<div class="breadcrumb__links">
<a href="index.php">Home <i class="fas fa-chevron-right"></i></a>
<a href="shop.php">Shop <i class="fas fa-chevron-right"></i></a>
<span>Shopping Cart</span>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- Breadcrumb Section End -->
<!-- Shopping Cart Section Begin -->
<section class="shopping-cart spad">
<div class="container">
<div class="row">
<div class="col-lg-8">
<div class="shopping__cart__table">
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Sub Total</th>
<th></th>
</tr>
</thead>
<tbody>
<?php if (isset($_SESSION['cart'])) { ?>
<?php foreach ($_SESSION['cart'] as $key => $value) { ?>
<tr>
<td class="product__cart__item">
<div class="product__cart__item__pic">
<img src="assets/img/product/<?php echo $value['product_image']; ?>" alt="">
</div>
<div class="product__cart__item__text">
<h6><?php echo $value['product_name']; ?></h6>
<h5><?php echo setRupiah(($value['product_price'] * $kurs_dollar)); ?></h5>
</div>
</td>
<td class="quantity__item">
<div class="quantity">
<form method="POST" action="shopping-cart.php">
<div>
<input type="hidden" name="product_id" value="<?php echo $value['product_id'] ?>" />
<h6><input type="number" name="product_quantity" value="<?php echo $value['product_quantity']; ?>"></h6>
<!--
<input type="hidden" name="product_id" value="<?php echo $value['product_id'] ?>">
<input type="number" name="product_quantity" value="<?php echo $value['product_quantity']; ?>">
-->
</div>
<div>
<button class="editbtn" type="submit" name="edit_quantity"><i class="fa fa-refresh"></i> Update</button>
</div>
<!--
<div>
<button class="editbtn" type="submit" name="edit_quantity"><i class="fa fa-refresh"></i> Update Qty</button>
</div>
-->
</form>
</div>
</td>
<td class="cart__price">
<span><?php echo setRupiah(($value['product_quantity'] * ($value['product_price'] * $kurs_dollar))); ?> </span>
</td>
<form method="POST" action="shopping-cart.php">
<td>
<input type="hidden" name="product_id" value="<?php echo $value['product_id'] ?>">
<button type="submit" class="btn btn-danger" name="remove_product"><i class="fa fa-trash"></i></button>
</td>
</form>
</tr>
<?php } ?>
<?php } ?>
</tbody>
</table>
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="continue__btn">
<a href="shop.php" class="btn btn-primary">Continue Shopping <i class="fa fa-arrow-circle-o-right fa-lg"></i></a>
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="cart__discount">
<h6>Discount codes</h6>
<form action="#">
<input type="text" placeholder="Coupon code">
<button type="submit">Apply</button>
</form>
</div>
<div class="cart__total">
<h6>Cart total</h6>
<ul>
<li>Total <span><?php if(isset($_SESSION['cart'])) { echo setRupiah($_SESSION['total'] * $kurs_dollar); } ?></span></li>
</ul>
<form method="POST" action="checkout.php">
<input type="submit" class="primary-btn" value="Checkout" name="checkout">
</form>
</div>
</div>
</div>
</div>
</section>
<!-- Shopping Cart Section End -->
<?php
include('layouts/footer.php');
?>