src/Controller/Shop/AddItemToCartController.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Shop;
  4. use App\Entity\Order\OrderItem;
  5. use App\Entity\Product\ProductVariant;
  6. use Brille24\SyliusTierPricePlugin\Entity\TierPrice;
  7. use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
  8. use Sylius\Bundle\OrderBundle\Controller\OrderItemController;
  9. use Sylius\Component\Order\CartActions;
  10. use Sylius\Component\Order\Model\OrderItemInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\HttpException;
  14. class AddItemToCartController extends OrderItemController
  15. {
  16.     public function addAction(Request $request): Response
  17.     {
  18.         $cart $this->getCurrentCart();
  19.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  20.         $this->isGrantedOr403($configurationCartActions::ADD);
  21.         /** @var OrderItemInterface $orderItem */
  22.         $orderItem $this->newResourceFactory->create($configuration$this->factory);
  23.         $this->getQuantityModifier()->modify($orderItem1);
  24.         $form $this->getFormFactory()->create(
  25.             $configuration->getFormType(),
  26.             $this->createAddToCartCommand($cart$orderItem),
  27.             $configuration->getFormOptions()
  28.         );
  29.         if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  30.             /** @var AddToCartCommandInterface $addToCartCommand */
  31.             $addToCartCommand $form->getData();
  32.             $errors $this->getCartItemErrors($addToCartCommand->getCartItem());
  33.             if (count($errors)) {
  34.                 $form $this->getAddToCartFormWithErrors($errors$form);
  35.                 return $this->handleBadAjaxRequestView($configuration$form);
  36.             }
  37.             $event $this->eventDispatcher->dispatchPreEvent(CartActions::ADD$configuration$orderItem);
  38.             if ($event->isStopped() && !$configuration->isHtmlRequest()) {
  39.                 throw new HttpException($event->getErrorCode(), $event->getMessage());
  40.             }
  41.             if ($event->isStopped()) {
  42.                 $this->flashHelper->addFlashFromEvent($configuration$event);
  43.                 return $this->redirectHandler->redirectToIndex($configuration$orderItem);
  44.             }
  45.             $this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
  46.             $cartManager $this->getCartManager();
  47.             $cartManager->persist($cart);
  48.             $cartManager->flush();
  49.             $resourceControllerEvent $this->eventDispatcher->dispatchPostEvent(CartActions::ADD$configuration$orderItem);
  50.             if ($resourceControllerEvent->hasResponse()) {
  51.                 return $resourceControllerEvent->getResponse();
  52.             }
  53.             /** @var ProductVariant $variant */
  54.             $variant $addToCartCommand->getCartItem()->getVariant();
  55.             $source_path '//placehold.it/400x300';
  56.             /** @var OrderItem $orderItem */
  57.             $orderItem $addToCartCommand->getCartItem();
  58.             if($orderItem->getConfiguration() && $orderItem->getConfiguration()->getImageRenderingPath()){
  59.                 $source_path $orderItem->getConfiguration()->getImageRenderingPath();
  60.             }elseif($variant->getImages()->first() != false) {
  61.                 $arrayImages $variant->getProduct()->getImagesWithoutOneType()->toArray();
  62.                 usort($arrayImages, function ($a,$b){
  63.                     return $a->getPosition() - $b->getPosition();
  64.                 });
  65.                 $source_path $arrayImages[0]->getPath();
  66.             } else {
  67.                 if($variant->getProduct()->getImages()->first() != false) {
  68.                     $arrayImages $variant->getProduct()->getImagesWithoutOneType()->toArray();
  69.                     usort($arrayImages, function ($a,$b){
  70.                         return $a->getPosition() - $b->getPosition();
  71.                     });
  72.                     $source_path $arrayImages[0]->getPath();
  73.                 }
  74.             }
  75.             $cacheManager $this->container->get('liip_imagine.cache.manager');
  76.             $path $cacheManager->getBrowserPath(parse_url($source_pathPHP_URL_PATH), 'sylius_shop_product_large_thumbnail_inset', [], null);
  77.             $htmlOptions '';
  78.             foreach ($variant->getOptionValues() as $optionValue) {
  79.                 $htmlOptions .= '<div>
  80.                                  <small><strong class="visible-devis">'.$optionValue->getName() .' : </strong>'$optionValue->getValue() .'</small>
  81.                                  </div>';
  82.             }
  83.             $quantityAdd $addToCartCommand->getCartItem()->getQuantity();
  84.             if ($variant->getName() !== null) {
  85.                 $name $variant->getName();
  86.             }else {
  87.                 $name $variant->getProduct()->getName();
  88.             }
  89.             $moneyFormatter $this->container->get('sylius.money_formatter');
  90.             $localeContext $this->container->get('sylius.context.locale');
  91.             $channelContext $this->container->get('sylius.context.channel');
  92.             $zone $channelContext->getChannel()->getDefaultTaxZone();
  93.             $taxResolver $this->container->get('sylius.tax_rate_resolver');
  94.             $taxCalculator $this->container->get('sylius.tax_calculator');
  95.             $taxRate $taxResolver->resolve($variant, ['zone' => $zone]);
  96.             $price $variant->getChannelPricingForChannel($channelContext->getChannel())->getPrice();
  97.             $possibleTierPrices $this->manager->getRepository(TierPrice::class)->getSortedTierPrices($variant$channelContext->getChannel(), null);
  98.             $cheapestTierPrice null;
  99.             foreach ($possibleTierPrices as $tierPrice) {
  100.                 if ($tierPrice->getQty() > $quantityAdd) {
  101.                     break;
  102.                 }
  103.                 $cheapestTierPrice $tierPrice;
  104.             }
  105.             if ($cheapestTierPrice) {
  106.                 $price $cheapestTierPrice->getPrice();
  107.             }
  108.             if ($taxRate) {
  109.                 $totalTaxAmount $taxCalculator->calculate($price$taxRate);
  110.                 $price $price $totalTaxAmount;
  111.             }
  112.             $priceConvert $moneyFormatter->format((int) $price$cart->getCurrencyCode(), $localeContext->getLocaleCode());
  113.             $responseJson = [ 'variantName' => $name,
  114.                 'source_path' => $path'options' => $htmlOptions'quantity' => $quantityAdd'price' => $priceConvert ];
  115.             //return avec popup
  116.             return new Response(json_encode($responseJson));
  117.         }
  118.         if (!$configuration->isHtmlRequest()) {
  119.             return $this->handleBadAjaxRequestView($configuration$form);
  120.         }
  121.         return $this->render(
  122.             $configuration->getTemplate(CartActions::ADD '.html'),
  123.             [
  124.                 'configuration' => $configuration,
  125.                 $this->metadata->getName() => $orderItem,
  126.                 'form' => $form->createView(),
  127.             ]
  128.         );
  129.     }
  130. }