Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.98 KB | None | 0 0
  1.  
  2. namespace App\Components;
  3.  
  4. use Nette\Application\UI\Control;
  5. use App\Model\ProductRepository;
  6.  
  7. class CartControl extends Control
  8. {
  9.     private $session;
  10.     private $cart;
  11.     protected $productRepository;
  12.  
  13.     public function __construct(\Nette\Http\Session $session, ProductRepository $productRepository)
  14.     {
  15.         $this->session = $session;
  16.         $this->cart = $this->session->getSection('cart');
  17.         $this->productRepository = $productRepository;
  18.     }
  19.  
  20.     public function render(): void
  21.     {
  22.         $cart = ['total' => 0];
  23.         if (is_array($this->cart->items)) {
  24.             foreach ($this->productRepository->findBy(array('id' => array_keys($this->cart->items)))->fetchAll() as $product) {
  25.                 $price = $product->price * $this->cart->items[$product->id];
  26.                 $cart['items'][$product->id] = ['title' => $product->title, 'quantity' => $this->cart->items[$product->id], 'price' => $price];
  27.                 $cart['total'] += $price;
  28.             }
  29.         }
  30.         $this->template->render(__DIR__ . '/cart.latte', (is_array($cart) ? $cart : []));
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement