Skip to content
This repository was archived by the owner on Jul 20, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions src/app/controllers/CartController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

require_once __DIR__ .'/../models/Cart.php';

class CartController{

private $cartModel;

public function __construct(){
$this->cartModel = new Cart();
}

public function cartAction(){
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: /cart');
exit;
}
else {
Comment thread
TheRefraction marked this conversation as resolved.
$product = $_POST['product'];
if ($_POST['action'] === 'add') {
$this->addToCart($product->id, $product->name, $product->price);
} else if ($_POST['action'] === 'remove') {
$this->removeFromCart($product->id);
}
}
}

public function viewCart(){
$cart = $this->cartModel->getCart();
$total = $this->cartModel->computeTotal();
require_once __DIR__ . "/../views/cart.php";
}

public function addToCart($product_id, $quantity, $price) {
$this->cartModel->addProduct($product_id, $quantity, $price);
}

public function removeFromCart($product_id) {
$this->cartModel->removeProduct($product_id);
}

}

11 changes: 0 additions & 11 deletions src/app/controllers/cart.php

This file was deleted.

45 changes: 45 additions & 0 deletions src/app/models/Cart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

class Cart
{
public function __construct() {
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = [];
}
}

public function addProduct($product_id, $name, $price) {
if (isset($_SESSION['cart'][$product_id])) {
Comment thread
TheRefraction marked this conversation as resolved.
$_SESSION['cart'][$product_id]['quantity']++;
} else {
$_SESSION['cart'][$product_id] = [
'name' => $name,
'quantity' => 1,
'price' => $price
];
}
}

public function removeProduct($product_id) {
if (isset($_SESSION['cart'][$product_id])) {
Comment thread
TheRefraction marked this conversation as resolved.
if($_SESSION['cart'][$product_id]['quantity'] - 1 === 0) {
unset($_SESSION['cart'][$product_id]);
} else {
$_SESSION['cart'][$product_id]['quantity'] --;
}
}
}

public function getCart() {
return $_SESSION['cart'];
}

public function computeTotal() {
$total = 0;
foreach ($_SESSION['cart'] as $product) {
$total += $product['quantity'] * $product['price'];
}
return $total;
}

}
28 changes: 0 additions & 28 deletions src/app/models/cart.php

This file was deleted.

25 changes: 12 additions & 13 deletions src/app/views/cart.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php require_once '../controllers/cart.php' ?>
<?php include 'partials/header.php'; ?>
<div>
<h1>Current order</h1>
<?php
foreach ($_SESSION['cart'] as $product_id => $quantity) {
//TODO: display product name instead of id
echo "<div>
<p>" . $product_id . "</p>
<p>" . $quantity . "</p>
</div>";
}
?>
</div>

<h1>Your Cart</h1>
<ul>
<?php foreach ($cart as $item): ?>
<li>
<?php echo $item['name']; ?>
x <?php echo $item['quantity']; ?>,
<?php echo $item['price']; ?> €
</li>
<?php endforeach; ?>
</ul>
<p>Total: <?php echo $total; ?> €</p>
2 changes: 1 addition & 1 deletion src/app/views/partials/navbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<?php if ($userId) { ?>
<li><a href="#">Order</a></li>
<li><a href="/account">My account</a></li>
<li><a href="/basket">Cart</a></li>
<li><a href="/cart">Cart</a></li>
<li><a href="/sign-out">Sign out</a></li>
<?php } else { ?>
<li><a href="/sign-in">Sign in</a></li>
Expand Down
8 changes: 8 additions & 0 deletions src/app/views/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
. " - " . $product->supplier_phone
. "</p>";
}
echo "
Comment thread
TheRefraction marked this conversation as resolved.
<form method='POST' action='/cart'>
<input type='hidden' name='product' value='" . $product . "'>
Comment thread
TheRefraction marked this conversation as resolved.
<button type='submit' name='action' value='add'>Add to Cart</button>
<button type='submit' name='action' value='remove'>Remove from Cart</button>
<!-- Handle appearance of the button above with Js: make
it greyed out or hidden when the add button is clicked ? -->
</form>"
?>
</div>
<?php include 'partials/footer.php'; ?>
2 changes: 2 additions & 0 deletions src/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
'/account-data' => ['AuthController', 'getAccountData'],
'/products' => ['ProductController', 'viewProducts'],
'/product' => ['ProductController', 'viewSingleProduct'],
'/cart' => ['CartController', 'viewCart'],
],
'POST' => [
'/sign-in' => ['AuthController', 'signIn'],
'/sign-up' => ['AuthController', 'signUp'],
'/cart' => ['CartController', 'cartAction'],
],
];