custom/plugins/BartCPreemiumArticle/src/Subscriber/RemoveFromSuggest.php line 30

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\ProductSuggestCriteriaEvent;
  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. class RemoveFromSuggest implements EventSubscriberInterface
  11. {
  12.     private PremiumArticleRepositoryInterface $getAllPreemiumWithoutSell;
  13.     public function __construct(PremiumArticleRepositoryInterface $getAllPreemiumWithoutSell)
  14.     {
  15.         $this->getAllPreemiumWithoutSell $getAllPreemiumWithoutSell;
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             ProductSuggestCriteriaEvent::class => 'onProductSearch',
  21.         ];
  22.     }
  23.     public function onProductSearch(ProductSuggestCriteriaEvent $event): void
  24.     {
  25.         $criteria $event->getCriteria();
  26.         $preemiumWithoutSellResult $this->getAllPreemiumWithoutSell->getAllWithoutSell();
  27.         $filters = [];
  28.         /** @var PremiumEntity $result */
  29.         foreach ($preemiumWithoutSellResult as $result) {
  30.             if ($result instanceof PremiumEntity && $result->getProduct() !== null) {
  31.                 $filters[] = new EqualsFilter(
  32.                     'product.id',
  33.                     $result->getProduct()->getId()
  34.                 );
  35.                 foreach ($result->getProduct()->getChildren() as $variant) {
  36.                     $filters[] = new EqualsFilter(
  37.                         'product.id',
  38.                         $variant->getId()
  39.                     );
  40.                 }
  41.             }
  42.         }
  43.         $criteria->addFilter(
  44.             new NotFilter(
  45.                 NotFilter::CONNECTION_OR,
  46.                 $filters
  47.             )
  48.         );
  49.     }
  50. }