custom/static-plugins/LDSCustom/src/Subscriber/Recipe.php line 43

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace LDSCustom\Subscriber;
  4. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Shopware\Core\Framework\Context;
  11. use Nds\RecipeManager\Core\Content\Recipe\RecipeEntity;
  12. use Shopware\Core\Content\Product\DataAbstractionLayer\CheapestPrice\CheapestPriceContainer;
  13. use Shopware\Core\Content\Product\SalesChannel\Price\AbstractProductPriceCalculator;
  14. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  16. use Shopware\Core\Framework\Feature;
  17. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  18. use LDSCustom\Core\Recipe\Extension\RecipeDataExtension;
  19. class Recipe implements EventSubscriberInterface
  20. {
  21.     private AbstractProductPriceCalculator $calculator;
  22.     private EntityRepository $recipeRepository;
  23.     private EntityRepository $productRepository;
  24.     public function __construct(AbstractProductPriceCalculator $calculatorEntityRepository $recipeRepositoryEntityRepository $productRepository)
  25.     {
  26.         $this->calculator $calculator;
  27.         $this->recipeRepository $recipeRepository;
  28.         $this->productRepository $productRepository;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             GenericPageLoadedEvent::class => 'onRecipePageLoaded',
  34.         ];
  35.     }
  36.     public function onRecipePageLoaded(GenericPageLoadedEvent $event): void
  37.     {
  38.         $salesChannelContext $event->getSalesChannelContext();
  39.         $page $event->getPage();
  40.         /** @var Context $context */
  41.         $context $salesChannelContext->getContext();
  42.         /** @var Request $request */
  43.         $request $event->getRequest();
  44.         $uri explode('/'$request->getRequestUri());
  45.         if (count($uri) < 3) {
  46.             return;
  47.         }
  48.         list(, $target$slug) = $uri;
  49.         if ($target !== 'rcp_recipe' || empty($slug)) {
  50.             return;
  51.         }
  52.         /** @var RecipeEntity $recipeProducts */
  53.         $products $this->getRecipeProductInfo($slug$context);
  54.         if (empty($products)) {
  55.             return;
  56.         }
  57.         $this->productSalesChannelLoaded($products$salesChannelContext);
  58.         $productExtension = new RecipeDataExtension();
  59.         $productExtension->assign([
  60.             'data' => $products,
  61.         ]);
  62.         $page->addExtension(RecipeDataExtension::EXTENSION_NAME$productExtension);
  63.     }
  64.     protected function getRecipeProductInfo(string $slugContext $context)
  65.     {
  66.         $criteria = new Criteria();
  67.         $criteria
  68.             ->addFilter(new EqualsFilter('slug'$slug))
  69.             ->addFilter(new EqualsFilter('active''1'))
  70.             ->addAssociations(
  71.                 [
  72.                     'ingredients',
  73.                     'ingredients.product',
  74.                 ]
  75.             );
  76.         $recipeProducts $this->recipeRepository->search($criteria$context)->first();
  77.         $ingredients $recipeProducts->getIngredients();
  78.         $products = [];
  79.         foreach ($ingredients as $ingredient) {
  80.             if ($ingredient->getProduct() !== null) {
  81.                 $products[] = $ingredient->getProduct();
  82.             }
  83.         }
  84.         if (empty($products)) {
  85.             return;
  86.         }
  87.         $return  = [];
  88.         foreach ($products as $product) {
  89.             $criteria = new Criteria();
  90.             $criteria
  91.                 ->addFilter(new EqualsFilter('id'$product->getId()))
  92.                 ->addFilter(new EqualsFilter('active''1'))
  93.                 ->addAssociations(
  94.                     [
  95.                         'prices',
  96.                         'deliveryTime',
  97.                         'cover',
  98.                         'productReviews',
  99.                     ]
  100.                 );
  101.             $return[] = $this->productRepository->search($criteria$context)->first();
  102.         }
  103.         if (empty($return)) {
  104.             return;
  105.         }
  106.         return $return;
  107.     }
  108.     /**
  109.      * @param Entity[] $elements
  110.      */
  111.     private function productSalesChannelLoaded(array $elementsSalesChannelContext $context): void
  112.     {
  113.         /** @var SalesChannelProductEntity $product */
  114.         foreach ($elements as $product) {
  115.             if (Feature::isActive('FEATURE_NEXT_16151')) {
  116.                 $price $product->get('cheapestPrice');
  117.                 if ($price instanceof CheapestPriceContainer) {
  118.                     $resolved $price->resolve($context->getContext());
  119.                     $product->assign([
  120.                         'cheapestPrice' => $resolved,
  121.                         'cheapestPriceContainer' => $price,
  122.                     ]);
  123.                 }
  124.             }
  125.         }
  126.         $this->calculator->calculate($elements$context);
  127.     }
  128. }