-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
100 lines (70 loc) · 2.82 KB
/
cart.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
<?php
$title = "Cart page";
include_once "./site-parts/header.php";
if (!isset($_SESSION["username"])) {
include_once "./site-parts/login-message.php";
include_once "./site-parts/footer-main.php";
exit();
}
if (isset($_POST["add_to_cart"])) {
// If user already added a product to the cart
if (isset($_SESSION["cart"])) {
$products_array_ids = array_column($_SESSION["cart"], "productId");
if (!in_array($_POST["productId"], $products_array_ids)) {
$product_array = array(
"productId" => $_POST["productId"],
"productName" => $_POST["productName"],
"productDescription" => $_POST["productDescription"],
"productPrice" => $_POST["productPrice"],
"productDiscount" => $_POST["productDiscount"],
"productPhoto" => $_POST["productPhoto"],
"productAvailable" => $_POST["productAvailable"],
"productShortDescription" => $_POST["productShortDescription"],
"productOldPrice" => $_POST["productOldPrice"],
"productQuantity" => 1
);
$_SESSION["cart"][$_POST["productId"]] = $product_array;
// echo '<script>alert("Product added !")</script>';
} else {
echo '<script>alert("Product is already added !")</script>';
}
// If this is the 1st time
} else {
$product_array = array(
"productId" => $_POST["productId"],
"productName" => $_POST["productName"],
"productDescription" => $_POST["productDescription"],
"productPrice" => $_POST["productPrice"],
"productDiscount" => $_POST["productDiscount"],
"productPhoto" => $_POST["productPhoto"],
"productAvailable" => $_POST["productAvailable"],
"productShortDescription" => $_POST["productShortDescription"],
"productOldPrice" => $_POST["productOldPrice"],
"productQuantity" => 1
);
$_SESSION["cart"][$_POST["productId"]] = $product_array;
}
// header("Location: ./products.php");
} else if (isset($_POST["remove_product"])) {
$product_id = $_POST["productId"];
unset($_SESSION["cart"][$product_id]);
} else if (isset($_POST["set_product"])) {
$product_id = $_POST["productId"];
$product_quantity = $_POST["productQuantity"];
$product_array = $_SESSION["cart"][$product_id];
$product_array["productQuantity"] = $product_quantity;
$_SESSION["cart"][$product_id] = $product_array;
} else if (isset($_POST["show_cart"])) {
}
// header("Location: ./index.php");
// else {
// header("Location: ./login.php");
// }
?>
<?php
include_once "./site-parts/cart-label.php";
include_once "./site-parts/mycart.php";
?>
<?php
include_once './site-parts/footer-main.php';
?>