<?php
namespace K3n\ThemeAfterPlugins\Subscriber;
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
use Shopware\Core\Content\Product\Exception\ProductNotFoundException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Event\NestedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
class ProductListingSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
// ProductListingCriteriaEvent::class => 'hideProductsBeforeCustomerLogin',
// ProductSearchCriteriaEvent::class => 'hideProductsBeforeCustomerLogin',
ProductPageLoadedEvent::class => 'onProductPageLoaded',
];
}
public function hideProductsBeforeCustomerLogin(NestedEvent $event)
{
if ($event instanceof ProductListingCriteriaEvent || $event instanceof ProductSearchCriteriaEvent)
{
$criteria = $event->getCriteria();
$criteria->addFilter(
new EqualsFilter('product.id', null)
);
}
}
public function onProductPageLoaded(ProductPageLoadedEvent $event)
{
throw new ProductNotFoundException($event->getPage()->getProduct()->getId());
}
}