-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcartFunctions.php
More file actions
162 lines (142 loc) · 4.46 KB
/
cartFunctions.php
File metadata and controls
162 lines (142 loc) · 4.46 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
159
160
161
162
<?php
session_start();
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'add':
addItem();
break;
case 'update':
updateItem();
break;
case 'remove':
removeItem();
break;
case 'deliveryOption':
deliveryOption();
break;
}
}
function addItem() {
// Check if user logged in
if (! isset($_SESSION["ShopperID"])) {
// redirect to login page if the session variable shopperid is not set
header ("Location: login.php");
exit;
}
// TO DO 1
// Write code to implement: if a user clicks on "Add to Cart" button, insert/update the
// database and also the session variable for counting number of items in shopping cart.
include_once("mysql_conn.php"); // Establish database connection handle: $conn
// Check if a shopping cart exist, if not create a new shopping cart
if(!isset($_SESSION["Cart"])){
//create a shopping cart for the shopper
$qry= "INSERT INTO Shopcart(ShopperID) VALUES(?)";
$stmt=$conn->prepare($qry);
$stmt->bind_param("i",$_SESSION["ShopperID"]);
$stmt->execute();
$stmt->close();
$qry="SELECT LAST_INSERT_ID() AS ShopCartID";
$result=$conn->query($qry);
$row=$result->fetch_array();
$_SESSION["Cart"]=$row["ShopCartID"];
}
// If the ProductID exists in the shopping cart,
// update the quantity, else add the item to the Shopping Cart.
$pid = $_POST["product_id"];
$quantity= $_POST["quantity"];
$qry="SELECT * FROM ShopCartItem WHERE ShopCartID=? AND ProductID=?";
$stmt=$conn->prepare($qry);
$stmt->bind_param("ii",$_SESSION["Cart"],$pid);
$stmt->execute();
$result=$stmt->get_result();
$stmt->close();
$addNewItem=0;
//if current item is already in the cart, increase quantity
if ($result->num_rows>0){
$qry="UPDATE ShopCartItem SET Quantity=LEAST(Quantity+?,10) WHERE ShopCartID=? AND ProductID=?";
$stmt=$conn->prepare($qry);
$stmt->bind_param("iii",$quantity,$_SESSION["Cart"],$pid);
$stmt->execute();
$stmt->close();
}
//else, add the product to shopping cart
else{
$qry= "INSERT INTO ShopCartItem(ShopCartID,ProductID,Price,Name,Quantity)SELECT ?,?,Price,ProductTitle,? FROM Product WHERE ProductID=?";
$stmt=$conn->prepare($qry);
$stmt->bind_param("iiii",$_SESSION["Cart"],$pid,$quantity,$pid);
$stmt->execute();
$stmt->close();
$addNewItem=1;
}
$conn->close();
if (isset($_SESSION["NumCartItem"])){
$_SESSION["NumCartItem"]=$_SESSION["NumCartItem"]+$addNewItem;
}
else{
$_SESSION["NumCartItem"]=1;
}
header("Location: shoppingCart.php");
exit;
// Update session variable used for counting number of items in the shopping cart.
// Redirect shopper to shopping cart page
}
function updateItem() {
// Check if shopping cart exists
if (! isset($_SESSION["Cart"])) {
// redirect to login page if the session variable cart is not set
header ("Location: login.php");
exit;
}
// TO DO 2
// Write code to implement: if a user clicks on "Update" button, update the database
// and also the session variable for counting number of items in shopping cart.
$cartid=$_SESSION["Cart"];
$pid=$_POST["product_id"];
$quantity=$_POST["quantity"];
include_once("mysql_conn.php");
$qry="UPDATE ShopCartItem SET Quantity=? WHERE ProductID=? AND ShopCartID=?";
$stmt=$conn->prepare($qry);
$stmt->bind_param("iii",$quantity,$pid,$cartid);
$stmt->execute();
$stmt->close();
$conn->close();
header("Location: shoppingCart.php");
exit;
}
function removeItem() {
if (! isset($_SESSION["Cart"])) {
// redirect to login page if the session variable cart is not set
header ("Location: login.php");
exit;
}
// TO DO 3
// Write code to implement: if a user clicks on "Remove" button, update the database
// and also the session variable for counting number of items in shopping cart.
echo("ASDs");
$cartid=$_SESSION["Cart"];
$pid=$_POST["product_id"];
$quantity=$_POST["quantity"];
include_once("mysql_conn.php");
$qry="DELETE FROM ShopCartItem WHERE ProductID=? AND ShopCartID=?";
$stmt=$conn->prepare($qry);
$stmt->bind_param("ii",$pid,$cartid);
$stmt->execute();
$stmt->close();
$conn->close();
header("Location: shoppingCart.php");
$_SESSION["NumCartItem"]=$_SESSION["NumCartItem"]-1;
exit;
}
function deliveryOption(){
if ($_POST["delivery"] == 'Normal'){
$_SESSION["Delivery"] = "Normal";
$_SESSION["ShipCharge"] = 5;
}
else{
$_SESSION["Delivery"] = "Express";
$_SESSION["ShipCharge"] = 10;
}
header ("Location: shoppingCart.php");
exit;
}
?>