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

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\PluginConfig;
  6. use Shopware\Core\Content\Product\Events\ProductCrossSellingsLoadedEvent;
  7. use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
  8. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  9. use Shopware\Core\Content\Product\ProductEntity;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  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\Search\SearchPageLoadedEvent;
  16. use Shopware\Storefront\Page\Suggest\SuggestPageLoadedEvent;
  17. use Shopware\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class ProductSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var PluginConfig
  23.      */
  24.     private $pluginConfig;
  25.     /**
  26.      * ProductSubscriber constructor.
  27.      *
  28.      * @param PluginConfig $pluginConfig
  29.      */
  30.     public function __construct(PluginConfig $pluginConfig)
  31.     {
  32.         $this->pluginConfig $pluginConfig;
  33.     }
  34.     /**
  35.      * @return string[]
  36.      */
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             ProductPageLoadedEvent::class           => 'onProductPageLoaded',
  41.             NavigationPageLoadedEvent::class        => 'onProductPageLoaded',
  42.             SearchPageLoadedEvent::class            => 'onProductPageLoaded',
  43.             ProductCrossSellingsLoadedEvent::class  => 'onCrossSellingLoaded',
  44.             SuggestPageLoadedEvent::class           => 'onProductPageLoaded',
  45.             ProductListingResultEvent::class        => 'onListingResult',
  46.             ProductListingCollectFilterEvent::class => 'onProductFilter',
  47.             WishlistPageLoadedEvent::class          => 'onProductPageLoaded',
  48.         ];
  49.     }
  50.     /**
  51.      * @param ProductListingCollectFilterEvent $event
  52.      */
  53.     public function onProductFilter(ProductListingCollectFilterEvent $event): void
  54.     {
  55.         if (
  56.             !$this->pluginConfig->isActive()
  57.             || (
  58.                 [] === $this->pluginConfig->getGroupsForBlockedPrices()
  59.                 && !$this->pluginConfig->isPriceBlockedForLoggedOut()
  60.             )
  61.         ) {
  62.             return;
  63.         }
  64.         if (!$this->isPriceBlocked($event->getSalesChannelContext())) {
  65.             return;
  66.         }
  67.         $priceFilter $event->getFilters()->get('price');
  68.         if (null === $priceFilter) {
  69.             return;
  70.         }
  71.         $priceFilter->assign(['filter' => new RangeFilter('product.listingPrices', [RangeFilter::GT => 0])]);
  72.     }
  73.     /**
  74.      * @param ProductListingResultEvent $event
  75.      */
  76.     public function onListingResult(ProductListingResultEvent $event): void
  77.     {
  78.         if (
  79.             !$this->pluginConfig->isActive()
  80.             || (
  81.                 [] === $this->pluginConfig->getGroupsForBlockedPrices()
  82.                 && !$this->pluginConfig->isPriceBlockedForLoggedOut()
  83.             )
  84.         ) {
  85.             return;
  86.         }
  87.         if (!$this->isPriceBlocked($event->getSalesChannelContext())) {
  88.             return;
  89.         }
  90.         $products $event->getResult()->getElements();
  91.         $priceStruct = new ProductPriceStruct();
  92.         $priceStruct->setPriceHidden(true);
  93.         /** @var ProductEntity $product */
  94.         foreach ($products as $product) {
  95.             $product->addExtension('netiAMblockedPrices'$priceStruct);
  96.         }
  97.     }
  98.     /**
  99.      * @param ProductCrossSellingsLoadedEvent $event
  100.      */
  101.     public function onCrossSellingLoaded(ProductCrossSellingsLoadedEvent $event): void
  102.     {
  103.         if (!$this->pluginConfig->isActive() || [] === $this->pluginConfig->getGroupsForBlockedPrices()) {
  104.             return;
  105.         }
  106.         if (!$this->isPriceBlocked($event->getSalesChannelContext())) {
  107.             return;
  108.         }
  109.         $crossSellings $event->getCrossSellings()->getElements();
  110.         $priceStruct = new ProductPriceStruct();
  111.         $priceStruct->setPriceHidden(true);
  112.         foreach ($crossSellings as $crossSelling) {
  113.             foreach ($crossSelling->getProducts()->getElements() as $productEntity) {
  114.                 $productEntity->addExtension('netiAMblockedPrices'$priceStruct);
  115.             }
  116.         }
  117.     }
  118.     /**
  119.      * @param PageLoadedEvent $event
  120.      */
  121.     public function onProductPageLoaded(PageLoadedEvent $event): void
  122.     {
  123.         if (
  124.             !$this->pluginConfig->isActive()
  125.             || (
  126.                 [] === $this->pluginConfig->getGroupsForBlockedPrices()
  127.                 && !$this->pluginConfig->isPriceBlockedForLoggedOut()
  128.             )
  129.         ) {
  130.             return;
  131.         }
  132.         if (!$this->isPriceBlocked($event->getSalesChannelContext())) {
  133.             return;
  134.         }
  135.         $page $event->getPage();
  136.         $page->assign([ 'netiAMblockedPrices' => true ]);
  137.     }
  138.     /**
  139.      * @param SalesChannelContext $salesChannelContext
  140.      *
  141.      * @return bool
  142.      */
  143.     private function isPriceBlocked(SalesChannelContext $salesChannelContext): bool
  144.     {
  145.         $customerGroupId $salesChannelContext->getSalesChannel()->getCustomerGroupId();
  146.         $customer        $salesChannelContext->getCustomer();
  147.         if (null === $customer && $this->pluginConfig->isPriceBlockedForLoggedOut()) {
  148.             return true;
  149.         }
  150.         if (null !== $customer) {
  151.             $customerGroupId $salesChannelContext->getCustomer()->getGroupId();
  152.         }
  153.         return \in_array($customerGroupId$this->pluginConfig->getGroupsForBlockedPrices(), true);
  154.     }
  155. }