custom/plugins/SasVariantSwitch/src/Subscriber/ProductListingResultLoadedSubscriber.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SasVariantSwitch\Subscriber;
  3. use SasVariantSwitch\SasVariantSwitch;
  4. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  5. use Shopware\Core\Content\Product\ProductCollection;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use SasVariantSwitch\Storefront\Event\ProductBoxLoadedEvent;
  9. use SasVariantSwitch\Storefront\Page\ProductListingConfigurationLoader;
  10. class ProductListingResultLoadedSubscriber implements EventSubscriberInterface
  11. {
  12.     private ProductListingConfigurationLoader $listingConfigurationLoader;
  13.     private SystemConfigService $systemConfigService;
  14.     public function __construct(
  15.         ProductListingConfigurationLoader $listingConfigurationLoader,
  16.         SystemConfigService $systemConfigService
  17.     ) {
  18.         $this->listingConfigurationLoader $listingConfigurationLoader;
  19.         $this->systemConfigService $systemConfigService;
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             // 'sales_channel.product.loaded' => 'handleProductListingLoadedRequest',
  25.             ProductListingResultEvent::class => [
  26.                 ['handleProductListingLoadedRequest'201],
  27.             ],
  28.             ProductBoxLoadedEvent::class => [
  29.                 ['handleProductBoxLoadedRequest'201],
  30.             ],
  31.         ];
  32.     }
  33.     public function handleProductListingLoadedRequest(ProductListingResultEvent $event): void
  34.     {
  35.         $context $event->getSalesChannelContext();
  36.         if (!$this->systemConfigService->getBool(SasVariantSwitch::SHOW_ON_PRODUCT_CARD$context->getSalesChannelId())) {
  37.             return;
  38.         }
  39.         /** @var ProductCollection $entities */
  40.         $entities $event->getResult()->getEntities();
  41.         $this->listingConfigurationLoader->loadListing($entities$context);
  42.     }
  43.     public function handleProductBoxLoadedRequest(ProductBoxLoadedEvent $event): void
  44.     {
  45.         $context $event->getSalesChannelContext();
  46.         if (!$this->systemConfigService->getBool(SasVariantSwitch::SHOW_ON_PRODUCT_CARD$context->getSalesChannelId())) {
  47.             return;
  48.         }
  49.         $this->listingConfigurationLoader->loadListing(new ProductCollection([$event->getProduct()]), $context);
  50.     }
  51. }