custom/plugins/ZeobvGetNotified/src/Storefront/Subscriber/PageSubscriber.php line 62

Open in your IDE?
  1. <?php declare(strict_types 1);
  2. namespace Zeobv\GetNotified\Storefront\Subscriber;
  3. use Shopware\Core\Framework\Struct\ArrayStruct;
  4. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  5. use Shopware\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Swag\CmsExtensions\Storefront\Pagelet\Quickview\QuickviewPageletLoadedEvent;
  8. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  9. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  10. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  11. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  12. use Zeobv\GetNotified\Service\StockSubscriptionService;
  13. use Zeobv\GetNotified\Struct\GetNotifiedConfigBuilder;
  14. /**
  15.  * Class PageSubscriber
  16.  *
  17.  * @package Zeobv\GetNotified\Storefront\Subscriber
  18.  */
  19. class PageSubscriber implements EventSubscriberInterface
  20. {
  21.     protected StockSubscriptionService $stockSubscriptionService;
  22.     public function __construct(
  23.         StockSubscriptionService $stockSubscriptionService
  24.     ) {
  25.         $this->stockSubscriptionService $stockSubscriptionService;
  26.     }
  27.     /**
  28.      * @return array
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         $subscribedEvents = [
  33.             'sales_channel.product.loaded' => 'onProductsLoaded',
  34.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  35.             NavigationPageLoadedEvent::class => 'onListingPageLoaded',
  36.             WishlistPageLoadedEvent::class => 'onListingPageLoaded',
  37.             SearchPageLoadedEvent::class => 'onListingPageLoaded',
  38.         ];
  39.         # Compatibility check for SwagCmsExtensions quickview pagelet
  40.         if (class_exists(QuickviewPageletLoadedEvent::class)) {
  41.             $subscribedEvents array_merge($subscribedEvents, [
  42.                 QuickviewPageletLoadedEvent::class => 'onQuickviewPageletLoaded',
  43.             ]);
  44.         }
  45.         return $subscribedEvents;
  46.     }
  47.     /**
  48.      * Add notification form config information for product listings such as Navigation, Search pages and product sliders
  49.      *
  50.      * @param SalesChannelEntityLoadedEvent $event
  51.      */
  52.     public function onProductsLoaded(SalesChannelEntityLoadedEvent $event): void
  53.     {
  54.         $salesChannelContext $event->getSalesChannelContext();
  55.         if (!$this->stockSubscriptionService->shouldShowSubscriptionForm($salesChannelContexttrue)) {
  56.             return;
  57.         }
  58.         /** @var SalesChannelProductEntity $product */
  59.         foreach ($event->getEntities() as $product) {
  60.             $product->addExtension(
  61.                 'zeobvGetNotifiedConfig',
  62.                 (new GetNotifiedConfigBuilder(
  63.                     $this->stockSubscriptionService,
  64.                     $salesChannelContext,
  65.                     $product
  66.                 ))->build()
  67.             );
  68.         }
  69.     }
  70.     /**
  71.      * @param NavigationPageLoadedEvent|SearchPageLoadedEvent $event
  72.      *
  73.      * @return void
  74.      */
  75.     public function onListingPageLoaded($event): void
  76.     {
  77.         $hideForm $this->stockSubscriptionService->getHideStockSubscriptionFormOnListingPage($event->getSalesChannelContext()->getSalesChannel());
  78.         $event->getSalesChannelContext()->addExtension('zeobvGetNotifiedEnabled', new ArrayStruct(['enabled' => !$hideForm]));
  79.     }
  80.     /**
  81.      * @param ProductPageLoadedEvent $event
  82.      */
  83.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  84.     {
  85.         $salesChannelContext $event->getSalesChannelContext();
  86.         if (!$this->stockSubscriptionService->shouldShowSubscriptionForm($salesChannelContextfalse)) {
  87.             return;
  88.         }
  89.         $product $event->getPage()->getProduct();
  90.         if ($product->hasExtension('zeobvGetNotifiedConfig')) {
  91.             return;
  92.         }
  93.         $event->getPage()->getProduct()->addExtension(
  94.             'zeobvGetNotifiedConfig',
  95.             (new GetNotifiedConfigBuilder(
  96.                 $this->stockSubscriptionService,
  97.                 $salesChannelContext,
  98.                 $product
  99.             ))->build()
  100.         );
  101.     }
  102.     /**
  103.      * @param QuickviewPageletLoadedEvent $event
  104.      */
  105.     public function onQuickviewPageletLoaded($event): void
  106.     {
  107.         $salesChannelContext $event->getSalesChannelContext();
  108.         if (!$this->stockSubscriptionService->shouldShowSubscriptionForm($salesChannelContextfalse)) {
  109.             return;
  110.         }
  111.         $product $event->getPagelet()->getProduct();
  112.         if ($product->hasExtension('zeobvGetNotifiedConfig')) {
  113.             return;
  114.         }
  115.         $product->addExtension(
  116.             'zeobvGetNotifiedConfig',
  117.             (new GetNotifiedConfigBuilder(
  118.                 $this->stockSubscriptionService,
  119.                 $salesChannelContext,
  120.                 $product
  121.             ))->build()
  122.         );
  123.     }
  124. }