-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
121 lines (103 loc) · 3.82 KB
/
action.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
<?php
session_start();
require 'connect.php';
if(isset($_SESSION['customer_email']))
{
$user=$_SESSION['customer_email'];
}
// Add products into the cart table
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$pname = $_POST['pname'];
$pprice = $_POST['pprice'];
$pimage = $_POST['pimage'];
$pcode = $_POST['pcode'];
$pqty = $_POST['pqty'];
$total_price = $pprice * $pqty;
$stmt = $con->prepare('SELECT product_id FROM cart WHERE product_id=?');
$stmt->bind_param('s',$pcode);
$stmt->execute();
$res = $stmt->get_result();
$r = $res->fetch_assoc();
$code = $r['product_id'] ?? '';
if (!$code) {
$query = $con->prepare('INSERT INTO cart (product_name,product_price,product_image,qty,total_price,product_id,users) VALUES (?,?,?,?,?,?,?)');
$query->bind_param('sssssss',$pname,$pprice,$pimage,$pqty,$total_price,$pcode,$user);
$query->execute();
echo '<div class="alert alert-success alert-dismissible mt-2">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Item added to your cart!</strong>
</div>';
} else {
echo '<div class="alert alert-danger alert-dismissible mt-2">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Item already added to your cart!</strong>
</div>';
}
}
// Get no.of items available in the cart table
if (isset($_GET['cartItem']) && isset($_GET['cartItem']) == 'cart_item') {
$stmt = $con->prepare('SELECT * FROM cart');
$stmt->execute();
$stmt->store_result();
$rows = $stmt->num_rows;
echo $rows;
}
// Remove single items from cart
if (isset($_GET['remove'])) {
$id = $_GET['remove'];
$stmt = $con->prepare('DELETE FROM cart WHERE product_id=?');
$stmt->bind_param('i',$id);
$stmt->execute();
$_SESSION['showAlert'] = 'block';
$_SESSION['message'] = 'Item removed from the cart!';
header('location:cart.php');
}
// Remove all items at once from cart
if (isset($_GET['clear'])) {
$stmt = $con->prepare('DELETE FROM cart');
$stmt->execute();
$_SESSION['showAlert'] = 'block';
$_SESSION['message'] = 'All Item removed from the cart!';
header('location:cart.php');
}
// Set total price of the product in the cart table
if (isset($_POST['qty'])) {
$qty = $_POST['qty'];
$pid = $_POST['pid'];
$pprice = $_POST['pprice'];
$tprice = $qty * $pprice;
$stmt = $con->prepare('UPDATE cart SET qty=?, total_price=? WHERE id=?');
$stmt->bind_param('isi',$qty,$tprice,$pid);
$stmt->execute();
}
// Checkout and save customer info in the orders table
if (isset($_POST['action']) && isset($_POST['action']) == 'order') {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$products = $_POST['products'];
$grand_total = $_POST['grand_total'];
$address = $_POST['address'];
$pmode = $_POST['pmode'];
$data = '';
$stmt = $con->prepare('INSERT INTO orders (name,email,phone,pmode,products,amount_paid,address,users)VALUES(?,?,?,?,?,?,?,?)');
$stmt->bind_param('ssssssss',$name,$email,$phone,$pmode,$products,$grand_total,$address,$user);
$stmt->execute();
$stmt2 = $con->prepare('DELETE FROM cart');
$stmt2->execute();
$data .= '<div class="text-center">
<h1 class="display-4 mt-2 text-danger">Thank You!</h1>
<h2 class="text-success">Your Order Placed Successfully!</h2>
<h4 class="bg-danger text-light rounded p-2">Items Purchased : ' . $products . '</h4>
<h4>Your Name : ' . $name . '</h4>
<h4>Your E-mail : ' . $email . '</h4>
<h4>Your Phone : ' . $phone . '</h4>
<h4>Total Amount Paid : ' . number_format($grand_total,2) . '</h4>
<h4>Payment Mode : ' . $pmode . '</h4>
</div>';
echo $data;
sleep(20);
echo "<script>window.open('index.php','_self')</script>";
}
?>