custom/plugins/WabsIgmTheme/src/Subscriber/NavigationSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. namespace Wabs\IgmTheme\Subscriber;
  3. use Acris\CustomerGroupAvailableProduct\Components\DisplayAllowedProductService;
  4. use Shopware\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Content\Category\Event\NavigationLoadedEvent;
  6. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  7. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Core\System\SystemConfig\SystemConfigService;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class NavigationSubscriber implements EventSubscriberInterface
  19. {
  20.     /** @var string[] */
  21.     protected $productCmsPageIds = [
  22.         '25b0681b674947948e62aad06eec6088',
  23.         'a5b1ecdd1e954364945534ec0b76af20',
  24.         '13c0e49bf179431895577ab23f07133b',
  25.         'e77093ea0970488696be0d8f0f86f858',
  26.         'bf7d1778b16f4576b92fd461025cfb8b',
  27.     ];
  28.     /** @var EntityRepositoryInterface */
  29.     protected $productRepository;
  30.     /** @var DisplayAllowedProductService */
  31.     protected $displayProductService;
  32.     /** @var SystemConfigService */
  33.     protected $configService;
  34.     public function __construct(
  35.         EntityRepositoryInterface $productRepository,
  36.         DisplayAllowedProductService $displayProductService,
  37.         SystemConfigService $configService
  38.     ) {
  39.         $this->productRepository $productRepository;
  40.         $this->displayProductService $displayProductService;
  41.         $this->configService $configService;
  42.     }
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return [
  46.             NavigationLoadedEvent::class => 'onNavigationLoaded',
  47.         ];
  48.     }
  49.     public function onNavigationLoaded(NavigationLoadedEvent $event)
  50.     {
  51.         $tree $event->getNavigation()->getTree();
  52.         foreach ($tree as $key => $item) {
  53.             $hasAvailableChildProductListings false;
  54.             $children $item->getChildren();
  55.             foreach ($children as $_key => $child) {
  56.                 $category $child->getCategory();
  57.                 $isProductPage in_array($category->getCmsPageId(), $this->productCmsPageIds);
  58.                 if ($isProductPage && $this->hasAvailableProductListings($category$event->getSalesChannelContext(),
  59.                         $event->getContext())) {
  60.                     $hasAvailableChildProductListings true;
  61.                 } else {
  62.                     unset($children[$_key]);
  63.                 }
  64.             }
  65.             $item->setChildren($children);
  66.             $category $item->getCategory();
  67.             $isProductPage in_array($category->getCmsPageId(), $this->productCmsPageIds);
  68.             if ($isProductPage && !$hasAvailableChildProductListings &&
  69.                 !$this->hasAvailableProductListings($category$event->getSalesChannelContext(),
  70.                     $event->getContext())) {
  71.                 unset($tree[$key]);
  72.             } else {
  73.                 $tree[$key] = $item;
  74.             }
  75.         }
  76.         $event->getNavigation()->setTree($tree);
  77.     }
  78.     protected function hasAvailableProductListings(
  79.         CategoryEntity $category,
  80.         SalesChannelContext $salesChannelContext,
  81.         Context $context
  82.     ) {
  83.         $criteria = new Criteria();
  84.         if ($category->getProductAssignmentType() == 'product_stream') {
  85.             $criteria->addAssociation('streams');
  86.             $criteria->addFilter(new EqualsFilter('streams.id'$category->getProductStreamId()));
  87.         } else {
  88.             $criteria->addFilter(new EqualsFilter('categories.id'$category->getId()));
  89.         }
  90.         $criteria->addFilter(new ProductAvailableFilter($salesChannelContext->getSalesChannel()->getId(),
  91.             ProductVisibilityDefinition::VISIBILITY_ALL));
  92.         $criteria->setLimit(1);
  93.         $this->addDisplayProductIdsForCriteria($criteria$salesChannelContext);
  94.         $productsCount $this->productRepository->search($criteria$context)->count();
  95.         return $productsCount 0;
  96.     }
  97.     private function addDisplayProductIdsForCriteria(Criteria $criteriaSalesChannelContext $salesChannelContext)
  98.     {
  99.         $displayProductIds $this->displayProductService->getDisplayProductIdsForCustomerGroupId(
  100.             $salesChannelContext->getCurrentCustomerGroup()->getId(),
  101.             $salesChannelContext->getContext());
  102.         if (empty($displayProductIds) === true) {
  103.             if ($this->configService->get('AcrisCustomerGroupAvailableProductCS.config.releaseProductsIfNoCustomerGroupAssigned',
  104.                     $salesChannelContext->getSalesChannel()->getId()) === DisplayAllowedProductService::DEFAULT_PLUGIN_CONFIG_RELEASE_PRODUCT_IF_NO_CUSTOMER_GROUPS_ASSIGNED) {
  105.                 $criteria->addAssociation('product.customerGroup');
  106.                 $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  107.                     new EqualsFilter('id'Uuid::randomHex()),
  108.                     new EqualsFilter('product.customerGroup.id'null),
  109.                 ]));
  110.                 return;
  111.             }
  112.             $criteria->addFilter(new EqualsFilter('id'Uuid::randomHex()));
  113.         } else {
  114.             $criteria->addFilter(new EqualsAnyFilter('id'$displayProductIds));
  115.         }
  116.     }
  117. }