-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.inc.php
executable file
·87 lines (71 loc) · 2.18 KB
/
cart.inc.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
<?php
include_once('includes/functions.inc.php');
session_start();
?>
<?php
if (isset($_POST['add'])) {
$menu = $_GET['menu'];
// Cart has at least one product
if (isset($_SESSION['cart'])) {
try {
$checkUnit = new check();
$pid = $_GET['id'];
$sql = "SELECT Quantity FROM product WHERE Product_ID = '$pid';";
$result = mysqli_query($conn, $sql);
$rowQuantity = mysqli_fetch_assoc($result);
$checkUnit->checkProductQuantity($_POST['amount'], $rowQuantity['Quantity']);
$item_array_id = array_column($_SESSION['cart'], "product_id");
// New product is added
if (!in_array($_GET['id'], $item_array_id)) {
$count = count($_SESSION['cart']);
$item_array = array(
'product_id' => $_GET['id'],
'item_name' => $_POST['hidden_name'],
'item_price' => $_POST['hidden_price'],
'item_amount' => $_POST['amount']
);
$_SESSION['cart'][$count] = $item_array;
//echo '<script>window.location="index.php"</script>';
$checkSou = new check();
$checkSou->checkSource($menu);
}
// Not allow duplicate product
else {
echo '<script>alert("Product is added")</script>';
//echo '<script>window.location="index.php"</script>';
$checkSou = new check();
$checkSou->checkSource($menu);
}
}
catch(Exception $e) {
$checkSou = new check();
$checkSou->checkSource($menu);
echo $e->getMessage();
}
}
// First product is added into cart
else {
$item_array = array(
'product_id' => $_GET['id'],
'item_name' => $_POST['hidden_name'],
'item_price' => $_POST['hidden_price'],
'item_amount' => $_POST['amount']
);
$_SESSION['cart'][0] = $item_array;
//echo '<script>window.location="index.php"</script>';
$checkSou = new check();
$checkSou->checkSource($menu);
}
}
// Delete product in cart
if (isset($_GET['action'])) {
if ($_GET['action'] == "delete") {
foreach ($_SESSION['cart'] as $key => $value) {
if ($value['product_id'] == $_GET['id']) {
unset($_SESSION['cart'][$key]);
echo '<script>alert("Product is removed")</script>';
echo '<script>window.location="cart.php"</script>';
}
}
}
}