-
Notifications
You must be signed in to change notification settings - Fork 4
/
addtocart.php
52 lines (46 loc) · 1.47 KB
/
addtocart.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
<?php
function checkValidQuantity($qty){
if ($qty >= 0){
return true;
}
else return false;
}
function checkifInCart($itemid){
if(isset($_SESSION['cartItems'])){
for($i = 0; $i < sizeof($_SESSION['cartItems']); $i++){ //MAKE SURE ITEMS DONT APPEAR TWICE IN CART
if($_SESSION['cartItems'][$i]["itemid"] == $itemid){
$_SESSION['cartItems'][$i]["qty"] += $qty;
return true;
echo '<script>alert("Added '.$qty.' item(s) to the cart.")</script>';
}
}
}
return false;
}
if(isset($_SERVER["REQUEST_METHOD"])){
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
echo '<script>alert("Please log in before adding to cart.")</script>';
}
else{
// echo '<script>alert("Yo.")</script>';
$username = $_SESSION['username'];
$userid = $_SESSION['userid'];
$qty = $_POST['quantity'];
$alreadyInCart = checkifInCart($itemid);
if(!$alreadyInCart){
//Organize product into array to put into the session variable
$product = array("qty"=>"$qty","itemid"=>"$itemid");
//Add to cartItems array
if(!isset($_SESSION["cartItems"])){
$_SESSION["cartItems"] = array($product);
}
else{
array_push($_SESSION["cartItems"],$product);
}
echo '<script>alert("Added '.$qty.' item(s) to the cart.")</script>';
}
}
}
}
?>