-
Notifications
You must be signed in to change notification settings - Fork 0
/
product-action.php
63 lines (59 loc) · 1.62 KB
/
product-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
<?php
if(!empty($_GET["action"]))
{
$productId = isset($_GET['id']) ? htmlspecialchars($_GET['id']) : '';
$quantity = isset($_POST['quantity']) ? htmlspecialchars($_POST['quantity']) : '';
switch($_GET["action"])
{
case "add":
if(!empty($quantity)) {
$stmt = $db->prepare("SELECT * FROM dishes where d_id= ?");
$stmt->bind_param('i',$productId);
$stmt->execute();
$productDetails = $stmt->get_result()->fetch_object();
$itemArray = array($productDetails->d_id=>array('title'=>$productDetails->title, 'd_id'=>$productDetails->d_id, 'quantity'=>$quantity, 'price'=>$productDetails->price));
if(!empty($_SESSION["cart_item"]))
{
if(in_array($productDetails->d_id,array_keys($_SESSION["cart_item"])))
{
foreach($_SESSION["cart_item"] as $k => $v)
{
if($productDetails->d_id == $k)
{
if(empty($_SESSION["cart_item"][$k]["quantity"]))
{
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $quantity;
}
}
}
else
{
$_SESSION["cart_item"] = $_SESSION["cart_item"] + $itemArray;
}
}
else
{
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "remove":
if(!empty($_SESSION["cart_item"]))
{
foreach($_SESSION["cart_item"] as $k => $v)
{
if($productId == $v['d_id'])
unset($_SESSION["cart_item"][$k]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
case "check":
header("location:checkout.php");
break;
}
}