Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. <?php
  2. namespace Publico\Controller;
  3.  
  4. use Doctrine\ORM\EntityManager;
  5. use ShoppingCart\Controller\Plugin\ShoppingCart;
  6. use Zend\Mvc\Controller\AbstractActionController;
  7. use Zend\View\Model\ViewModel;
  8.  
  9. class CarrinhoController extends AbstractActionController
  10. {
  11.     private $carrinho;
  12.     private $entityManager;
  13.  
  14.     protected function setCarrinho(ShoppingCart $cart)
  15.     {
  16.         $this->carrinho = $cart;
  17.         return $this;
  18.     }
  19.  
  20.     protected function getCarrinho()
  21.     {
  22.         if (null === $this->carrinho) {
  23.             $this->setCarrinho(new ShoppingCart());
  24.         }
  25.         return $this->carrinho;
  26.     }
  27.  
  28.     protected function setEntityManager(EntityManager $em)
  29.     {
  30.         $this->entityManager = $em;
  31.         return $this;
  32.     }
  33.  
  34.     protected function getEntityManager()
  35.     {
  36.         if (null === $this->entityManager) {
  37.             $this->setEntityManager($this->getServiceLocator()->get('Doctrine\ORM\EntityManager'));
  38.         }
  39.         return $this->entityManager;
  40.     }
  41.  
  42.     public function indexAction()
  43.     {
  44.         try {
  45.             $carrinho = $this->getCarrinho();
  46.         } catch (\Exception $e) {
  47.             die($e->getMessage());
  48.         }
  49.  
  50.         $carrinhoItems = [
  51.             'carrinho' => $carrinho->cart(),
  52.             'valorTotal' => $carrinho->total_sum(),
  53.             'qtdTotal' => $carrinho->total_items(),
  54.         ];
  55.  
  56.         die($carrinhoItems);
  57.  
  58.         return new ViewModel([
  59.             'carrinho' => $this->carrinho->cart(),
  60.             'valorTotal' => $this->carrinho->total_sum(),
  61.             'qtdTotal' => $this->carrinho->total_items(),
  62.         ]);
  63.     }
  64.  
  65.     public function getAction()
  66.     {
  67.  
  68.     }
  69.  
  70.     public function addAction()
  71.     {
  72.         $id = $this->getEvent()->getRouteMatch()->getParam('id');
  73.         $produto = $this->getEntityManager()->find('Core\Entity\Produtos', $id);
  74.         $itemCarrinho = [
  75.             'id' => $produto->getId(),
  76.             'qty' => 1,
  77.             'price' => $produto->getPreco(),
  78.             'product' => $produto->getNome(),
  79.         ];
  80.         $carrinho = $this->getCarrinho();
  81.         return $carrinho->insert($itemCarrinho);
  82.     }
  83.  
  84.     public function removeAction()
  85.     {
  86.         $token = $this->getEvent()->getRouteMatch()->getParam('token');
  87.         return $this->carrinho->remove($token);
  88.     }
  89.  
  90.     public function destroyAction()
  91.     {
  92.         return $this->carrinho->destroy();
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement