src/Controller/CustomOrderItemController.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. // use Sylius\Component\Order\Model\OrderItem;
  5. use App\Entity\Order\OrderItem;
  6. use App\Entity\Product\Product;
  7. use FOS\RestBundle\View\View;
  8. use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
  9. use Sylius\Bundle\OrderBundle\Controller\OrderItemController;
  10. use Sylius\Component\Core\Model\ProductVariant;
  11. use Sylius\Component\Core\Model\TaxonInterface;
  12. use Sylius\Component\Order\CartActions;
  13. use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Exception\HttpException;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use function count;
  20. use function gettype;
  21. class CustomOrderItemController extends OrderItemController
  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 OrderItem $orderItem */
  29.         $orderItem $this->newResourceFactory->create($configuration$this->factory);
  30.         /** @var OrderItemQuantityModifierInterface $orderItemQuantityModifierInterface */
  31.         $orderItemQuantityModifierInterface $this->getQuantityModifier();
  32.         $orderItemQuantityModifierInterface->modify($orderItem1);
  33.         $formType $configuration->getFormType();
  34.         if ($formType === null) {
  35.             $jsonResponse['message'] = 'cannot get formType';
  36.             return new JsonResponse($jsonResponse);
  37.         }
  38.         $form $this->getFormFactory()->create(
  39.             $formType,
  40.             $this->createAddToCartCommand($cart$orderItem),
  41.             $configuration->getFormOptions(),
  42.         );
  43.         if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  44.             /** @var AddToCartCommandInterface $addToCartCommand */
  45.             $addToCartCommand $form->getData();
  46.             $errors $this->getCartItemErrors($addToCartCommand->getCartItem());
  47.             if (count($errors)) {
  48.                 $form $this->getAddToCartFormWithErrors($errors$form);
  49.                 return $this->handleBadAjaxRequestView($configuration$form);
  50.             }
  51.             $event $this->eventDispatcher->dispatchPreEvent(CartActions::ADD$configuration$orderItem);
  52.             if ($event->isStopped() && ! $configuration->isHtmlRequest()) {
  53.                 throw new HttpException($event->getErrorCode(), $event->getMessage());
  54.             }
  55.             if ($event->isStopped()) {
  56.                 $this->flashHelper->addFlashFromEvent($configuration$event);
  57.                 return $this->redirectHandler->redirectToIndex($configuration$orderItem);
  58.             }
  59.             $itemsInCart $cart->getItems();
  60.             $allreadyInCart false;
  61.             /** @var OrderItem $cartOrderItem */
  62.             $cartOrderItem $addToCartCommand->getCartItem();
  63.             if ($cartOrderItem !== null) {
  64.                 /** @var ProductVariant $productVarriant */
  65.                 $productVarriant $cartOrderItem->getVariant();
  66.                 /** @var Product $product */
  67.                 $product $cartOrderItem->getProduct();
  68.                 $toAdd $product->getId();
  69.                 //vérification s'il s'agit d'un pack ou histoire
  70.                 // $type = $cartOrderItem->getProduct();
  71.                 $catalogue false;
  72.                 /** @var TaxonInterface $taxon */
  73.                 foreach ($product->getTaxons() as $taxon) {
  74.                     if ($taxon->getCode() === 'storyteller') {
  75.                         continue;
  76.                     }
  77.                     $catalogue true;
  78.                 }
  79.                 if ($catalogue === true) {
  80.                      /** @var OrderItem $item */
  81.                     foreach ($itemsInCart as $item) {
  82.                         if ($item->getProduct() !== null) {
  83.                             if ($item->getProduct()->getId() !== $toAdd) {
  84.                                 continue;
  85.                             }
  86.                         }
  87.                         $allreadyInCart true;
  88.                     }
  89.                 }
  90.             }
  91.             if ($allreadyInCart === false) {
  92.                 $this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
  93.                 $cartManager $this->getCartManager();
  94.                 $cartManager->persist($cart);
  95.                 $cartManager->flush();
  96.                 $resourceControllerEvent $this->eventDispatcher->dispatchPostEvent(CartActions::ADD$configuration$orderItem);
  97.                 if ($resourceControllerEvent->hasResponse()) {
  98.                     $jsonResponse['message'] = 'Error';
  99.                     return $resourceControllerEvent->getResponse() ??  new JsonResponse($jsonResponse);
  100.                 }
  101.                 $this->flashHelper->addSuccessFlash($configurationCartActions::ADD$orderItem);
  102.             } else {
  103.                 $this->addFlash('error'$this->getTranslator()->trans('sylius.pages.content.item_already_in_cart'));
  104.             }
  105.             if ($request->isXmlHttpRequest()) {
  106.                 if ($this->viewHandler !== null) {
  107.                     return $this->viewHandler->handle($configurationView::create([], Response::HTTP_CREATED));
  108.                 }
  109.             }
  110.             return $this->redirectHandler->redirectToResource($configuration$orderItem);
  111.         }
  112.         if (! $configuration->isHtmlRequest()) {
  113.             return $this->handleBadAjaxRequestView($configuration$form);
  114.         }
  115.         if (gettype($configuration->getTemplate(CartActions::ADD '.html')) === 'string') {
  116.             return $this->render(
  117.                 $configuration->getTemplate(CartActions::ADD '.html'),
  118.                 [
  119.                     'configuration' => $configuration,
  120.                     $this->metadata->getName() => $orderItem,
  121.                     'form' => $form->createView(),
  122.                 ],
  123.             );
  124.         }
  125.         $jsonResponse['message'] = 'Error';
  126.         return new JsonResponse($jsonResponse);
  127.     }
  128.     private function getTranslator(): TranslatorInterface
  129.     {
  130.         /** @var TranslatorInterface $translator */
  131.         $translator $this->get('translator');
  132.         return $translator;
  133.     }
  134. }