-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.php
More file actions
158 lines (130 loc) · 5.62 KB
/
cart.php
File metadata and controls
158 lines (130 loc) · 5.62 KB
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
<?php
include 'components/connection.php';
session_start();
if (isset($_SESSION['user_id'])) {
$user_id = $_SESSION['user_id'];
} else {
$user_id = '';
}
if (isset($_POST['lagout'])) {
session_destroy();
header("location:login.php");
}
// update product in cart
if (isset($_POST['update_cart'])) {
$cart_id = $_POST['cart_id'];
$cart_id = filter_var($cart_id, FILTER_SANITIZE_STRING);
$qty = $_POST['qty'];
$qty = filter_var($qty, FILTER_SANITIZE_STRING);
$update_qty = $conn->prepare("UPDATE `cart` SET qty =? WHERE id = ?");
$update_qty->execute([$qty, $cart_id]);
$success_msg[] = "Cart updated successfully";
}
// delete item from wishlist
if (isset($_POST['delete_item'])) {
$cart_id = $_POST['cart_id'];
$cart_id = filter_var($cart_id, FILTER_SANITIZE_STRING);
$varify_delete_items = $conn->prepare("SELECT * FROM `cart` WHERE id=?");
$varify_delete_items->execute([$cart_id]);
if ($varify_delete_items->rowCount() > 0) {
$delete_cart_id = $conn->prepare("DELETE FROM `cart` WHERE id=?");
$delete_cart_id->execute([$cart_id]);
$success_msg[] = 'item deleted from cart successfully';
} else {
$warning_msg[] = 'cart item already deleted';
}
}
// empty cart
if (isset($_POST['empty_cart'])) {
$varify_empty_item = $conn->prepare("SELECT * FROM `cart` WHERE user_id=?");
$varify_empty_item->execute([$user_id]);
if ($varify_empty_item->rowCount() > 0) {
$delete_cart_id = $conn->prepare("DELETE FROM `cart` WHERE user_id=?");
$delete_cart_id->execute([$user_id]);
$success_msg[] = 'empty successfully';
} else {
$warning_msg[] = 'cart item already deleted';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
<link rel="stylesheet" href="style.css">
<title>MyWebsite - cart </title>
</head>
<body>
<?php include './components/header.php'; ?>
<div class="main">
<div class="banner">
<h1>my cart</h1>
</div>
<div class="title2">
<a href="home.php">home</a><span>/ cart</span>
</div>
<section class="products">
<h1 class="title">products add in cart</h1>
<div class="box-container">
<?php
$grand_total = 0;
$select_cart = $conn->prepare("SELECT * FROM `cart` WHERE `user_id` =?");
$select_cart->execute([$user_id]);
if ($select_cart->rowCount() > 0) {
while ($fetch_cart = $select_cart->fetch(PDO::FETCH_ASSOC)) {
$select_products = $conn->prepare("SELECT * FROM `products` WHERE `id` =? ");
$select_products->execute([$fetch_cart['product_id']]);
if ($select_products->rowCount() > 0) {
$ftch_products = $select_products->fetch(PDO::FETCH_ASSOC);
?>
<form action="" method="post" class="box">
<input type="hidden" name="cart_id" value="<?= $fetch_cart['id'] ?>">
<img src="./image/<?= $ftch_products['image']; ?>" class="img" alt="">
<h3 class="name">
<?= $ftch_products['name'] ?>
</h3>
<div class="flex">
<p class="price">price $<?= $ftch_products['price'] ?> /-</p>
<input type="hidden" name="qty" required min="1" value="<?= $fetch_cart['qty'] ?>" max="99" maxlength="2" class="qty">
<button type="submit" name="update_cart" class="bx bxs-edit fa-edit"></button>
</div>
<p class="sub-total">sub total: <span>$<?= $sub_total = ($fetch_cart['qty'] * $fetch_cart['price']) ?></span></p>
<button type="submit" name="delete_item" class="btn" onclick="return confirm('delete this item')">delete</button>
</form>
<?php
$grand_total += $sub_total;
} else {
echo "<p class='empty'>product was not found</p>";
}
}
} else {
echo '
<p class="empty">no products added yet!</p>';
}
?>
</div>
<?php
if ($grand_total != 0) {
?>
<div class="cart-total">
<p>total amount payable: <span>$<?= $grand_total ?>/-</span></p>
<div class="button">
<form method="post">
<button type="submit" name="empty_cart" class="btn" onclick="return confirm('are you sure to empty your cart')">empty cart</button>
</form>
<a href="checkout.php" class="btn">proceed to checkout</a>
</div>
</div>
<?php
}
?>
</section>
<?php include 'components/footer.php'; ?>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
<script src="script.js"></script>
<?php include './components/alert.php'; ?>
</body>
</html>