Browse Source

Add online store cart ( issue #8 )

master
Skylar Ittner 1 year ago
parent
commit
275c32f2dd
4 changed files with 123 additions and 2 deletions
  1. 60
    0
      public/action.php
  2. 3
    0
      public/index.php
  3. 30
    1
      public/lib/item.php
  4. 30
    1
      public/parts/cart.php

+ 60
- 0
public/action.php View File

@@ -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;
}

+ 3
- 0
public/index.php View File

@@ -42,6 +42,9 @@ if (isset($_GET['page'])) {
$page = "404";
}
break;
case "cart":
$page = "cart";
break;
case "home":
default:
$page = "home";

+ 30
- 1
public/lib/item.php View File

@@ -100,11 +100,40 @@ END;
<button type="submit" class="btn btn-primary"><i class="fas fa-cart-plus"></i> Add</button>
</div>
</div>
<input type="hidden" name="item" value="<?php echo $id; ?>" />
<input type="hidden" name="item" value="$id" />
<input type="hidden" name="action" value="addtocart" />
</form>
</div>
</div>
END;
return $html;
}

static function cart(Item $item, int $qty) {
$id = $item->getId();
$name = $item->getName();
$catid = $item->getCategoryId();
$catname = $item->getCategoryName();
$price = $item->getPrice();
$html = <<<END
<div class="list-group-item d-flex flex-wrap">
<div>
<h4><a href="./?page=item&id=$id">$name</a></h4>
<span>$$price</span>
</div>
<div class="ml-auto">
<form action="./action.php" method="POST">
<div class="input-group">
<input type="number" name="qty" class="form-control mb-2" placeholder="Quantity" value="$qty" />
<div class="input-group-addon">
<button type="submit" class="btn btn-primary"><i class="fas fa-sync"></i> Update</button>
</div>
</div>
<input type="hidden" name="item" value="$id" />
<input type="hidden" name="action" value="updatecart" />
</form>
</div>
</div>
END;
return $html;
}

+ 30
- 1
public/parts/cart.php View File

@@ -1,8 +1,37 @@
<?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/.
*/

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…
Cancel
Save