<?php
declare(strict_types=1);
namespace NetInventors\NetiNextAccessManager\Subscriber;
use NetInventors\NetiNextAccessManager\Components\Struct\ProductPriceStruct;
use NetInventors\NetiNextAccessManager\Service\CustomerService;
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\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Product\QuickView\MinimalQuickViewPageLoadedEvent;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Shopware\Storefront\Page\Suggest\SuggestPageLoadedEvent;
use Shopware\Storefront\Page\Wishlist\GuestWishlistPageLoadedEvent;
use Shopware\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
use Shopware\Storefront\Pagelet\Wishlist\GuestWishlistPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductSubscriber implements EventSubscriberInterface
{
private PluginConfig $pluginConfig;
private CustomerService $customerService;
public function __construct(PluginConfig $pluginConfig, CustomerService $customerService)
{
$this->pluginConfig = $pluginConfig;
$this->customerService = $customerService;
}
/**
* @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',
GuestWishlistPageletLoadedEvent::class => 'onGuestWishlistPageletLoaded',
MinimalQuickViewPageLoadedEvent::class => 'onProductPageLoaded'
];
}
/**
* @param ProductListingCollectFilterEvent $event
*/
public function onProductFilter(ProductListingCollectFilterEvent $event): void
{
if (
!$this->pluginConfig->isActive()
|| (
[] === $this->pluginConfig->getGroupsForBlockedPrices()
&& !$this->pluginConfig->isPriceBlockedForLoggedOut()
)
) {
return;
}
if (!$this->customerService->isPriceBlocked($event->getSalesChannelContext())) {
return;
}
$priceFilter = $event->getFilters()->get('price');
if (null === $priceFilter) {
return;
}
$priceFilter->assign([ 'filter' => new RangeFilter('product.listingPrices', [ RangeFilter::GT => 0 ]) ]);
}
public function onListingResult(ProductListingResultEvent $event): void
{
if (!$this->pluginConfig->isActive()) {
return;
}
$salesChannelContext = $event->getSalesChannelContext();
$priceBlocked = $this->customerService->isPriceBlocked($salesChannelContext);
$buttonBlocked = $this->customerService->isBuyButtonBlocked($salesChannelContext);
if (!$priceBlocked && !$buttonBlocked) {
return;
}
$products = $event->getResult()->getElements();
$priceStruct = new ProductPriceStruct();
$priceStruct->setPriceHidden($priceBlocked);
$priceStruct->setBuyButtonHidden($buttonBlocked);
/** @var ProductEntity $product */
foreach ($products as $product) {
$product->addExtension('netiAMblockedPrices', $priceStruct);
}
}
/**
* @param ProductCrossSellingsLoadedEvent $event
*/
public function onCrossSellingLoaded(ProductCrossSellingsLoadedEvent $event): void
{
if (!$this->pluginConfig->isActive()) {
return;
}
$salesChannelContext = $event->getSalesChannelContext();
$priceBlocked = $this->customerService->isPriceBlocked($salesChannelContext);
$buttonBlocked = $this->customerService->isBuyButtonBlocked($salesChannelContext);
if (!$priceBlocked && !$buttonBlocked) {
return;
}
$priceStruct = new ProductPriceStruct();
$priceStruct->setPriceHidden($priceBlocked);
$priceStruct->setBuyButtonHidden($buttonBlocked);
$crossSellings = $event->getCrossSellings()->getElements();
foreach ($crossSellings as $crossSellingElement) {
foreach ($crossSellingElement->getProducts()->getElements() as $productEntity) {
$productEntity->addExtension('netiAMblockedPrices', $priceStruct);
}
}
}
public function onProductPageLoaded(PageLoadedEvent $event): void
{
if (!$this->pluginConfig->isActive()) {
return;
}
$salesChannelContext = $event->getSalesChannelContext();
$page = $event->getPage();
if ($this->customerService->isPriceBlocked($salesChannelContext)) {
$page->assign([ 'netiAMblockedPrices' => $this->customerService->isPriceBlocked($salesChannelContext) ]);
return;
}
$page->assign([ 'netiAMblockedBuyButton' => $this->customerService->isBuyButtonBlocked($salesChannelContext) ]);
}
public function onGuestWishlistPageletLoaded(GuestWishlistPageletLoadedEvent $event): void
{
if (!$this->pluginConfig->isActive()) {
return;
}
$salesChannelContext = $event->getSalesChannelContext();
$page = $event->getPagelet();
$page->assign([ 'netiAMblockedPrices' => $this->customerService->isPriceBlocked($salesChannelContext) ]);
}
}