src/Controller/Shop/AddItemToCartController.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Shop;
  4. use App\Entity\Channel\ChannelPricing;
  5. use App\Entity\Configurator\ConfiguratorOption;
  6. use App\Entity\Order\Order;
  7. use App\Entity\Product\ProductVariant;
  8. use App\Provider\PriceTTCProvider;
  9. use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
  10. use Sylius\Bundle\OrderBundle\Controller\OrderItemController;
  11. use Sylius\Component\Order\CartActions;
  12. use Sylius\Component\Order\Model\OrderItemInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Exception\HttpException;
  16. class AddItemToCartController extends OrderItemController
  17. {
  18.     public function addAction(Request $request): Response
  19.     {
  20.         /** @var Order $cart */
  21.         $cart $this->getCurrentCart();
  22.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  23.         $this->isGrantedOr403($configurationCartActions::ADD);
  24.         /** @var OrderItemInterface $orderItem */
  25.         $orderItem $this->newResourceFactory->create($configuration$this->factory);
  26.         $this->getQuantityModifier()->modify($orderItem1);
  27.         $form $this->getFormFactory()->create(
  28.             $configuration->getFormType(),
  29.             $this->createAddToCartCommand($cart$orderItem),
  30.             $configuration->getFormOptions()
  31.         );
  32.         if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  33.             $moneyFormatter $this->container->get('sylius.money_formatter');
  34.             $em $this->getDoctrine()->getManager();
  35.             $configuratorOptionRepository $em->getRepository(ConfiguratorOption::class);
  36.             $criteria = [];
  37.             if ($request->request->get('demarches_administratives')) {
  38.                 $criteria[] = $request->request->get('demarches_administratives');
  39.             }
  40.             if ($request->request->get('installation_kit_solaire')) {
  41.                 $criteria[] = $request->request->get('installation_kit_solaire');
  42.             }
  43.             if ($request->request->get('panneaux_emplacement')) {
  44.                 $criteria[] = $request->request->get('panneaux_emplacement');
  45.             }
  46.             //todo : créer une méthode de repo qui récupère toutes les options comprise dans le tableau des codes $criteria
  47. //            $configuratorOptions = $configuratorOptionRepository->findByCodes($criteria);
  48.             /** @var AddToCartCommandInterface $addToCartCommand */
  49.             $addToCartCommand $form->getData();
  50.             /** @var ProductVariant $variant */
  51.             $variant $addToCartCommand->getCartItem()->getVariant();
  52.             /** @var ChannelPricing $pricing */
  53.             $price $variant->getChannelPricingForChannel($cart->getChannel())->getPrice();
  54.             $addToCartCommand->getCartItem()->setImmutable(true);
  55.             $addToCartCommand->getCartItem()->setUnitPrice($price);
  56.             $errors $this->getCartItemErrors($addToCartCommand->getCartItem());
  57.             if (count($errors)) {
  58.                 $form $this->getAddToCartFormWithErrors($errors$form);
  59.                 return $this->handleBadAjaxRequestView($configuration$form);
  60.             }
  61.             $event $this->eventDispatcher->dispatchPreEvent(CartActions::ADD$configuration$orderItem);
  62.             if ($event->isStopped() && !$configuration->isHtmlRequest()) {
  63.                 throw new HttpException($event->getErrorCode(), $event->getMessage());
  64.             }
  65.             if ($event->isStopped()) {
  66.                 $this->flashHelper->addFlashFromEvent($configuration$event);
  67.                 return $this->redirectHandler->redirectToIndex($configuration$orderItem);
  68.             }
  69.             $this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
  70.             $source_path '//placehold.it/400x300';
  71.             if($variant->getImages()->first() != false) {
  72.                 $source_path $variant->getImages()->first()->getPath();
  73.             }
  74.             else {
  75.                 if($variant->getProduct()->getImages()->first() != false) {
  76.                     $source_path $variant->getProduct()->getImages()->first()->getPath();
  77.                 }
  78.             }
  79.             $cacheManager $this->container->get('liip_imagine.cache.manager');
  80.             $path $cacheManager->getBrowserPath(parse_url($source_pathPHP_URL_PATH), 'sylius_shop_product_large_thumbnail', [], null);
  81.             $htmlOptions '';
  82.             foreach ($variant->getOptionValues() as $optionValue) {
  83.                 $htmlOptions .= '<div>
  84.                                  <small><strong class="visible-devis">'.$optionValue->getName() .' : </strong>'$optionValue->getValue() .'</small>
  85.                                  </div>';
  86.             }
  87.             $quantityAdd $addToCartCommand->getCartItem()->getQuantity();
  88.             if ($variant->getName() !== null) {
  89.                 $name $variant->getName();
  90.             }
  91.             else {
  92.                 $name $variant->getProduct()->getName();
  93.             }
  94.             //add tva
  95.             /** @var PriceTTCProvider $priceTTCProvider */
  96.             $priceTTCProvider $this->container->get('app.provider.price_ttc_provider');
  97.             $priceTTC $priceTTCProvider->getPriceTTCFromVariant($variant$pricefalse);
  98.             $priceConvert $moneyFormatter->format((int)$priceTTC$cart->getCurrencyCode(), $cart->getLocaleCode());
  99.             //partie datalayer
  100.             $gtmEventListener $this->container->get('app.google_tag_manager.listener.event');
  101.             $datalayer $gtmEventListener->addToCart($orderItem->getProduct());
  102.             $responseJson = [
  103.                 'variantName' => $name,
  104.                 'source_path' => $path,
  105.                 'options' => $htmlOptions,
  106.                 'quantity' => $quantityAdd,
  107.                 'price' => $priceConvert,
  108.                 'dataLayer' => $datalayer
  109.             ];
  110.             $cartManager $this->getCartManager();
  111.             $cartManager->persist($cart);
  112.             $cartManager->flush();
  113.             $resourceControllerEvent $this->eventDispatcher->dispatchPostEvent(CartActions::ADD$configuration$orderItem);
  114.             if ($resourceControllerEvent->hasResponse()) {
  115.                 return $resourceControllerEvent->getResponse();
  116.             }
  117.             return new Response(json_encode($responseJson));
  118.         }
  119.         if (!$configuration->isHtmlRequest()) {
  120.             return $this->handleBadAjaxRequestView($configuration$form);
  121.         }
  122.         return $this->render(
  123.             $configuration->getTemplate(CartActions::ADD '.html'),
  124.             [
  125.                 'configuration' => $configuration,
  126.                 $this->metadata->getName() => $orderItem,
  127.                 'form' => $form->createView(),
  128.             ]
  129.         );
  130.     }
  131. }