-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
118 lines (114 loc) · 4.98 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
<?php
//action.php
session_start();
$connect = mysqli_connect("localhost", "root", "", "sampath_store");
if(isset($_POST["product_id"]))
{
$order_table = '';
$message = '';
if($_POST["action"] == "add")
{
if(isset($_SESSION["shopping_cart"]))
{
$is_available = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($_SESSION["shopping_cart"][$keys]['product_id'] == $_POST["product_id"])
{
$is_available++;
$_SESSION["shopping_cart"][$keys]['product_quantity'] = $_SESSION["shopping_cart"][$keys]['product_quantity'] + $_POST["product_quantity"];
}
}
if($is_available < 1)
{
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quantity' => $_POST["product_quantity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
}
else
{
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quantity' => $_POST["product_quantity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
}
if($_POST["action"] == "remove")
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($values["product_id"] == $_POST["product_id"])
{
unset($_SESSION["shopping_cart"][$keys]);
$message = '<label class="text-success">Product Removed</label>';
}
}
}
if($_POST["action"] == "quantity_change")
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($_SESSION["shopping_cart"][$keys]['product_id'] == $_POST["product_id"])
{
$_SESSION["shopping_cart"][$keys]['product_quantity'] = $_POST["quantity"];
}
}
}
$order_table .= '
'.$message.'
<table class="table table-bordered">
<tr>
<th width="40%">Product Name</th>
<th width="10%">Quantity</th>
<th width="20%">Price</th>
<th width="15%">Total</th>
<th width="5%">Action</th>
</tr>
';
if(!empty($_SESSION["shopping_cart"]))
{
$total = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
$order_table .= '
<tr>
<td>'.$values["product_name"].'</td>
<td><input type="text" name="quantity[]" id="quantity'.$values["product_id"].'" value="'.$values["product_quantity"].'" class="form-control quantity" data-product_id="'.$values["product_id"].'" /></td>
<td align="right">$ '.$values["product_price"].'</td>
<td align="right">$ '.number_format($values["product_quantity"] * $values["product_price"], 2).'</td>
<td><button name="delete" class="btn btn-danger btn-xs delete" id="'.$values["product_id"].'">Remove</button></td>
</tr>
';
$total = $total + ($values["product_quantity"] * $values["product_price"]);
}
$order_table .= '
<tr>
<td colspan="3" align="right">Total</td>
<td align="right">$ '.number_format($total, 2).'</td>
<td></td>
</tr>
<tr>
<td colspan="5" align="center">
<form method="post" action="cart.php">
<input type="submit" name="place_order" class="btn btn-warning" value="Place Order" />
</form>
</td>
</tr>
';
}
$order_table .= '</table>';
$output = array(
'order_table' => $order_table,
'cart_item' => count($_SESSION["shopping_cart"])
);
echo json_encode($output);
}
?>