<?php
declare(strict_types=1);
namespace WebLa_HideWithoutStock\Subscriber;
use WebLa_HideWithoutStock\WebLa_HideWithoutStock;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class ProductLoadingSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
public function __construct(SystemConfigService $systemConfigService)
{
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_SEARCH_CRITERIA => 'onCriteria',
ProductEvents::PRODUCT_LISTING_CRITERIA => 'onCriteria',
ProductEvents::PRODUCT_SUGGEST_CRITERIA => 'onCriteria',
];
}
public function onCriteria($event)
{
$salesChannelContext = $event->getSalesChannelContext();
$criteria = $event->getCriteria();
if ($this->systemConfigService->get('WebLa_HideWithoutStock.config.active', $salesChannelContext->getSalesChannel()->getId())) {
$criteria->addFilter(
new MultiFilter(
MultiFilter::CONNECTION_OR,
[
new RangeFilter('availableStock', [RangeFilter::GTE => 1]),
new EqualsFilter('customFields.'.WebLa_HideWithoutStock::CUSTOM_FIELD_SHOW_EMPTY_STOCK, true)
]
)
);
}
return $criteria;
}
}