diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..ad91452 --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + /usr/local/etc/php/conf.d/docker-php-ext-mysqli.ini, /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini, /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini, /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/app/controllers/CartController.php b/src/app/controllers/CartController.php new file mode 100644 index 0000000..edca590 --- /dev/null +++ b/src/app/controllers/CartController.php @@ -0,0 +1,43 @@ +cartModel = new Cart(); + } + + public function cartAction(){ + if ($_SERVER['REQUEST_METHOD'] !== 'POST') { + header('Location: /cart'); + exit; + } + else { + $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); + } + +} + diff --git a/src/app/controllers/cart.php b/src/app/controllers/cart.php deleted file mode 100644 index f35afea..0000000 --- a/src/app/controllers/cart.php +++ /dev/null @@ -1,11 +0,0 @@ -addProduct($product_id); -} elseif ($_POST['action'] === 'remove') { - $cart->removeProduct($product_id); -} diff --git a/src/app/models/Cart.php b/src/app/models/Cart.php new file mode 100644 index 0000000..641253d --- /dev/null +++ b/src/app/models/Cart.php @@ -0,0 +1,45 @@ + $name, + 'quantity' => 1, + 'price' => $price + ]; + } + } + + public function removeProduct($product_id) { + if (isset($_SESSION['cart'][$product_id])) { + 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; + } + +} \ No newline at end of file diff --git a/src/app/models/cart.php b/src/app/models/cart.php deleted file mode 100644 index c01c25f..0000000 --- a/src/app/models/cart.php +++ /dev/null @@ -1,28 +0,0 @@ -product_list[$product_id], $this->product_list)){ - $this->product_list[$product_id]++; - } - else{ - $this->product_list[$product_id] = 1; - } - } - - public function removeProduct($product_id){ - if($this->product_list[$product_id] > 1){ - $this->product_list[$product_id]--; - } - else{ - unset($this->product_list[$product_id]); - } - } - -} \ No newline at end of file diff --git a/src/app/views/cart.php b/src/app/views/cart.php index e46a237..3faf425 100644 --- a/src/app/views/cart.php +++ b/src/app/views/cart.php @@ -1,16 +1,13 @@ -
-

Current order

- $quantity) { - //TODO: display product name instead of id - echo "
-

" . $product_id . "

-

" . $quantity . "

-
"; - } - ?> -
- - \ No newline at end of file +

Your Cart

+ +

Total:

\ No newline at end of file diff --git a/src/app/views/partials/navbar.php b/src/app/views/partials/navbar.php index 111707c..a634bb5 100644 --- a/src/app/views/partials/navbar.php +++ b/src/app/views/partials/navbar.php @@ -25,7 +25,7 @@ - + @@ -34,4 +34,4 @@ - \ No newline at end of file + diff --git a/src/app/views/product.php b/src/app/views/product.php index 6eb00df..813768a 100644 --- a/src/app/views/product.php +++ b/src/app/views/product.php @@ -17,6 +17,14 @@ . " - " . $product->supplier_phone . "

"; } + echo " +
+ + + + +
" ?> diff --git a/src/routes/web.php b/src/routes/web.php index 4e7487e..18a907a 100644 --- a/src/routes/web.php +++ b/src/routes/web.php @@ -40,7 +40,12 @@ 'controller' => 'ProductController', 'action' => 'viewSingleProduct', 'access' => ['guest', 'client'] - ], + ], + '/cart' => [ + 'controller' => 'CartController', + 'action' => 'viewCart', + 'access' => ['client'] + ], '/admin' => [ 'controller' => 'AdminController', 'action' => 'viewAdmin', @@ -147,7 +152,12 @@ 'controller' => 'AuthController', 'action' => 'updateAccount', 'access' => ['client'] - ], + ], + '/cart' => [ + 'controller' => 'CartController', + 'action' => 'cartAction', + 'access' => ['client'] + ], '/admin/accounts/update' => [ 'controller' => 'AdminController', 'action' => 'updateAccountAdmin',