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

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.         '3d434a9a0746418ab9d4c17c5c61857b',
  28.     ];
  29.     /** @var EntityRepositoryInterface */
  30.     protected $productRepository;
  31.     /** @var DisplayAllowedProductService */
  32.     protected $displayProductService;
  33.     /** @var SystemConfigService */
  34.     protected $configService;
  35.     public function __construct(
  36.         EntityRepositoryInterface $productRepository,
  37.         DisplayAllowedProductService $displayProductService,
  38.         SystemConfigService $configService
  39.     ) {
  40.         $this->productRepository $productRepository;
  41.         $this->displayProductService $displayProductService;
  42.         $this->configService $configService;
  43.     }
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             NavigationLoadedEvent::class => 'onNavigationLoaded',
  48.         ];
  49.     }
  50.     public function onNavigationLoaded(NavigationLoadedEvent $event)
  51.     {
  52.         $tree $event->getNavigation()->getTree();
  53.         foreach ($tree as $key => $item) {
  54.             $hasAvailableChildProductListings false;
  55.             $children $item->getChildren();
  56.             foreach ($children as $_key => $child) {
  57.                 $category $child->getCategory();
  58.                 $isProductPage $category->getType() === 'page' && in_array($category->getCmsPageId(), $this->productCmsPageIds);
  59.                 if($isProductPage) {
  60.                     if ($this->hasAvailableProductListings($category$event->getSalesChannelContext(),
  61.                             $event->getContext())) {
  62.                         $hasAvailableChildProductListings true;
  63.                     } else {
  64.                         unset($children[$_key]);
  65.                     }
  66.                 }
  67.             }
  68.             $item->setChildren($children);
  69.             $category $item->getCategory();
  70.             $isProductPage $category->getType() === 'page' && in_array($category->getCmsPageId(), $this->productCmsPageIds);
  71.             if ($isProductPage && !$hasAvailableChildProductListings &&
  72.                 !$this->hasAvailableProductListings($category$event->getSalesChannelContext(),
  73.                     $event->getContext())) {
  74.                 unset($tree[$key]);
  75.             } else {
  76.                 $tree[$key] = $item;
  77.             }
  78.         }
  79.         $event->getNavigation()->setTree($tree);
  80.     }
  81.     protected function hasAvailableProductListings(
  82.         CategoryEntity $category,
  83.         SalesChannelContext $salesChannelContext,
  84.         Context $context
  85.     ) {
  86.         $criteria = new Criteria();
  87.         if ($category->getProductAssignmentType() == 'product_stream') {
  88.             $criteria->addAssociation('streams');
  89.             $criteria->addFilter(new EqualsFilter('streams.id'$category->getProductStreamId()));
  90.         } else {
  91.             $criteria->addFilter(new EqualsFilter('categories.id'$category->getId()));
  92.         }
  93.         $criteria->addFilter(new ProductAvailableFilter($salesChannelContext->getSalesChannel()->getId(),
  94.             ProductVisibilityDefinition::VISIBILITY_ALL));
  95.         $criteria->setLimit(1);
  96.         $this->addDisplayProductIdsForCriteria($criteria$salesChannelContext);
  97.         $productsCount $this->productRepository->search($criteria$context)->count();
  98.         return $productsCount 0;
  99.     }
  100.     private function addDisplayProductIdsForCriteria(Criteria $criteriaSalesChannelContext $salesChannelContext)
  101.     {
  102.         $displayProductIds $this->displayProductService->getDisplayProductIdsForCustomerGroupId(
  103.             $salesChannelContext->getCurrentCustomerGroup()->getId(),
  104.             $salesChannelContext->getContext());
  105.         if (empty($displayProductIds) === true) {
  106.             if ($this->configService->get('AcrisCustomerGroupAvailableProductCS.config.releaseProductsIfNoCustomerGroupAssigned',
  107.                     $salesChannelContext->getSalesChannel()->getId()) === DisplayAllowedProductService::DEFAULT_PLUGIN_CONFIG_RELEASE_PRODUCT_IF_NO_CUSTOMER_GROUPS_ASSIGNED) {
  108.                 $criteria->addAssociation('product.customerGroup');
  109.                 $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  110.                     new EqualsFilter('id'Uuid::randomHex()),
  111.                     new EqualsFilter('product.customerGroup.id'null),
  112.                 ]));
  113.                 return;
  114.             }
  115.             $criteria->addFilter(new EqualsFilter('id'Uuid::randomHex()));
  116.         } else {
  117.             $criteria->addFilter(new EqualsAnyFilter('id'$displayProductIds));
  118.         }
  119.     }
  120. }