<?php
namespace B2bSellersCore\Components\Product\Subscriber;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductHidingSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProductListingCriteriaEvent::class => 'addProductListingHidingCriteria',
ProductSuggestCriteriaEvent::class => 'addProductSuggestHidingCriteria',
ProductSearchCriteriaEvent::class => 'addProductSearchHidingCriteria',
];
}
public function addProductSuggestHidingCriteria(ProductSuggestCriteriaEvent $event)
{
$this->addHidingCriteria($event->getCriteria());
}
public function addProductListingHidingCriteria(ProductListingCriteriaEvent $event)
{
$this->addHidingCriteria($event->getCriteria());
}
public function addProductSearchHidingCriteria(ProductListingCriteriaEvent $event)
{
$this->addHidingCriteria($event->getCriteria());
}
private function addHidingCriteria(Criteria $criteria) {
$criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
new NotFilter(NotFilter::CONNECTION_AND, [
new EqualsFilter('customFields.b2b_hide_product', true),
]
),
new EqualsFilter('customFields.b2b_hide_product', NULL)
]));
}
}