src/Controller/Shop/AddItemToCartController.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Shop;
  4. use App\Entity\Product\ProductVariant;
  5. use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
  6. use Sylius\Bundle\OrderBundle\Controller\OrderItemController;
  7. use Sylius\Component\Order\CartActions;
  8. use Sylius\Component\Order\Model\OrderItemInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\Exception\HttpException;
  12. class AddItemToCartController extends OrderItemController
  13. {
  14.     public function addAction(Request $request): Response
  15.     {
  16.         $cart $this->getCurrentCart();
  17.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  18.         $this->isGrantedOr403($configurationCartActions::ADD);
  19.         /** @var OrderItemInterface $orderItem */
  20.         $orderItem $this->newResourceFactory->create($configuration$this->factory);
  21.         $orderItem->setPurchasePrice($orderItem->getVariant()->getPurchasePrice());
  22.         $this->getQuantityModifier()->modify($orderItem1);
  23.         $form $this->getFormFactory()->create(
  24.             $configuration->getFormType(),
  25.             $this->createAddToCartCommand($cart$orderItem),
  26.             $configuration->getFormOptions()
  27.         );
  28.         if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  29.             /** @var AddToCartCommandInterface $addToCartCommand */
  30.             $addToCartCommand $form->getData();
  31.             $errors $this->getCartItemErrors($addToCartCommand->getCartItem());
  32.             if (count($errors)) {
  33.                 $form $this->getAddToCartFormWithErrors($errors$form);
  34.                 return $this->handleBadAjaxRequestView($configuration$form);
  35.             }
  36.             $event $this->eventDispatcher->dispatchPreEvent(CartActions::ADD$configuration$orderItem);
  37.             if ($event->isStopped() && !$configuration->isHtmlRequest()) {
  38.                 throw new HttpException($event->getErrorCode(), $event->getMessage());
  39.             }
  40.             if ($event->isStopped()) {
  41.                 $this->flashHelper->addFlashFromEvent($configuration$event);
  42.                 return $this->redirectHandler->redirectToIndex($configuration$orderItem);
  43.             }
  44.             $this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
  45.             $cartManager $this->getCartManager();
  46.             $cartManager->persist($cart);
  47.             $cartManager->flush();
  48.             $resourceControllerEvent $this->eventDispatcher->dispatchPostEvent(CartActions::ADD$configuration$orderItem);
  49.             if ($resourceControllerEvent->hasResponse()) {
  50.                 return $resourceControllerEvent->getResponse();
  51.             }
  52.             /** @var ProductVariant $variant */
  53.             $variant $addToCartCommand->getCartItem()->getVariant();
  54.             $source_path '//placehold.it/400x300';
  55.             if ($variant->getImages()->first() != false) {
  56.                 $source_path $variant->getImages()->first()->getPath();
  57.             } else {
  58.                 if ($variant->getProduct()->getImages()->first() != false) {
  59.                     $source_path $variant->getProduct()->getImages()->first()->getPath();
  60.                 }
  61.             }
  62.             $cacheManager $this->container->get('liip_imagine.cache.manager');
  63.             $path $cacheManager->getBrowserPath(parse_url($source_path\PHP_URL_PATH), 'sylius_shop_product_large_thumbnail', [], null);
  64.             $htmlOptions '';
  65.             foreach ($variant->getOptionValues() as $optionValue) {
  66.                 $htmlOptions .= '<div>
  67.                                  <small><strong class="visible-devis">' $optionValue->getName() . ' : </strong>' $optionValue->getValue() . '</small>
  68.                                  </div>';
  69.             }
  70.             $quantityAdd $addToCartCommand->getCartItem()->getQuantity();
  71.             if ($variant->getName() !== null) {
  72.                 $name $variant->getName();
  73.             } else {
  74.                 $name $variant->getProduct()->getName();
  75.             }
  76.             $moneyFormatter $this->container->get('sylius.money_formatter');
  77.             $localeContext $this->container->get('sylius.context.locale');
  78.             $channelContext $this->container->get('sylius.context.channel');
  79.             $productVariantPriceCalculator $this->container->get('sylius.calculator.product_variant_price');
  80.             $price $productVariantPriceCalculator->calculate($variant, ['channel' => $channelContext->getChannel(),  'quantity' => $orderItem->getQuantity(), 'customer' => $cart->getCustomer()]);
  81.             $priceConvert $moneyFormatter->format($price$cart->getCurrencyCode(), $localeContext->getLocaleCode());
  82.             //partie datalayer
  83.             $gtmEventListener $this->container->get('app.google_tag_manager.listener.event');
  84.             $datalayer $gtmEventListener->addToCart($orderItem->getProduct());
  85.             $responseJson = ['variantName' => $name,
  86.                 'source_path' => $path'options' => $htmlOptions'quantity' => $quantityAdd'price' => $priceConvert'dataLayer' => $datalayer, ];
  87.             // return avec popup
  88.             return new Response(json_encode($responseJson));
  89.         }
  90.         if (!$configuration->isHtmlRequest()) {
  91.             return $this->handleBadAjaxRequestView($configuration$form);
  92.         }
  93.         return $this->render(
  94.             $configuration->getTemplate(CartActions::ADD '.html'),
  95.             [
  96.                 'configuration' => $configuration,
  97.                 $this->metadata->getName() => $orderItem,
  98.                 'form' => $form->createView(),
  99.             ]
  100.         );
  101.     }
  102. }