Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace App\Components;
- use Nette\Application\UI\Control;
- use App\Model\ProductRepository;
- class CartControl extends Control
- {
- private $session;
- private $cart;
- protected $productRepository;
- public function __construct(\Nette\Http\Session $session, ProductRepository $productRepository)
- {
- $this->session = $session;
- $this->cart = $this->session->getSection('cart');
- $this->productRepository = $productRepository;
- }
- public function render(): void
- {
- $cart = ['total' => 0];
- if (is_array($this->cart->items)) {
- foreach ($this->productRepository->findBy(array('id' => array_keys($this->cart->items)))->fetchAll() as $product) {
- $price = $product->price * $this->cart->items[$product->id];
- $cart['items'][$product->id] = ['title' => $product->title, 'quantity' => $this->cart->items[$product->id], 'price' => $price];
- $cart['total'] += $price;
- }
- }
- $this->template->render(__DIR__ . '/cart.latte', (is_array($cart) ? $cart : []));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement