src/AppBundle/Controller/CartController.php line 135

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.         $trackingManager->forwardTrackedCodesAsFlashMessage();
  75.         return $this->redirectToRoute('shop-cart-detail');
  76.     }
  77.     /**
  78.      * @Route("/cart", name="shop-cart-detail")
  79.      *
  80.      * @param Request $request
  81.      * @param BreadcrumbHelperService $breadcrumbHelperService
  82.      * @param Factory $ecommerceFactory
  83.      *
  84.      * @return \Symfony\Component\HttpFoundation\Response
  85.      */
  86.     public function cartListingAction(Request $requestBreadcrumbHelperService $breadcrumbHelperServiceFactory $ecommerceFactory)
  87.     {
  88.         $cart $this->getCart();
  89.         if ($request->getMethod() == Request::METHOD_POST) {
  90.             $items $request->get('items');
  91.             foreach ($items as $itemKey => $quantity) {
  92.                 $product AbstractProduct::getById($itemKey);
  93.                 $cart->updateItem($itemKey$product$quantitytrue);
  94.             }
  95.             $cart->save();
  96.             $trackingManager $ecommerceFactory->getTrackingManager();
  97.             $trackingManager->trackCartUpdate($cart);
  98.         }
  99.         $breadcrumbHelperService->enrichCartPage();
  100.         if ($cart->isEmpty()) {
  101.             return $this->render('cart/cart_empty.html.twig'array_merge($this->view->getAllParameters(), ['cart' => $cart]));
  102.         } else {
  103.             return $this->render('cart/cart_listing.html.twig'array_merge($this->view->getAllParameters(), ['cart' => $cart]));
  104.         }
  105.     }
  106.     /**
  107.      * @Route("/cart/remove-from-cart", name="shop-remove-from-cart")
  108.      *
  109.      * @param Request $request
  110.      * @param Factory $ecommerceFactory
  111.      *
  112.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  113.      */
  114.     public function removeFromCartAction(Request $requestFactory $ecommerceFactory)
  115.     {
  116.         $id $request->get('id');
  117.         $product AbstractProduct::getById($id);
  118.         if (null === $product) {
  119.             throw new \Exception('Product not found');
  120.         }
  121.         $cart $this->getCart();
  122.         $cart->removeItem($id);
  123.         $cart->save();
  124.         $trackingManager $ecommerceFactory->getTrackingManager();
  125.         $trackingManager->trackCartProductActionRemove($cart$product);
  126.         $trackingManager->forwardTrackedCodesAsFlashMessage();
  127.         return $this->redirectToRoute('shop-cart-detail');
  128.     }
  129.     /**
  130.      * @Route("/cart/apply-voucher", name="shop-cart-apply-voucher")
  131.      *
  132.      * @param Request $request
  133.      * @param Translator $translator
  134.      * @param Factory $ecommerceFactory
  135.      *
  136.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  137.      *
  138.      * @throws \Exception
  139.      */
  140.     public function applyVoucherAction(Request $requestTranslator $translatorFactory $ecommerceFactory)
  141.     {
  142.         if ($token strip_tags($request->get('voucher-code'))) {
  143.             $cart $this->getCart();
  144.             try {
  145.                 $success $cart->addVoucherToken($token);
  146.                 if ($success) {
  147.                     $this->addFlash('success'$translator->trans('cart.voucher-code-added'));
  148.                     $trackingManager $ecommerceFactory->getTrackingManager();
  149.                     $trackingManager->trackCartUpdate($cart);
  150.                 } else {
  151.                     $this->addFlash('danger'$translator->trans('cart.voucher-code-could-not-be-added'));
  152.                 }
  153.             } catch (VoucherServiceException $e) {
  154.                 $this->addFlash('danger'$translator->trans('cart.error-voucher-code-' $e->getCode()));
  155.             }
  156.         } else {
  157.             $this->addFlash('danger'$translator->trans('cart.empty-voucher-code'));
  158.         }
  159.         return $this->redirectToRoute('shop-cart-detail');
  160.     }
  161.     /**
  162.      * @Route("/cart/remove-voucher", name="shop-cart-remove-voucher")
  163.      *
  164.      * @param Request $request
  165.      * @param Translator $translator
  166.      * @param Factory $ecommerceFactory
  167.      *
  168.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  169.      */
  170.     public function removeVoucherAction(Request $requestTranslator $translatorFactory $ecommerceFactory)
  171.     {
  172.         if ($token strip_tags($request->get('voucher-code'))) {
  173.             $cart $this->getCart();
  174.             try {
  175.                 $cart->removeVoucherToken($token);
  176.                 $this->addFlash('success'$translator->trans('cart.voucher-code-removed'));
  177.                 $trackingManager $ecommerceFactory->getTrackingManager();
  178.                 $trackingManager->trackCartUpdate($cart);
  179.             } catch (VoucherServiceException $e) {
  180.                 $this->addFlash('danger'$translator->trans('cart.error-voucher-code-' $e->getCode()));
  181.             }
  182.         } else {
  183.             $this->addFlash('danger'$translator->trans('cart.empty-voucher-code'));
  184.         }
  185.         return $this->redirectToRoute('shop-cart-detail');
  186.     }
  187. }