<?php
declare(strict_types=1);
namespace NetInventors\NetiNextAccessManager\Subscriber;
use NetInventors\NetiNextAccessManager\Components\Struct\ProductPriceStruct;
use NetInventors\NetiNextAccessManager\Service\PluginConfig;
use Shopware\Core\Content\Product\Events\ProductCrossSellingsLoadedEvent;
use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Shopware\Storefront\Page\Suggest\SuggestPageLoadedEvent;
use Shopware\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductSubscriber implements EventSubscriberInterface
{
/**
* @var PluginConfig
*/
private $pluginConfig;
/**
* ProductSubscriber constructor.
*
* @param PluginConfig $pluginConfig
*/
public function __construct(PluginConfig $pluginConfig)
{
$this->pluginConfig = $pluginConfig;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded',
NavigationPageLoadedEvent::class => 'onProductPageLoaded',
SearchPageLoadedEvent::class => 'onProductPageLoaded',
ProductCrossSellingsLoadedEvent::class => 'onCrossSellingLoaded',
SuggestPageLoadedEvent::class => 'onProductPageLoaded',
ProductListingResultEvent::class => 'onListingResult',
ProductListingCollectFilterEvent::class => 'onProductFilter',
WishlistPageLoadedEvent::class => 'onProductPageLoaded',
];
}
/**
* @param ProductListingCollectFilterEvent $event
*/
public function onProductFilter(ProductListingCollectFilterEvent $event): void
{
if (
!$this->pluginConfig->isActive()
|| (
[] === $this->pluginConfig->getGroupsForBlockedPrices()
&& !$this->pluginConfig->isPriceBlockedForLoggedOut()
)
) {
return;
}
if (!$this->isPriceBlocked($event->getSalesChannelContext())) {
return;
}
$priceFilter = $event->getFilters()->get('price');
if (null === $priceFilter) {
return;
}
$priceFilter->assign(['filter' => new RangeFilter('product.listingPrices', [RangeFilter::GT => 0])]);
}
/**
* @param ProductListingResultEvent $event
*/
public function onListingResult(ProductListingResultEvent $event): void
{
if (
!$this->pluginConfig->isActive()
|| (
[] === $this->pluginConfig->getGroupsForBlockedPrices()
&& !$this->pluginConfig->isPriceBlockedForLoggedOut()
)
) {
return;
}
if (!$this->isPriceBlocked($event->getSalesChannelContext())) {
return;
}
$products = $event->getResult()->getElements();
$priceStruct = new ProductPriceStruct();
$priceStruct->setPriceHidden(true);
/** @var ProductEntity $product */
foreach ($products as $product) {
$product->addExtension('netiAMblockedPrices', $priceStruct);
}
}
/**
* @param ProductCrossSellingsLoadedEvent $event
*/
public function onCrossSellingLoaded(ProductCrossSellingsLoadedEvent $event): void
{
if (!$this->pluginConfig->isActive() || [] === $this->pluginConfig->getGroupsForBlockedPrices()) {
return;
}
if (!$this->isPriceBlocked($event->getSalesChannelContext())) {
return;
}
$crossSellings = $event->getCrossSellings()->getElements();
$priceStruct = new ProductPriceStruct();
$priceStruct->setPriceHidden(true);
foreach ($crossSellings as $crossSelling) {
foreach ($crossSelling->getProducts()->getElements() as $productEntity) {
$productEntity->addExtension('netiAMblockedPrices', $priceStruct);
}
}
}
/**
* @param PageLoadedEvent $event
*/
public function onProductPageLoaded(PageLoadedEvent $event): void
{
if (
!$this->pluginConfig->isActive()
|| (
[] === $this->pluginConfig->getGroupsForBlockedPrices()
&& !$this->pluginConfig->isPriceBlockedForLoggedOut()
)
) {
return;
}
if (!$this->isPriceBlocked($event->getSalesChannelContext())) {
return;
}
$page = $event->getPage();
$page->assign([ 'netiAMblockedPrices' => true ]);
}
/**
* @param SalesChannelContext $salesChannelContext
*
* @return bool
*/
private function isPriceBlocked(SalesChannelContext $salesChannelContext): bool
{
$customerGroupId = $salesChannelContext->getSalesChannel()->getCustomerGroupId();
$customer = $salesChannelContext->getCustomer();
if (null === $customer && $this->pluginConfig->isPriceBlockedForLoggedOut()) {
return true;
}
if (null !== $customer) {
$customerGroupId = $salesChannelContext->getCustomer()->getGroupId();
}
return \in_array($customerGroupId, $this->pluginConfig->getGroupsForBlockedPrices(), true);
}
}