src/Controller/CartController.php line 128

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