custom/plugins/IntediaDoofinderSW6/src/Core/Content/ProductExport/Subscriber/ProductExportSubscriber.php line 68

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Intedia\Doofinder\Core\Content\ProductExport\Subscriber;
  3. use Shopware\Core\Content\Category\CategoryDefinition;
  4. use Shopware\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Content\Category\Service\NavigationLoader;
  6. use Shopware\Core\Content\Category\Tree\Tree;
  7. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  8. use Shopware\Core\Content\Product\ProductEntity;
  9. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  10. use Shopware\Core\Content\ProductExport\Event\ProductExportProductCriteriaEvent;
  11. use Shopware\Core\Content\ProductExport\Event\ProductExportRenderBodyContextEvent;
  12. use Shopware\Core\Content\ProductExport\ProductExportEntity;
  13. use Shopware\Core\Content\ProductStream\Exception\NoFilterException;
  14. use Shopware\Core\Content\ProductStream\Service\ProductStreamBuilder;
  15. use Shopware\Core\Framework\Context;
  16. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  19. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class ProductExportSubscriber implements EventSubscriberInterface
  22. {
  23.     protected NavigationLoader $navigationLoader;
  24.     protected ProductStreamBuilder $productStreamBuilder;
  25.     protected EntityRepository $categoryRepository;
  26.     protected EntityRepository $productRepository;
  27.     protected ?Tree $categoryTree null;
  28.     protected array $productStreamCategories = [];
  29.     protected array $products = [];
  30.     /**
  31.      * @param NavigationLoader $navigationLoader
  32.      * @param ProductStreamBuilder $productStreamBuilder
  33.      * @param EntityRepository $categoryRepository
  34.      * @param EntityRepository $productRepository
  35.      */
  36.     public function __construct(NavigationLoader $navigationLoaderProductStreamBuilder $productStreamBuilderEntityRepository $categoryRepositoryEntityRepository $productRepository)
  37.     {
  38.         $this->navigationLoader     $navigationLoader;
  39.         $this->productStreamBuilder $productStreamBuilder;
  40.         $this->categoryRepository   $categoryRepository;
  41.         $this->productRepository    $productRepository;
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return [
  49.             ProductExportProductCriteriaEvent::class   => 'onProductExportCriteriaEvent',
  50.             ProductExportRenderBodyContextEvent::class => 'onProductExportRenderBodyContextEvent'
  51.         ];
  52.     }
  53.     /**
  54.      * @param ProductExportProductCriteriaEvent $event
  55.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  56.      */
  57.     public function onProductExportCriteriaEvent(ProductExportProductCriteriaEvent $event): void
  58.     {
  59.         $criteria      $event->getCriteria();
  60.         $productExport $event->getProductExport();
  61.         $context       $event->getSalesChannelContext();
  62.         // add data for DooFinderCategories
  63.         if ($this->usesDooFinderCategoriesExtension($productExport)) {
  64.             $this->addDooFinderCategoriesData($criteria$context);
  65.         }
  66.         if ($this->usesDooFinderGroupIdExtension($productExport)) {
  67.             $this->addDooFinderGroupIdData($criteria$context);
  68.         }
  69.     }
  70.     protected function addDooFinderGroupIdData(Criteria $criteriaSalesChannelContext $context): void
  71.     {
  72.         if ($context->getSalesChannel()) {
  73.             $this->products $this->getProducts($context);
  74.         }
  75.     }
  76.     /**
  77.      * @param Criteria $criteria
  78.      * @param SalesChannelContext $context
  79.      */
  80.     protected function addDooFinderCategoriesData(Criteria $criteriaSalesChannelContext $context): void
  81.     {
  82.         if (!$criteria->hasAssociation('categories')) {
  83.             $criteria->addAssociation('categories');
  84.         }
  85.         if ($context->getSalesChannel()) {
  86.             $navigationCategoryId $context->getSalesChannel()->getNavigationCategoryId();
  87.             $this->categoryTree   $this->navigationLoader->load($navigationCategoryId$context$navigationCategoryId10);
  88.             $this->buildProductStreamCategories($context);
  89.         }
  90.     }
  91.     protected function buildProductStreamCategories(SalesChannelContext $context)
  92.     {
  93.         foreach ($this->getCategoriesHavingStreams($context->getContext()) as $category)
  94.         {
  95.             foreach ($this->getProductIdsInStream($category$context) as $productId)
  96.             {
  97.                 if (!array_key_exists($productId$this->productStreamCategories)) {
  98.                     $this->productStreamCategories[$productId] = [];
  99.                 }
  100.                 $this->productStreamCategories[$productId][] = $category->getId();
  101.             }
  102.         }
  103.     }
  104.     /**
  105.      * @param ProductExportRenderBodyContextEvent $event
  106.      */
  107.     public function onProductExportRenderBodyContextEvent(ProductExportRenderBodyContextEvent $event): void
  108.     {
  109.         $context $event->getContext();
  110.         $context['groupIds'] = $this->products;
  111.         $context['categoryTree'] = $this->categoryTree;
  112.         $context['productStreamCategories'] = $this->productStreamCategories;
  113.         $event->setContext($context);
  114.     }
  115.     protected function getProductIdsInStream(CategoryEntity $categorySalesChannelContext $salesChannelContext): array
  116.     {
  117.         if (is_null($category->getProductStreamId())) {
  118.             return [];
  119.         }
  120.         try {
  121.             $filters $this->productStreamBuilder->buildFilters($category->getProductStreamId(), $salesChannelContext->getContext());
  122.             $criteria = new Criteria();
  123.             $criteria->addFilter(...$filters);
  124.             $criteria->addFilter(
  125.                 new ProductAvailableFilter($salesChannelContext->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_ALL)
  126.             );
  127.             return $this->productRepository->searchIds($criteria$salesChannelContext->getContext())->getIds();
  128.         }
  129.         catch (NoFilterException $exception) {
  130.         }
  131.         return [];
  132.     }
  133.     protected function getCategoriesHavingStreams(Context $context)
  134.     {
  135.         $criteria = new Criteria();
  136.         $criteria->addFilter(new EqualsFilter('productAssignmentType'CategoryDefinition::PRODUCT_ASSIGNMENT_TYPE_PRODUCT_STREAM));
  137.         return $this->categoryRepository->search($criteria$context)->getEntities();
  138.     }
  139.     protected function usesDooFinderCategoriesExtension(ProductExportEntity $productExport): bool
  140.     {
  141.         return strpos($productExport->getBodyTemplate(), 'doofinderCategories') !== false;
  142.     }
  143.     protected function usesDooFinderGroupIdExtension(ProductExportEntity $productExport): bool
  144.     {
  145.         return strpos($productExport->getBodyTemplate(), 'doofinderGroupId') !== false;
  146.     }
  147.     protected function getProducts($context): array
  148.     {
  149.         $criteria = new Criteria();
  150.         $criteria->addAssociation('children');
  151.         $products $this->productRepository->search($criteriaContext::createDefaultContext());
  152.         $productArray = [];
  153.         /** @var ProductEntity $product */
  154.         foreach ($products as $product) {
  155.             $productArray[$product->getId()] = $product->getProductNumber();
  156.         }
  157.         return $productArray;
  158.     }
  159. }