custom/plugins/NetiNextAccessManager/src/Subscriber/ProductSubscriber.php line 60

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextAccessManager\Subscriber;
  4. use NetInventors\NetiNextAccessManager\Components\Struct\ProductPriceStruct;
  5. use NetInventors\NetiNextAccessManager\Service\CustomerService;
  6. use NetInventors\NetiNextAccessManager\Service\PluginConfig;
  7. use Shopware\Core\Content\Product\Events\ProductCrossSellingsLoadedEvent;
  8. use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
  9. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  10. use Shopware\Core\Content\Product\ProductEntity;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  12. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  13. use Shopware\Storefront\Page\PageLoadedEvent;
  14. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  15. use Shopware\Storefront\Page\Product\QuickView\MinimalQuickViewPageLoadedEvent;
  16. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  17. use Shopware\Storefront\Page\Suggest\SuggestPageLoadedEvent;
  18. use Shopware\Storefront\Page\Wishlist\GuestWishlistPageLoadedEvent;
  19. use Shopware\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
  20. use Shopware\Storefront\Pagelet\Wishlist\GuestWishlistPageletLoadedEvent;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. class ProductSubscriber implements EventSubscriberInterface
  23. {
  24.     private PluginConfig    $pluginConfig;
  25.     private CustomerService $customerService;
  26.     public function __construct(PluginConfig $pluginConfigCustomerService $customerService)
  27.     {
  28.         $this->pluginConfig    $pluginConfig;
  29.         $this->customerService $customerService;
  30.     }
  31.     /**
  32.      * @return string[]
  33.      */
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             ProductPageLoadedEvent::class           => 'onProductPageLoaded',
  38.             NavigationPageLoadedEvent::class        => 'onProductPageLoaded',
  39.             SearchPageLoadedEvent::class            => 'onProductPageLoaded',
  40.             ProductCrossSellingsLoadedEvent::class  => 'onCrossSellingLoaded',
  41.             SuggestPageLoadedEvent::class           => 'onProductPageLoaded',
  42.             ProductListingResultEvent::class        => 'onListingResult',
  43.             ProductListingCollectFilterEvent::class => 'onProductFilter',
  44.             WishlistPageLoadedEvent::class          => 'onProductPageLoaded',
  45.             GuestWishlistPageletLoadedEvent::class  => 'onGuestWishlistPageletLoaded',
  46.             MinimalQuickViewPageLoadedEvent::class  => 'onProductPageLoaded'
  47.         ];
  48.     }
  49.     /**
  50.      * @param ProductListingCollectFilterEvent $event
  51.      */
  52.     public function onProductFilter(ProductListingCollectFilterEvent $event): void
  53.     {
  54.         if (
  55.             !$this->pluginConfig->isActive()
  56.             || (
  57.                 [] === $this->pluginConfig->getGroupsForBlockedPrices()
  58.                 && !$this->pluginConfig->isPriceBlockedForLoggedOut()
  59.             )
  60.         ) {
  61.             return;
  62.         }
  63.         if (!$this->customerService->isPriceBlocked($event->getSalesChannelContext())) {
  64.             return;
  65.         }
  66.         $priceFilter $event->getFilters()->get('price');
  67.         if (null === $priceFilter) {
  68.             return;
  69.         }
  70.         $priceFilter->assign([ 'filter' => new RangeFilter('product.listingPrices', [ RangeFilter::GT => ]) ]);
  71.     }
  72.     public function onListingResult(ProductListingResultEvent $event): void
  73.     {
  74.         if (!$this->pluginConfig->isActive()) {
  75.             return;
  76.         }
  77.         $salesChannelContext $event->getSalesChannelContext();
  78.         $priceBlocked        $this->customerService->isPriceBlocked($salesChannelContext);
  79.         $buttonBlocked       $this->customerService->isBuyButtonBlocked($salesChannelContext);
  80.         if (!$priceBlocked && !$buttonBlocked) {
  81.             return;
  82.         }
  83.         $products $event->getResult()->getElements();
  84.         $priceStruct = new ProductPriceStruct();
  85.         $priceStruct->setPriceHidden($priceBlocked);
  86.         $priceStruct->setBuyButtonHidden($buttonBlocked);
  87.         /** @var ProductEntity $product */
  88.         foreach ($products as $product) {
  89.             $product->addExtension('netiAMblockedPrices'$priceStruct);
  90.         }
  91.     }
  92.     /**
  93.      * @param ProductCrossSellingsLoadedEvent $event
  94.      */
  95.     public function onCrossSellingLoaded(ProductCrossSellingsLoadedEvent $event): void
  96.     {
  97.         if (!$this->pluginConfig->isActive()) {
  98.             return;
  99.         }
  100.         $salesChannelContext $event->getSalesChannelContext();
  101.         $priceBlocked        $this->customerService->isPriceBlocked($salesChannelContext);
  102.         $buttonBlocked       $this->customerService->isBuyButtonBlocked($salesChannelContext);
  103.         if (!$priceBlocked && !$buttonBlocked) {
  104.             return;
  105.         }
  106.         $priceStruct = new ProductPriceStruct();
  107.         $priceStruct->setPriceHidden($priceBlocked);
  108.         $priceStruct->setBuyButtonHidden($buttonBlocked);
  109.         $crossSellings $event->getCrossSellings()->getElements();
  110.         foreach ($crossSellings as $crossSellingElement) {
  111.             foreach ($crossSellingElement->getProducts()->getElements() as $productEntity) {
  112.                 $productEntity->addExtension('netiAMblockedPrices'$priceStruct);
  113.             }
  114.         }
  115.     }
  116.     public function onProductPageLoaded(PageLoadedEvent $event): void
  117.     {
  118.         if (!$this->pluginConfig->isActive()) {
  119.             return;
  120.         }
  121.         $salesChannelContext $event->getSalesChannelContext();
  122.         $page                $event->getPage();
  123.         if ($this->customerService->isPriceBlocked($salesChannelContext)) {
  124.             $page->assign([ 'netiAMblockedPrices' => $this->customerService->isPriceBlocked($salesChannelContext) ]);
  125.             return;
  126.         }
  127.         $page->assign([ 'netiAMblockedBuyButton' => $this->customerService->isBuyButtonBlocked($salesChannelContext) ]);
  128.     }
  129.     public function onGuestWishlistPageletLoaded(GuestWishlistPageletLoadedEvent $event): void
  130.     {
  131.         if (!$this->pluginConfig->isActive()) {
  132.             return;
  133.         }
  134.         $salesChannelContext $event->getSalesChannelContext();
  135.         $page                $event->getPagelet();
  136.         $page->assign([ 'netiAMblockedPrices' => $this->customerService->isPriceBlocked($salesChannelContext) ]);
  137.     }
  138. }