Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Publico\Controller;
- use Doctrine\ORM\EntityManager;
- use ShoppingCart\Controller\Plugin\ShoppingCart;
- use Zend\Mvc\Controller\AbstractActionController;
- use Zend\View\Model\ViewModel;
- class CarrinhoController extends AbstractActionController
- {
- private $carrinho;
- private $entityManager;
- protected function setCarrinho(ShoppingCart $cart)
- {
- $this->carrinho = $cart;
- return $this;
- }
- protected function getCarrinho()
- {
- if (null === $this->carrinho) {
- $this->setCarrinho(new ShoppingCart());
- }
- return $this->carrinho;
- }
- protected function setEntityManager(EntityManager $em)
- {
- $this->entityManager = $em;
- return $this;
- }
- protected function getEntityManager()
- {
- if (null === $this->entityManager) {
- $this->setEntityManager($this->getServiceLocator()->get('Doctrine\ORM\EntityManager'));
- }
- return $this->entityManager;
- }
- public function indexAction()
- {
- try {
- $carrinho = $this->getCarrinho();
- } catch (\Exception $e) {
- die($e->getMessage());
- }
- $carrinhoItems = [
- 'carrinho' => $carrinho->cart(),
- 'valorTotal' => $carrinho->total_sum(),
- 'qtdTotal' => $carrinho->total_items(),
- ];
- die($carrinhoItems);
- return new ViewModel([
- 'carrinho' => $this->carrinho->cart(),
- 'valorTotal' => $this->carrinho->total_sum(),
- 'qtdTotal' => $this->carrinho->total_items(),
- ]);
- }
- public function getAction()
- {
- }
- public function addAction()
- {
- $id = $this->getEvent()->getRouteMatch()->getParam('id');
- $produto = $this->getEntityManager()->find('Core\Entity\Produtos', $id);
- $itemCarrinho = [
- 'id' => $produto->getId(),
- 'qty' => 1,
- 'price' => $produto->getPreco(),
- 'product' => $produto->getNome(),
- ];
- $carrinho = $this->getCarrinho();
- return $carrinho->insert($itemCarrinho);
- }
- public function removeAction()
- {
- $token = $this->getEvent()->getRouteMatch()->getParam('token');
- return $this->carrinho->remove($token);
- }
- public function destroyAction()
- {
- return $this->carrinho->destroy();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement