Add online store cart ( issue #8 )
parent
d180061bf8
commit
275c32f2dd
@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once __DIR__ . "/../required.php";
|
||||||
|
require_once __DIR__ . "/lib/item.php";
|
||||||
|
|
||||||
|
switch ($VARS['action']) {
|
||||||
|
case "addtocart":
|
||||||
|
$item = $VARS['item'];
|
||||||
|
$qty = $VARS['qty'];
|
||||||
|
|
||||||
|
if (!$binstack->has('items', ['AND' => ['itemid' => $item, 'price[>]' => 0]])) {
|
||||||
|
header('Location: ./?page=cart&msg=invaliditem');
|
||||||
|
die("Invalid item");
|
||||||
|
}
|
||||||
|
$cart = [];
|
||||||
|
|
||||||
|
if (!empty($_SESSION['cart'])) {
|
||||||
|
$cart = $_SESSION['cart'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($cart[$item])) {
|
||||||
|
$cart[$item] = $qty;
|
||||||
|
} else {
|
||||||
|
$cart[$item] += $qty;
|
||||||
|
}
|
||||||
|
|
||||||
|
$_SESSION['cart'] = $cart;
|
||||||
|
|
||||||
|
header('Location: ./?page=cart&msg=itemadded');
|
||||||
|
die();
|
||||||
|
|
||||||
|
break;
|
||||||
|
case "updatecart":
|
||||||
|
$item = $VARS['item'];
|
||||||
|
$qty = $VARS['qty'];
|
||||||
|
|
||||||
|
$cart = [];
|
||||||
|
|
||||||
|
if (!empty($_SESSION['cart'])) {
|
||||||
|
$cart = $_SESSION['cart'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$cart[$item] = $qty;
|
||||||
|
|
||||||
|
if ($qty <= 0) {
|
||||||
|
unset($cart[$item]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$_SESSION['cart'] = $cart;
|
||||||
|
|
||||||
|
header('Location: ./?page=cart&msg=itemupdated');
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
@ -1,8 +1,37 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
if (!defined('NICKELBOX')) {
|
||||||
|
die("Direct access denied.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$cart = [];
|
||||||
|
|
||||||
|
if (!empty($_SESSION['cart'])) {
|
||||||
|
$cart = $_SESSION['cart'];
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="container mt-4">
|
||||||
|
<h1 class="display-4">Cart</h1>
|
||||||
|
|
||||||
|
<div class="list-group list-group-flush">
|
||||||
|
<?php
|
||||||
|
if (count($cart) > 0) {
|
||||||
|
foreach ($cart as $i => $qty) {
|
||||||
|
echo RenderItem::cart(new Item($i), $qty);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
?>
|
||||||
|
<p>The cart is empty.</p>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</div>
|
||||||
|
</div>
|
Loading…
Reference in New Issue