src/Ecommerce/Controller/CartController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Ecommerce\Controller;
  3. use App\Helpers\HelperFunctions;
  4. use CoreShop\Bundle\OrderBundle\DTO\AddToCartInterface;
  5. use CoreShop\Bundle\OrderBundle\Form\Type\AddToCartType;
  6. use CoreShop\Bundle\OrderBundle\Form\Type\CartType;
  7. use CoreShop\Component\Order\Model\PurchasableInterface;
  8. use CoreShop\Component\Tracking\Tracker\TrackerInterface;
  9. use Exception;
  10. use Pimcore\Model\DataObject\CoreShopCustomer;
  11. use Pimcore\Model\DataObject\CoreShopOrderItem;
  12. use Pimcore\Model\Site;
  13. use Pimcore\Twig\Extension\Templating\Placeholder;
  14. use Symfony\Component\Form\FormError;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use CoreShop\Bundle\FrontendBundle\Controller\CartController as BaseCartController;
  20. class CartController extends BaseCartController
  21. {
  22.     public function __construct(protected Placeholder $placeholder)
  23.     {
  24.         if (Site::isSiteRequest()) {
  25.             throw $this->createNotFoundException('Page not found!');
  26.         }
  27.     }
  28.     protected function mustLogin(bool $ajax false): Response
  29.     {
  30.         if ($ajax) {
  31.             return new JsonResponse(['success' => false]);
  32.         }
  33.         $this->addFlash('warning''Kérlek jelentkezz be a kosár használatához!');
  34.         return $this->redirectToRoute('profil');
  35.     }
  36.     public function summaryAction(Request $request): Response
  37.     {
  38.         if (!$this->getUser()) {
  39.             return $this->mustLogin();
  40.         }
  41.         $bc = [
  42.             [
  43.                 'title' => 'Főoldal',
  44.                 'path' => '/'
  45.             ],
  46.             [
  47.                 'title' => 'Kosár',
  48.                 'path' => $this->generateUrl('coreshop_checkout', ['stepIdentifier' => 'customer'])
  49.             ]
  50.         ];
  51.         $pl $this->placeholder;
  52.         $pl('breadcrumbNews')->set($bc);
  53.         $this->checkFixCart();
  54.         return parent::summaryAction($request);
  55.     }
  56.     public function addItemAction(Request $request): Response
  57.     {
  58.         if (!$this->getUser()) {
  59.             return $this->mustLogin($request->isXmlHttpRequest());
  60.         }
  61.         $this->denyAccessUnlessGranted('CORESHOP_CART');
  62.         $this->denyAccessUnlessGranted('CORESHOP_CART_ADD_ITEM');
  63.         $redirect $this->getParameterFromRequest($request'_redirect'$this->generateUrl('coreshop_index'));
  64.         $product $this->get('coreshop.repository.stack.purchasable')->find($this->getParameterFromRequest($request'product'));
  65.         if (!$product instanceof PurchasableInterface) {
  66.             if ($request->isXmlHttpRequest()) {
  67.                 return new JsonResponse([
  68.                     'success' => false,
  69.                 ]);
  70.             }
  71.             return $this->redirect($redirect);
  72.         }
  73.         $cartItem $this->get('coreshop.factory.order_item')->createWithPurchasable($product);
  74.         $this->getQuantityModifer()->modify($cartItem1);
  75.         $addToCart $this->createAddToCart($this->getCart(), $cartItem);
  76.         $form $this->get('form.factory')->createNamed('coreshop-' $product->getId(), AddToCartType::class, $addToCart);
  77.         if ($request->isMethod('POST')) {
  78.             $redirect $this->getParameterFromRequest($request'_redirect'$this->generateUrl('coreshop_cart_summary'));
  79.             $form->handleRequest($request);
  80.             if ($form->isSubmitted() && $form->isValid()) {
  81.                 /**
  82.                  * @var AddToCartInterface $addToCart
  83.                  */
  84.                 $addToCart $form->getData();
  85.                 $this->getCartModifier()->addToList($addToCart->getCart(), $addToCart->getCartItem());
  86.                 $this->getCartManager()->persistCart($this->getCart());
  87.                 $this->get(TrackerInterface::class)->trackCartAdd(
  88.                     $addToCart->getCart(),
  89.                     $addToCart->getCartItem()->getProduct(),
  90.                     $addToCart->getCartItem()->getQuantity(),
  91.                 );
  92.                 $this->addFlash('success'$this->get('translator')->trans('coreshop.ui.item_added'));
  93.                 if ($request->isXmlHttpRequest()) {
  94.                     return new JsonResponse([
  95.                         'success' => true,
  96.                     ]);
  97.                 }
  98.                 return $this->redirect($redirect);
  99.             }
  100.             foreach ($form->getErrors(truetrue) as $error) {
  101.                 $this->addFlash('error'$error->getMessage());
  102.             }
  103.             if ($request->isXmlHttpRequest()) {
  104.                 return new JsonResponse([
  105.                     'success' => false,
  106.                     'errors' => array_map(function (FormError $error) {
  107.                         return $error->getMessage();
  108.                     }, iterator_to_array($form->getErrors(true))),
  109.                 ]);
  110.             }
  111.             return $this->redirect($redirect);
  112.         }
  113.         if ($request->isXmlHttpRequest()) {
  114.             return new JsonResponse([
  115.                 'success' => false,
  116.             ]);
  117.         }
  118.         return $this->render(
  119.             $this->getParameterFromRequest($request'template'$this->templateConfigurator->findTemplate('Product/_addToCart.html')),
  120.             [
  121.                 'form' => $form->createView(),
  122.                 'product' => $product,
  123.                 '_redirect' => $redirect
  124.             ],
  125.         );
  126.     }
  127.     /**
  128.      * @Route("/cart-add-item", name="cart_add_item", methods={"POST"})
  129.      */
  130.     public function cartAddItemAction(Request $request): Response
  131.     {
  132.         if (!$this->getUser()) {
  133.             return $this->mustLogin($request->isXmlHttpRequest());
  134.         }
  135.         $id = (int)$request->request->get('id');
  136.         if (!$id) {
  137.             return $this->errorResponse();
  138.         }
  139.         $this->denyAccessUnlessGranted('CORESHOP_CART');
  140.         $this->denyAccessUnlessGranted('CORESHOP_CART_ADD_ITEM');
  141.         $product $this->get('coreshop.repository.stack.purchasable')->find($id);
  142.         if (!$product instanceof PurchasableInterface) {
  143.             return $this->errorResponse();
  144.         }
  145.         $this->checkFixCart();
  146.         $cartItem $this->get('coreshop.factory.order_item')->createWithPurchasable($product);
  147.         $this->getQuantityModifer()->modify($cartItem1);
  148.         $addToCart $this->createAddToCart($this->getCart(), $cartItem);
  149.         $this->getCartModifier()->addToList($addToCart->getCart(), $addToCart->getCartItem());
  150.         try {
  151.             $this->getCartManager()->persistCart($this->getCart());
  152.         } catch (Exception $e) {
  153.             return new JsonResponse('error');
  154.         }
  155.         $this->get(TrackerInterface::class)->trackCartAdd(
  156.             $addToCart->getCart(),
  157.             $addToCart->getCartItem()->getProduct(),
  158.             $addToCart->getCartItem()->getQuantity(),
  159.         );
  160.         return $this->successResponse($request);
  161.     }
  162.     /**
  163.      * @Route("/cart-remove-item", name="cart_remove_item", methods={"POST"})
  164.      */
  165.     public function cartRemoveItemAction(Request $request): Response
  166.     {
  167.         if (!$this->getUser()) {
  168.             return $this->mustLogin($request->isXmlHttpRequest());
  169.         }
  170.         $id = (int)$request->request->get('id');
  171.         try {
  172.             $cartItem $this->getRemoveItem($id);
  173.         } catch (Exception $e) {
  174.             return $this->errorResponse();
  175.         }
  176.         if ($cartItem->getQuantity() <= 1) {
  177.             return $this->cartDeleteItemAction($request$cartItem);
  178.         }
  179.         $product $this->get('coreshop.repository.stack.purchasable')->find($id);
  180.         if (!$product instanceof PurchasableInterface) {
  181.             return $this->errorResponse();
  182.         }
  183.         $this->checkFixCart();
  184.         $cartItem $this->get('coreshop.factory.order_item')->createWithPurchasable($product);
  185.         $cartItem->setQuantity(-1);
  186.         $addToCart $this->createAddToCart($this->getCart(), $cartItem);
  187.         $this->getCartModifier()->addToList($addToCart->getCart(), $addToCart->getCartItem());
  188.         $this->getCartManager()->persistCart($this->getCart());
  189.         $this->get(TrackerInterface::class)->trackCartRemove(
  190.             $addToCart->getCart(),
  191.             $addToCart->getCartItem()->getProduct(),
  192.             $addToCart->getCartItem()->getQuantity(),
  193.         );
  194.         return $this->successResponse($request);
  195.     }
  196.     /**
  197.      * @Route("/cart-delete-item", name="cart_delete_item", methods={"POST"})
  198.      */
  199.     public function cartDeleteItemAction(Request $request, ?CoreShopOrderItem $cartItem null): Response
  200.     {
  201.         if (!$this->getUser()) {
  202.             return $this->mustLogin($request->isXmlHttpRequest());
  203.         }
  204.         if (!$cartItem instanceof CoreShopOrderItem) {
  205.             try {
  206.                 $cartItem $this->getRemoveItem((int)$request->request->get('id'));
  207.             } catch (Exception $e) {
  208.                 return $this->errorResponse();
  209.             }
  210.         }
  211.         $this->checkFixCart();
  212.         $this->getCartModifier()->removeFromList($this->getCart(), $cartItem);
  213.         $this->getCartManager()->persistCart($this->getCart());
  214.         $this->get(TrackerInterface::class)->trackCartRemove($this->getCart(), $cartItem->getProduct(), $cartItem->getQuantity());
  215.         $data = [];
  216.         if (filter_var($request->request->get('fromCheckoutPage'false), FILTER_VALIDATE_BOOLEAN)) {
  217.             $data['cart'] = $this->render('seikoboutique/templates/bundles/CoreShopFrontendBundle/Cart/Summary/_checkout_cart_table.html.twig', [
  218.                 'cart' => $this->getCart(),
  219.                 'form' => $this->get('form.factory')->createNamed('coreshop'CartType::class, $this->getCart())->createView()
  220.             ])->getContent();
  221.         }
  222.         return $this->successResponse($request$data);
  223.     }
  224.     private function errorResponse(): JsonResponse
  225.     {
  226.         return new JsonResponse(['success' => false]);
  227.     }
  228.     private function successResponse(Request $request, array $data = []): JsonResponse
  229.     {
  230.         $count 0;
  231.         if ($this->getCart()->getItems()) {
  232.             foreach ($this->getCart()->getItems() as $item) {
  233.                 $count += $item->getQuantity();
  234.             }
  235.         }
  236.         return new JsonResponse([
  237.             'success' => true,
  238.             'count' => (int)$count,
  239.             'miniCart' => $this->miniCartAction($request)->getContent(),
  240.             ...$data
  241.         ]);
  242.     }
  243.     /**
  244.      * @throws Exception
  245.      */
  246.     private function getRemoveItem($id): CoreShopOrderItem
  247.     {
  248.         if (!$id) {
  249.             throw new Exception('error');
  250.         }
  251.         $this->denyAccessUnlessGranted('CORESHOP_CART');
  252.         $this->denyAccessUnlessGranted('CORESHOP_CART_REMOVE_ITEM');
  253.         foreach ($this->getCart()->getItems() as $item) {
  254.             if ($item->getProduct()->getId() === $id) {
  255.                 return $item;
  256.             }
  257.         }
  258.         throw new Exception('error');
  259.     }
  260.     private function checkFixCart(): void
  261.     {
  262.         HelperFunctions::checkFixCart($this->getCart(), $this->container);
  263.     }
  264.     public function miniCartAction(): Response
  265.     {
  266.         return $this->render($this->templateConfigurator->findTemplate('Cart/_miniCartInner.html'), [
  267.             'cart' => $this->getCart(),
  268.         ]);
  269.     }
  270. }