src/Controller/CartController.php line 104

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 App\Controller;
  15. use App\Model\Product\AbstractProduct;
  16. use App\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\Bundle\EcommerceFrameworkBundle\Model\CheckoutableInterface;
  21. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\ProductInterface;
  22. use Pimcore\Controller\FrontendController;
  23. use Pimcore\Translation\Translator;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. class CartController extends FrontendController
  30. {
  31.     const DEFAULT_CART_NAME 'cart';
  32.     /**
  33.      * @var Factory
  34.      */
  35.     protected $factory;
  36.     public function __construct(Factory $factory)
  37.     {
  38.         $this->factory $factory;
  39.     }
  40.     /**
  41.      * @return CartInterface
  42.      */
  43.     protected function getCart()
  44.     {
  45.         $cartManager $this->factory->getCartManager();
  46.         return $cartManager->getOrCreateCartByName(self::DEFAULT_CART_NAME);
  47.     }
  48.     /**
  49.      * @Route("/cart/add-to-cart", name="shop-add-to-cart", methods={"POST"})
  50.      *
  51.      * @param Request $request
  52.      * @param Factory $ecommerceFactory
  53.      *
  54.      * @return RedirectResponse
  55.      *
  56.      * @throws \Exception
  57.      */
  58.     public function addToCartAction(Request $requestFactory $ecommerceFactory)
  59.     {
  60.         if (!$this->isCsrfTokenValid('addToCart'$request->get('_csrf_token'))) {
  61.             throw new \Exception('Invalid request');
  62.         }
  63.         $id $request->get('id');
  64.         $product AbstractProduct::getById($id);
  65.         if (null === $product) {
  66.             throw new \Exception('Product not found');
  67.         }
  68.         $cart $this->getCart();
  69.         if ($cart->getItemCount() > 99) {
  70.             throw new \Exception('Maximum Cart items limit Reached');
  71.         }
  72.         $cart->addItem($product1);
  73.         $cart->save();
  74.         $trackingManager $ecommerceFactory->getTrackingManager();
  75.         $trackingManager->trackCartProductActionAdd($cart$product);
  76.         $trackingManager->forwardTrackedCodesAsFlashMessage();
  77.         return $this->redirectToRoute('shop-cart-detail');
  78.     }
  79.     /**
  80.      * @Route("/cart", name="shop-cart-detail")
  81.      *
  82.      * @param Request $request
  83.      * @param BreadcrumbHelperService $breadcrumbHelperService
  84.      * @param Factory $ecommerceFactory
  85.      *
  86.      * @return Response
  87.      */
  88.     public function cartListingAction(Request $requestBreadcrumbHelperService $breadcrumbHelperServiceFactory $ecommerceFactory)
  89.     {
  90.         $cart $this->getCart();
  91.         if ($request->getMethod() == Request::METHOD_POST) {
  92.             if (!$this->isCsrfTokenValid('cartListing'$request->get('_csrf_token'))) {
  93.                 throw new AccessDeniedHttpException('Invalid request');
  94.             }
  95.             $items $request->get('items');
  96.             foreach ($items as $itemKey => $quantity) {
  97.                 if (!is_numeric($quantity)) {
  98.                     continue;
  99.                 }
  100.                 if ($cart->getItemCount() > 99) {
  101.                     break;
  102.                 }
  103.                 $product AbstractProduct::getById($itemKey);
  104.                 if ($product instanceof CheckoutableInterface) {
  105.                     $cart->updateItem($itemKey$productfloor($quantity), true);
  106.                 }
  107.             }
  108.             $cart->save();
  109.             $trackingManager $ecommerceFactory->getTrackingManager();
  110.             $trackingManager->trackCartUpdate($cart);
  111.         }
  112.         $breadcrumbHelperService->enrichCartPage();
  113.         $params array_merge($request->request->all(), $request->query->all());
  114.         if ($cart->isEmpty()) {
  115.             return $this->render('cart/cart_empty.html.twig'array_merge($params, ['cart' => $cart]));
  116.         } else {
  117.             return $this->render('cart/cart_listing.html.twig'array_merge($params, ['cart' => $cart]));
  118.         }
  119.     }
  120.     /**
  121.      * @Route("/cart/remove-from-cart", name="shop-remove-from-cart")
  122.      *
  123.      * @param Request $request
  124.      * @param Factory $ecommerceFactory
  125.      *
  126.      * @return RedirectResponse
  127.      */
  128.     public function removeFromCartAction(Request $requestFactory $ecommerceFactory)
  129.     {
  130.         $id $request->get('id');
  131.         $product AbstractProduct::getById($id);
  132.         $cart $this->getCart();
  133.         $cart->removeItem($id);
  134.         $cart->save();
  135.         if ($product instanceof ProductInterface) {
  136.             $trackingManager $ecommerceFactory->getTrackingManager();
  137.             $trackingManager->trackCartProductActionRemove($cart$product);
  138.             $trackingManager->forwardTrackedCodesAsFlashMessage();
  139.         }
  140.         return $this->redirectToRoute('shop-cart-detail');
  141.     }
  142.     /**
  143.      * @Route("/cart/apply-voucher", name="shop-cart-apply-voucher")
  144.      *
  145.      * @param Request $request
  146.      * @param Translator $translator
  147.      * @param Factory $ecommerceFactory
  148.      *
  149.      * @return RedirectResponse
  150.      *
  151.      * @throws \Exception
  152.      */
  153.     public function applyVoucherAction(Request $requestTranslator $translatorFactory $ecommerceFactory)
  154.     {
  155.         if ($token strip_tags($request->get('voucher-code'))) {
  156.             $cart $this->getCart();
  157.             try {
  158.                 $success $cart->addVoucherToken($token);
  159.                 if ($success) {
  160.                     $this->addFlash('success'$translator->trans('cart.voucher-code-added'));
  161.                     $trackingManager $ecommerceFactory->getTrackingManager();
  162.                     $trackingManager->trackCartUpdate($cart);
  163.                 } else {
  164.                     $this->addFlash('danger'$translator->trans('cart.voucher-code-could-not-be-added'));
  165.                 }
  166.             } catch (VoucherServiceException $e) {
  167.                 $this->addFlash('danger'$translator->trans('cart.error-voucher-code-' $e->getCode()));
  168.             }
  169.         } else {
  170.             $this->addFlash('danger'$translator->trans('cart.empty-voucher-code'));
  171.         }
  172.         return $this->redirectToRoute('shop-cart-detail');
  173.     }
  174.     /**
  175.      * @Route("/cart/remove-voucher", name="shop-cart-remove-voucher")
  176.      *
  177.      * @param Request $request
  178.      * @param Translator $translator
  179.      * @param Factory $ecommerceFactory
  180.      *
  181.      * @return RedirectResponse
  182.      */
  183.     public function removeVoucherAction(Request $requestTranslator $translatorFactory $ecommerceFactory)
  184.     {
  185.         if ($token strip_tags($request->get('voucher-code'))) {
  186.             $cart $this->getCart();
  187.             try {
  188.                 $cart->removeVoucherToken($token);
  189.                 $this->addFlash('success'$translator->trans('cart.voucher-code-removed'));
  190.                 $trackingManager $ecommerceFactory->getTrackingManager();
  191.                 $trackingManager->trackCartUpdate($cart);
  192.             } catch (VoucherServiceException $e) {
  193.                 $this->addFlash('danger'$translator->trans('cart.error-voucher-code-' $e->getCode()));
  194.             }
  195.         } else {
  196.             $this->addFlash('danger'$translator->trans('cart.empty-voucher-code'));
  197.         }
  198.         return $this->redirectToRoute('shop-cart-detail');
  199.     }
  200. }