forked from heyabhiraj/CANMANsys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
238 lines (207 loc) · 8.82 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
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
227
228
229
230
231
232
233
234
235
236
237
238
<?php
session_start();
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
header("Location: login.php");
exit;
}
include('./admin/function.php');
$items = getDayMenu();
// FUNCTIONS
// Function to remove item from cart
function removeFromCart($itemId)
{
foreach ($_SESSION['cart'] as $key => $cartItem) {
if ($cartItem['id'] == $itemId) {
unset($_SESSION['cart'][$key]);
return;
}
}
}
// Function to calculate subtotal of items in the cart
function calculateSubtotal($cartItems)
{
if (empty($cartItems))
return 0;
$subtotal = 0;
foreach ($cartItems as $cartItem) {
$subtotal += $cartItem['item'][3] * $cartItem['quantity'];
}
return $subtotal;
}
// Function to calculate total including taxes and shipping (if applicable)
function calculateTotal($subtotal)
{
// Add taxes, shipping costs, or any other additional fees to the subtotal
$total = $subtotal;
$_SESSION['total'] = $total;
return $total;
}
//
// PROGRAM STARTS
// Check if item removal request is received
if (isset($_POST['remove_item']) && isset($_POST['item_id'])) {
$itemId = $_POST['item_id'];
removeFromCart($itemId);
// Redirect back to cart page to reflect changes
header('Location: cart.php');
exit;
}
// Check if an item's quantity should be updated
if (isset($_POST['update_quantity']) && isset($_POST['item_id']) && isset($_POST['quantity'])) {
$itemId = $_POST['item_id'];
$quantity = $_POST['quantity'];
updateCartItemQuantity($itemId, $quantity);
}
// Initialize cart items array
$cartItems = array();
// Check if cart session variable exists
if (isset($_SESSION['cart'])) {
// Loop through cart items and retrieve details from $items array
foreach ($_SESSION['cart'] as $cartItem) {
foreach ($items as $item) {
if ($item[0] == $cartItem['id']) {
$cartItems[] = array('item' => $item, 'quantity' => $cartItem['quantity']);
break;
}
}
}
}
// Calculate subtotal
$subtotal = calculateSubtotal($cartItems);
// Calculate total
$total = calculateTotal($subtotal);
//
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home - <?php echo $_SESSION['fname']; ?> </title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="h-20 p-5">
<div class="flex p-5 mb-1 items-center justify-around bg-white ">
<!-- Left side Logo -->
<h1 class="text-4xl text-yellow-600 drop-shadow-lg"> <a href="">CANMANsys </a></h1>
<!-- Right side buttons -->
<div class="flex items-center">
<button class="bg-black text-white rounded-full px-4 py-2" onclick='window.location.href="logout.php"'>logOut</button>
</div>
</div>
<div class=" bg-orange-100 rounded-lg h-900 w-auto p-2 drop-shadow-lg">
<?php
include('./navbar.php');
?>
<?php if (isset($_SESSION['error'])) {
echo "<p class='text-center text-red-500'>" . $_SESSION['error'] . "</p>";
unset($_SESSION['error']); //Clear Error after display
}
?>
<div class="mx-auto max-w-5xl justify-center px-6 md:flex md:space-x-6 xl:px-0">
<div class="rounded-lg md:w-2/3">
<?php if (empty($cartItems)) : ?>
<p>Your cart is empty...</p>
<a href="menu.php">
<p class="w-50 mt-10 text-white text-center bg-black p-1 rounded-md"> Click to ADD more items</p>
</a>
<?php else : ?>
<a href="menu.php">
<p class="mb-8 font-semibold underline">
< Back to menu</p>
</a>
<?php foreach ($cartItems as $cartItem) : ?>
<div class="justify-between mb-6 rounded-lg bg-white p-6 shadow-md sm:flex sm:justify-start">
<img src="img.svg" alt="product-image" class="w-full rounded-lg sm:w-40" />
<div class="sm:ml-4 sm:flex sm:w-full sm:justify-between">
<div class="mt-5 sm:mt-0">
<h2 class="text-lg font-bold text-gray-900"><?= $cartItem['item'][1]; ?> </h2>
<p class="mt-1 text-xs text-gray-700"><?= $cartItem['item'][2]; ?> </p>
</div>
<div class="mt-4 flex justify-between sm:space-y-6 sm:mt-0 sm:block sm:space-x-6">
<div class="flex justify-end">
<form action="cart.php" method="post">
<input type="hidden" name="item_id" value="<?= $cartItem['item'][0]; ?>">
<button type="submit" name="remove_item">
<svg xmlns="https://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="h-5 w-5 hover:text-red-500">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg></button>
</div>
<div class="flex items-center border-gray-100">
<button class="rounded-l bg-gray-100 py-1 px-3.5 duration-100 hover:bg-black hover:text-blue-50" name="update_quantity" onclick="decrementQuantity(this)">-</button>
<input class="h-8 w-8 border bg-white text-center text-xs outline-none" readonly name="quantity" value="<?= $cartItem['quantity']; ?>">
<button class="rounded-l bg-gray-100 py-1 px-3.5 duration-100 hover:bg-black hover:text-blue-50" name="update_quantity" onclick="incrementQuantity(this)">+</button>
</div>
<p class="text-xl text-center font-bold text-green-700">₹ <?= $cartItem['item'][3]; ?> </p>
</div>
</div>
</form>
</div><?php endforeach; ?>
<?php endif; ?>
</div>
<!-- Sub total -->
<div class="mt-10 h-full rounded-lg border bg-white p-6 shadow-md md:mt-0 md:w-1/3">
<form action="order.php" method="POST">
<div class="m-2 flex justify-between">
<p class="text-gray-700">Subtotal</p>
<p class="text-gray-700">₹ <?= $subtotal; ?></p>
</div>
<hr class="my-4 mb-10" />
<div class="flex justify-between">
<p class="text-lg font-bold">Total </p>
<div class="">
<p name="total" class="text-xl font-bold text-green-700">₹ <?= $total; ?></p>
<input type="hidden" name="total" value="<?= $total; ?>">
<p class="text-sm text-gray-700">including Tax</p>
</div>
</div>
<label class="block mb-2 text-sm font-medium text-gray-900">Order notes</label>
<textarea name="ordernotes" rows="3" class="block p-2.5 w-full text-sm text-gray-900 rounded-lg border border-gray-300" placeholder="Cooking Instruction Here..."></textarea>
<label class="block mt-2 text-sm font-medium text-gray-900">Payment </label>
<select id="payment" name="paymentmode" class=" mt-4 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5">
<?php
echo $_SESSION['Type'];
if ($_SESSION['Type'] === 'faculty') {
// If the user type is 'faculty', render the paylater or credit button
echo '<option>Credit or Pay Later</option>';
}
?>
<option>Cash On Delivery</option>
</select>
<label id='paylater' class=' w-full text-xs font-medium text-gray-600'></label>
<button type="submit" name="place_order" class="mt-6 w-full rounded-md bg-yellow-700 py-1.5 font-medium text-blue-50 hover:bg-black">Order Now</button>
</form>
</div>
</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
function incrementQuantity(button) {
var input = button.previousElementSibling;
var value = parseInt(input.value);
input.value = isNaN(value) ? 1 : value + 1;
}
function decrementQuantity(button) {
var input = button.nextElementSibling;
var value = parseInt(input.value);
if (!isNaN(value) && value > 1) {
input.value = value - 1;
}
}
</script>
<script>
$(document).ready(function(){
$('#payment').change(function(){
const mode = $(this).val();
if(mode === "Credit or Pay Later")
$("#paylater").html("The amount will be deducted from your credit after delivery.");
else
$("#paylater").html("");
});
});
</script>
</body>
</html>