custom/plugins/BartCPreemiumArticle/src/Subscriber/RemoveFromSearch.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BartCPreemiumArticle\PremiumArticle\Subscriber;
  4. use BartCPreemiumArticle\PremiumArticle\Core\Content\Premium\PremiumEntity;
  5. use BartCPreemiumArticle\PremiumArticle\Repository\PremiumArticleRepositoryInterface;
  6. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. // todo change name to checkSearchResultPage or something like that
  11. class RemoveFromSearch implements EventSubscriberInterface
  12. {
  13.     private PremiumArticleRepositoryInterface $getAllPreemiumWithoutSell;
  14.     public function __construct(PremiumArticleRepositoryInterface $getAllPreemiumWithoutSell)
  15.     {
  16.         $this->getAllPreemiumWithoutSell $getAllPreemiumWithoutSell;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             ProductSearchCriteriaEvent::class => 'onProductSearch',
  22.         ];
  23.     }
  24.     public function onProductSearch(ProductSearchCriteriaEvent $event): void
  25.     {
  26.         $criteria $event->getCriteria();
  27.         $preemiumWithoutSellResult $this->getAllPreemiumWithoutSell->getAllWithoutSell();
  28.         $filters = [];
  29.         /** @var PremiumEntity $result */
  30.         foreach ($preemiumWithoutSellResult as $result) {
  31.             if ($result instanceof PremiumEntity && $result->getProduct() !== null) {
  32.                 $filters[] = new EqualsFilter(
  33.                     'product.id',
  34.                     $result->getProduct()->getId()
  35.                 );
  36.                 foreach ($result->getProduct()->getChildren() as $variant) {
  37.                     $filters[] = new EqualsFilter(
  38.                         'product.id',
  39.                         $variant->getId()
  40.                     );
  41.                 }
  42.             }
  43.         }
  44.         $criteria->addFilter(
  45.             new NotFilter(
  46.                 NotFilter::CONNECTION_OR,
  47.                 $filters
  48.             )
  49.         );
  50.     }
  51. }