src/Controller/CartController.php line 67

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")
  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.         $id $request->get('id');
  61.         $product AbstractProduct::getById($id);
  62.         if (null === $product) {
  63.             throw new \Exception('Product not found');
  64.         }
  65.         $cart $this->getCart();
  66.         if ($cart->getItemCount() > 99) {
  67.             throw new \Exception('Maximum Cart items limit Reached');
  68.         }
  69.         $cart->addItem($product1);
  70.         $cart->save();
  71.         $trackingManager $ecommerceFactory->getTrackingManager();
  72.         $trackingManager->trackCartProductActionAdd($cart$product);
  73.         $trackingManager->forwardTrackedCodesAsFlashMessage();
  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 Response
  84.      */
  85.     public function cartListingAction(
  86.         Request $request,
  87.         BreadcrumbHelperService $breadcrumbHelperService,
  88.         Factory $ecommerceFactory
  89.     )
  90.     {
  91.         $cart $this->getCart();
  92.         if ($request->getMethod() == Request::METHOD_POST) {
  93.             $this->handleCartListingActionPost($request$ecommerceFactory$cart);
  94.         }
  95.         $breadcrumbHelperService->enrichCartPage();
  96.         $params array_merge($request->request->all(), $request->query->all());
  97.         if ($cart->isEmpty()) {
  98.             return $this->render('cart/cart_empty.html.twig'array_merge($params, ['cart' => $cart]));
  99.         } else {
  100.             return $this->render('cart/cart_listing.html.twig'array_merge($params, ['cart' => $cart]));
  101.         }
  102.     }
  103.     private function handleCartListingActionPost(Request $requestFactory $ecommerceFactoryCartInterface $cart)
  104.     {
  105.         if (!$this->isCsrfTokenValid('cartListing'$request->get('_csrf_token'))) {
  106.             throw new AccessDeniedHttpException('Invalid request');
  107.         }
  108.         $items $request->get('items');
  109.         foreach ($items as $itemKey => $quantity) {
  110.             if (!is_numeric($quantity)) {
  111.                 continue;
  112.             }
  113.             if ($cart->getItemCount() > 99) {
  114.                 break;
  115.             }
  116.             $product AbstractProduct::getById($itemKey);
  117.             if ($product instanceof CheckoutableInterface) {
  118.                 $cart->updateItem($itemKey$productfloor($quantity), true);
  119.             }
  120.         }
  121.         $cart->save();
  122.         $trackingManager $ecommerceFactory->getTrackingManager();
  123.         $trackingManager->trackCartUpdate($cart);
  124.     }
  125.     /**
  126.      * @Route("/cart/remove-from-cart", name="shop-remove-from-cart")
  127.      *
  128.      * @param Request $request
  129.      * @param Factory $ecommerceFactory
  130.      *
  131.      * @return RedirectResponse
  132.      */
  133.     public function removeFromCartAction(Request $requestFactory $ecommerceFactory)
  134.     {
  135.         $id $request->get('id');
  136.         $product AbstractProduct::getById($id);
  137.         $cart $this->getCart();
  138.         $cart->removeItem($id);
  139.         $cart->save();
  140.         if ($product instanceof ProductInterface) {
  141.             $trackingManager $ecommerceFactory->getTrackingManager();
  142.             $trackingManager->trackCartProductActionRemove($cart$product);
  143.             $trackingManager->forwardTrackedCodesAsFlashMessage();
  144.         }
  145.         return $this->redirectToRoute('shop-cart-detail');
  146.     }
  147.     /**
  148.      * @Route("/cart/apply-voucher", name="shop-cart-apply-voucher")
  149.      *
  150.      * @param Request $request
  151.      * @param Translator $translator
  152.      * @param Factory $ecommerceFactory
  153.      *
  154.      * @return RedirectResponse
  155.      *
  156.      * @throws \Exception
  157.      */
  158.     public function applyVoucherAction(Request $requestTranslator $translatorFactory $ecommerceFactory)
  159.     {
  160.         if ($token strip_tags($request->get('voucher-code'))) {
  161.             $cart $this->getCart();
  162.             try {
  163.                 $success $cart->addVoucherToken($token);
  164.                 if ($success) {
  165.                     $this->addFlash('success'$translator->trans('cart.voucher-code-added'));
  166.                     $trackingManager $ecommerceFactory->getTrackingManager();
  167.                     $trackingManager->trackCartUpdate($cart);
  168.                 } else {
  169.                     $this->addFlash('danger'$translator->trans('cart.voucher-code-could-not-be-added'));
  170.                 }
  171.             } catch (VoucherServiceException $e) {
  172.                 $this->addFlash('danger'$translator->trans('cart.error-voucher-code-' $e->getCode()));
  173.             }
  174.         } else {
  175.             $this->addFlash('danger'$translator->trans('cart.empty-voucher-code'));
  176.         }
  177.         return $this->redirectToRoute('shop-cart-detail');
  178.     }
  179.     /**
  180.      * @Route("/cart/remove-voucher", name="shop-cart-remove-voucher")
  181.      *
  182.      * @param Request $request
  183.      * @param Translator $translator
  184.      * @param Factory $ecommerceFactory
  185.      *
  186.      * @return RedirectResponse
  187.      */
  188.     public function removeVoucherAction(Request $requestTranslator $translatorFactory $ecommerceFactory)
  189.     {
  190.         if ($token strip_tags($request->get('voucher-code'))) {
  191.             $cart $this->getCart();
  192.             try {
  193.                 $cart->removeVoucherToken($token);
  194.                 $this->addFlash('success'$translator->trans('cart.voucher-code-removed'));
  195.                 $trackingManager $ecommerceFactory->getTrackingManager();
  196.                 $trackingManager->trackCartUpdate($cart);
  197.             } catch (VoucherServiceException $e) {
  198.                 $this->addFlash('danger'$translator->trans('cart.error-voucher-code-' $e->getCode()));
  199.             }
  200.         } else {
  201.             $this->addFlash('danger'$translator->trans('cart.empty-voucher-code'));
  202.         }
  203.         return $this->redirectToRoute('shop-cart-detail');
  204.     }
  205. }