src/Controller/CartController.php line 115

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