custom/plugins/MoorlInterrupter/src/Storefront/Subscriber/ProductListingSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace MoorlInterrupter\Storefront\Subscriber;
  3. use MoorlInterrupter\Core\Content\Interrupter\InterrupterCollection;
  4. use Shopware\Core\Content\Cms\DataResolver\CmsSlotsDataResolver;
  5. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ProductListingSubscriber implements EventSubscriberInterface
  15. {
  16.     private DefinitionInstanceRegistry $definitionInstanceRegistry;
  17.     private SystemConfigService $systemConfigService;
  18.     private CmsSlotsDataResolver $resolver;
  19.     public function __construct(
  20.         DefinitionInstanceRegistry $definitionInstanceRegistry,
  21.         SystemConfigService $systemConfigService,
  22.         CmsSlotsDataResolver $resolver
  23.     )
  24.     {
  25.         $this->definitionInstanceRegistry $definitionInstanceRegistry;
  26.         $this->systemConfigService $systemConfigService;
  27.         $this->resolver $resolver;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             ProductListingResultEvent::class => 'onProductListingResult',
  33.         ];
  34.     }
  35.     public function onProductListingResult(ProductListingResultEvent $event): void
  36.     {
  37.         $salesChannelContext $event->getSalesChannelContext();
  38.         $result $event->getResult();
  39.         $request $event->getRequest();
  40.         $navigationId $result->getCurrentFilter('navigationId');
  41.         if (!$navigationId) {
  42.             return;
  43.         }
  44.         $time = (new \DateTimeImmutable())->format(DATE_ATOM);
  45.         $criteria = new Criteria();
  46.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_AND,  [
  47.             new EqualsFilter('active'true),
  48.             new EqualsFilter('categories.id'$navigationId),
  49.             new RangeFilter('minResults', ['lte' => $result->count()]),
  50.             new MultiFilter(
  51.                 MultiFilter::CONNECTION_OR, [
  52.                     new EqualsFilter('showFrom'null),
  53.                     new RangeFilter('showFrom', ['lte' => $time])
  54.                 ]
  55.             ),
  56.             new MultiFilter(
  57.                 MultiFilter::CONNECTION_OR, [
  58.                     new EqualsFilter('showUntil'null),
  59.                     new RangeFilter('showUntil', ['gte' => $time])
  60.                 ]
  61.             ),
  62.             new MultiFilter(
  63.                 MultiFilter::CONNECTION_OR, [
  64.                     new EqualsFilter('page'0),
  65.                     new EqualsFilter('page'$result->getPage())
  66.                 ]
  67.             )
  68.         ]));
  69.         $repo $this->definitionInstanceRegistry->getRepository('moorl_interrupter');
  70.         /** @var InterrupterCollection $moorlInterrupters */
  71.         $moorlInterrupters $repo->search($criteria$salesChannelContext->getContext())->getEntities();
  72.         if ($moorlInterrupters->count() === 0) {
  73.             return;
  74.         }
  75.         $resolverContext = new ResolverContext($salesChannelContext$request);
  76.         $this->resolver->resolve($moorlInterrupters$resolverContext);
  77.         $result->assign([
  78.             'moorlInterrupters' => $moorlInterrupters
  79.         ]);
  80.     }
  81. }