-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddToCart.php
More file actions
37 lines (29 loc) · 1.41 KB
/
Copy pathAddToCart.php
File metadata and controls
37 lines (29 loc) · 1.41 KB
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
<?php // <--- do NOT put anything before this PHP tag
include_once('./functions.php');
// Did the user click the buy button AND did they provide a ProductID?
if (!isset($_POST['BuyButton']) && !isset($_POST['Quantity']) && !isset($_GET['ProductID'])) {
include "./views/partials/head.php";
include "./views/Error.view.php";
} else {
$productToBuy = trim($_GET['ProductID']);
// add the product to the shopping cart cookie
// if the cookie is defined AND not an empty string
if (isset($_COOKIE['ShoppingCart']) && $_COOKIE['ShoppingCart'] != "") {
// TODO: Get the list of items in the shopping cart.
// and then add the $productToBuy to the end of the comma separated list.
// TODO: set the "ShoppingCart" cookie (notice the capital letters).
// setcookie(...);
$items = str_getcsv($_COOKIE['ShoppingCart']);
$items[] = $productToBuy . "=" . $_POST['Quantity'];
setcookie('ShoppingCart', implode(",", $items));
} else {
// add this producuID to the shopping cart.
// because this is the first item in the cart no commas are required.
// TODO: set the "ShoppingCart" cookie (notice the capital letters).
// setcookie(...);
setcookie("ShoppingCart", (string) $productToBuy . "=" . $_POST['Quantity']);
}
// redirect the user back to ViewProduct.php
$redirection = $_SERVER['HTTP_REFERER'] ?? "./ViewProduct.php?ProductID=$productToBuy";
redirect($redirection);
}