src/Controller/OrderItemController.php line 28

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\Controller;
  12. use App\Entity\FreeOption\FreeOption;
  13. use FOS\RestBundle\View\View;
  14. use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
  15. use Sylius\Component\Order\CartActions;
  16. use Sylius\Component\Order\Model\OrderItemInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpKernel\Exception\HttpException;
  20. use Sylius\Bundle\OrderBundle\Controller\OrderItemController as BaseOrderItemController;
  21. class OrderItemController extends BaseOrderItemController
  22. {
  23.     public function addAction(Request $request): Response
  24.     {
  25.         $cart $this->getCurrentCart();
  26.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  27.         $this->isGrantedOr403($configurationCartActions::ADD);
  28.         /** @var OrderItemInterface $orderItem */
  29.         $orderItem $this->newResourceFactory->create($configuration$this->factory);
  30.         $this->getQuantityModifier()->modify($orderItem1);
  31.         $form $this->getFormFactory()->create(
  32.             $configuration->getFormType(),
  33.             $this->createAddToCartCommand($cart$orderItem),
  34.             $configuration->getFormOptions()
  35.         );
  36.         if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  37.             /** @var AddToCartCommandInterface $addToCartCommand */
  38.             $addToCartCommand $form->getData();
  39.             $request->getSession()->set('slug_product_taxon_referer'$orderItem->getProduct()->getSlug());
  40.             $errors $this->getCartItemErrors($addToCartCommand->getCartItem());
  41.             if (count($errors)) {
  42.                 $form $this->getAddToCartFormWithErrors($errors$form);
  43.                 return $this->handleBadAjaxRequestView($configuration$form);
  44.             }
  45.             $event $this->eventDispatcher->dispatchPreEvent(CartActions::ADD$configuration$orderItem);
  46.             if ($event->isStopped() && !$configuration->isHtmlRequest()) {
  47.                 throw new HttpException($event->getErrorCode(), $event->getMessage());
  48.             }
  49.             if ($event->isStopped()) {
  50.                 $this->flashHelper->addFlashFromEvent($configuration$event);
  51.                 return $this->redirectHandler->redirectToIndex($configuration$orderItem);
  52.             }
  53.             $freeOptions = [];
  54.             $freeOptionValueRepository $this->get('app.repository.free_option_value');
  55.             foreach ($request->request->all() as $key => $value) {
  56.                 if (str_starts_with($key'free_option_quantity_')) {
  57.                     $optionId str_replace('free_option_quantity_'''$key);
  58.                     $optionValueId $request->request->get('option_'.$optionId);
  59.                     if (null === $optionValueId) {
  60.                         continue;
  61.                     }
  62.                     /** @var FreeOption $freeOption */
  63.                     $freeOptionValue $freeOptionValueRepository->find($optionValueId);
  64.                     if ($freeOptionValue) {
  65.                         $price $freeOptionValue->getPrice();
  66.                         $freeOptions[] = ['id' => $optionValueId'option_id' => $optionId,'quantity' => $value'price' => $price'ref' => $freeOptionValue->getRef(), 'title' => $freeOptionValue->getTranslation()->getTitle(), 'option_title' => $freeOptionValue->getFreeOption()->getTranslation()->getTitle()];
  67.                     }
  68.                 }
  69.             }
  70.             if (!empty($freeOptions)) {
  71.                 $addToCartCommand->getCartItem()->setFreeOptions($freeOptions);
  72.             }
  73.             $this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
  74.             $cartManager $this->getCartManager();
  75.             $cartManager->persist($cart);
  76.             $cartManager->flush();
  77.             $resourceControllerEvent $this->eventDispatcher->dispatchPostEvent(CartActions::ADD$configuration$orderItem);
  78.             if ($resourceControllerEvent->hasResponse()) {
  79.                 return $resourceControllerEvent->getResponse();
  80.             }
  81.             $this->flashHelper->addSuccessFlash($configurationCartActions::ADD$orderItem);
  82.             if ($request->isXmlHttpRequest()) {
  83.                 return $this->viewHandler->handle($configurationView::create([], Response::HTTP_CREATED));
  84.             }
  85.             return $this->redirectHandler->redirectToResource($configuration$orderItem);
  86.         }
  87.         if (!$configuration->isHtmlRequest()) {
  88.             return $this->handleBadAjaxRequestView($configuration$form);
  89.         }
  90.         $view View::create()
  91.             ->setData([
  92.                 'configuration' => $configuration,
  93.                 $this->metadata->getName() => $orderItem,
  94.                 'form' => $form->createView(),
  95.             ])
  96.             ->setTemplate($configuration->getTemplate(CartActions::ADD '.html'))
  97.         ;
  98.         return $this->viewHandler->handle($configuration$view);
  99.     }
  100. }