src/AppBundle/Controller/CartController.php line 134

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace AppBundle\Controller;
  15. use AppBundle\Model\Product\AbstractProduct;
  16. use AppBundle\Website\Navigation\BreadcrumbHelperService;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\CartInterface;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\Exception\VoucherServiceException;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  20. use Pimcore\Controller\FrontendController;
  21. use Pimcore\Translation\Translator;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  25. class CartController extends FrontendController
  26. {
  27.     const DEFAULT_CART_NAME 'cart';
  28.     /**
  29.      * @var Factory
  30.      */
  31.     protected $factory;
  32.     public function __construct(Factory $factory)
  33.     {
  34.         $this->factory $factory;
  35.     }
  36.     /**
  37.      * @inheritDoc
  38.      */
  39.     public function onKernelController(FilterControllerEvent $event)
  40.     {
  41.         // enable view auto-rendering
  42.         $this->setViewAutoRender($event->getRequest(), true'twig');
  43.     }
  44.     /**
  45.      * @return CartInterface
  46.      */
  47.     protected function getCart()
  48.     {
  49.         $cartManager $this->factory->getCartManager();
  50.         return $cartManager->getOrCreateCartByName(self::DEFAULT_CART_NAME);
  51.     }
  52.     /**
  53.      * @Route("/cart/add-to-cart", name="shop-add-to-cart")
  54.      *
  55.      * @param Request $request
  56.      * @param Factory $ecommerceFactory
  57.      *
  58.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  59.      *
  60.      * @throws \Exception
  61.      */
  62.     public function addToCartAction(Request $requestFactory $ecommerceFactory)
  63.     {
  64.         $id $request->get('id');
  65.         $product AbstractProduct::getById($id);
  66.         if (null === $product) {
  67.             throw new \Exception('Product not found');
  68.         }
  69.         $cart $this->getCart();
  70.         $cart->addItem($product1);
  71.         $cart->save();
  72.         $trackingManager $ecommerceFactory->getTrackingManager();
  73.         $trackingManager->trackCartProductActionAdd($cart$product);
  74.         return $this->redirectToRoute('shop-cart-detail');
  75.     }
  76.     /**
  77.      * @Route("/cart", name="shop-cart-detail")
  78.      *
  79.      * @param Request $request
  80.      * @param BreadcrumbHelperService $breadcrumbHelperService
  81.      * @param Factory $ecommerceFactory
  82.      *
  83.      * @return \Symfony\Component\HttpFoundation\Response
  84.      */
  85.     public function cartListingAction(Request $requestBreadcrumbHelperService $breadcrumbHelperServiceFactory $ecommerceFactory)
  86.     {
  87.         $cart $this->getCart();
  88.         if ($request->getMethod() == Request::METHOD_POST) {
  89.             $items $request->get('items');
  90.             foreach ($items as $itemKey => $quantity) {
  91.                 $product AbstractProduct::getById($itemKey);
  92.                 $cart->updateItem($itemKey$product$quantitytrue);
  93.             }
  94.             $cart->save();
  95.             $trackingManager $ecommerceFactory->getTrackingManager();
  96.             $trackingManager->trackCartUpdate($cart);
  97.         }
  98.         $breadcrumbHelperService->enrichCartPage();
  99.         if ($cart->isEmpty()) {
  100.             return $this->render('cart/cart_empty.html.twig'array_merge($this->view->getAllParameters(), ['cart' => $cart]));
  101.         } else {
  102.             return $this->render('cart/cart_listing.html.twig'array_merge($this->view->getAllParameters(), ['cart' => $cart]));
  103.         }
  104.     }
  105.     /**
  106.      * @Route("/cart/remove-from-cart", name="shop-remove-from-cart")
  107.      *
  108.      * @param Request $request
  109.      * @param Factory $ecommerceFactory
  110.      *
  111.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  112.      */
  113.     public function removeFromCartAction(Request $requestFactory $ecommerceFactory)
  114.     {
  115.         $id $request->get('id');
  116.         $product AbstractProduct::getById($id);
  117.         if (null === $product) {
  118.             throw new \Exception('Product not found');
  119.         }
  120.         $cart $this->getCart();
  121.         $cart->removeItem($id);
  122.         $cart->save();
  123.         $trackingManager $ecommerceFactory->getTrackingManager();
  124.         $trackingManager->trackCartProductActionAdd($cart$product);
  125.         return $this->redirectToRoute('shop-cart-detail');
  126.     }
  127.     /**
  128.      * @Route("/cart/apply-voucher", name="shop-cart-apply-voucher")
  129.      *
  130.      * @param Request $request
  131.      * @param Translator $translator
  132.      * @param Factory $ecommerceFactory
  133.      *
  134.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  135.      *
  136.      * @throws \Exception
  137.      */
  138.     public function applyVoucherAction(Request $requestTranslator $translatorFactory $ecommerceFactory)
  139.     {
  140.         if ($token strip_tags($request->get('voucher-code'))) {
  141.             $cart $this->getCart();
  142.             try {
  143.                 $success $cart->addVoucherToken($token);
  144.                 if ($success) {
  145.                     $this->addFlash('success'$translator->trans('cart.voucher-code-added'));
  146.                     $trackingManager $ecommerceFactory->getTrackingManager();
  147.                     $trackingManager->trackCartUpdate($cart);
  148.                 } else {
  149.                     $this->addFlash('danger'$translator->trans('cart.voucher-code-could-not-be-added'));
  150.                 }
  151.             } catch (VoucherServiceException $e) {
  152.                 $this->addFlash('danger'$translator->trans('cart.error-voucher-code-' $e->getCode()));
  153.             }
  154.         } else {
  155.             $this->addFlash('danger'$translator->trans('cart.empty-voucher-code'));
  156.         }
  157.         return $this->redirectToRoute('shop-cart-detail');
  158.     }
  159.     /**
  160.      * @Route("/cart/remove-voucher", name="shop-cart-remove-voucher")
  161.      *
  162.      * @param Request $request
  163.      * @param Translator $translator
  164.      * @param Factory $ecommerceFactory
  165.      *
  166.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  167.      */
  168.     public function removeVoucherAction(Request $requestTranslator $translatorFactory $ecommerceFactory)
  169.     {
  170.         if ($token strip_tags($request->get('voucher-code'))) {
  171.             $cart $this->getCart();
  172.             try {
  173.                 $cart->removeVoucherToken($token);
  174.                 $this->addFlash('success'$translator->trans('cart.voucher-code-removed'));
  175.                 $trackingManager $ecommerceFactory->getTrackingManager();
  176.                 $trackingManager->trackCartUpdate($cart);
  177.             } catch (VoucherServiceException $e) {
  178.                 $this->addFlash('danger'$translator->trans('cart.error-voucher-code-' $e->getCode()));
  179.             }
  180.         } else {
  181.             $this->addFlash('danger'$translator->trans('cart.empty-voucher-code'));
  182.         }
  183.         return $this->redirectToRoute('shop-cart-detail');
  184.     }
  185. }