custom/plugins/WodkaHoverOnListingSW6/src/Subscriber/FrontendSubscriber.php line 80

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace WodkaHoverOnListingSW6\Subscriber;
  3. use WodkaHoverOnListingSW6\Services\HoverOnListingConfiguration;
  4. use WodkaHoverOnListingSW6\Services\HoverOnListingConfigurationService;
  5. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaEntity;
  6. use Shopware\Core\Content\Product\Events\ProductCrossSellingIdsCriteriaEvent;
  7. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  8. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  9. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  10. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  11. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Shopware\Storefront\Event\StorefrontRenderEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class FrontendSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var array
  19.      */
  20.     protected array $pluginConfig;
  21.     /**
  22.      * @var HoverOnListingConfigurationService
  23.      */
  24.     protected HoverOnListingConfigurationService $hoverOnListingConfigurationService;
  25.     /**
  26.      * @param HoverOnListingConfigurationService $hoverOnListingConfigurationService
  27.      */
  28.     public function __construct(HoverOnListingConfigurationService $hoverOnListingConfigurationService)
  29.     {
  30.         $this->hoverOnListingConfigurationService $hoverOnListingConfigurationService;
  31.         $this->pluginConfig = [];
  32.     }
  33.     /**
  34.      * @return string[]
  35.      */
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             ProductListingCriteriaEvent::class => 'onProductListingCriteria',
  40.             ProductSearchCriteriaEvent::class => 'onProductSearchCriteria',
  41.             ProductListingResultEvent::class => 'handleResult',
  42.             ProductSearchResultEvent::class => 'handleResult',
  43.             ProductCrossSellingIdsCriteriaEvent::class => 'onCrossSellingProductListCriteria',
  44.             StorefrontRenderEvent::class => 'onStorefrontRender'
  45.         ];
  46.     }
  47.     /**
  48.      * @param SalesChannelContext $salesChannelContext
  49.      * @return HoverOnListingConfiguration|null
  50.      */
  51.     private function getPluginConfig(SalesChannelContext $salesChannelContext): ?HoverOnListingConfiguration
  52.     {
  53.         if (!isset($this->pluginConfig[$salesChannelContext->getToken()]) || !($this->pluginConfig[$salesChannelContext->getToken()] instanceof HoverOnListingConfiguration)) {
  54.             $this->pluginConfig[$salesChannelContext->getToken()] = $this->hoverOnListingConfigurationService->getConfiguration($salesChannelContext);
  55.         }
  56.         return $this->pluginConfig[$salesChannelContext->getToken()];
  57.     }
  58.     /**
  59.      * @param SalesChannelContext $salesChannelContext
  60.      * @return bool
  61.      */
  62.     private function isPluginActive(SalesChannelContext $salesChannelContext): bool
  63.     {
  64.         return $this->getPluginConfig($salesChannelContext) && $this->getPluginConfig($salesChannelContext)->isActive();
  65.     }
  66.     /**
  67.      * @param StorefrontRenderEvent $event
  68.      * @return void
  69.      */
  70.     public function onStorefrontRender(StorefrontRenderEvent $event): void
  71.     {
  72.         $event->getSalesChannelContext()
  73.             ->addExtension('WodkaHoverOnListingSW6'$this->getPluginConfig($event->getSalesChannelContext()));
  74.     }
  75.     /**
  76.      * @param ProductSearchCriteriaEvent $event
  77.      * @return void
  78.      */
  79.     public function onProductSearchCriteria(ProductSearchCriteriaEvent $event): void
  80.     {
  81.         if ($this->isPluginActive($event->getSalesChannelContext())) {
  82.             $criteria $event->getCriteria();
  83.             $criteria->addAssociation('media');
  84.         }
  85.     }
  86.     /**
  87.      * @param ProductCrossSellingIdsCriteriaEvent $event
  88.      * @return void
  89.      */
  90.     public function onCrossSellingProductListCriteria(ProductCrossSellingIdsCriteriaEvent $event): void
  91.     {
  92.         if ($this->isPluginActive($event->getSalesChannelContext())) {
  93.             $criteria $event->getCriteria();
  94.             $criteria->addAssociation('media');
  95.         }
  96.     }
  97.     /**
  98.      * @param ProductListingCriteriaEvent $event
  99.      * @return void
  100.      */
  101.     public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
  102.     {
  103.         if ($this->isPluginActive($event->getSalesChannelContext())) {
  104.             $criteria $event->getCriteria();
  105.             $criteria->addAssociation('media');
  106.         }
  107.     }
  108.     /**
  109.      * @param ProductListingResultEvent $event
  110.      * @return void
  111.      */
  112.     public function handleResult(ProductListingResultEvent $event): void
  113.     {
  114.         if ($this->isPluginActive($event->getSalesChannelContext())) {
  115.             /** @var SalesChannelProductEntity $product */
  116.             foreach ($event->getResult()->getEntities() as $product) {
  117.                 // Check if we have more than 1 images
  118.                 if ($product->getMedia() && $product->getMedia()->count() >= 2) {
  119.                     $hoverPosition 1;
  120.                     if ($product->getCover() && $product->getCover()->getPosition() === 0) {
  121.                         /** @var ProductMediaEntity $productMedia */
  122.                         foreach ($product->getMedia() as $productMedia) {
  123.                             if ($productMedia->getPosition() === $hoverPosition) {
  124.                                 $product->addExtension("hover"$productMedia->getMedia());
  125.                                 break;
  126.                             }
  127.                         }
  128.                     } else {
  129.                         // Product media position is sometimes broken and its value is 1 for all product media
  130.                         /** @var ProductMediaEntity $productMedia */
  131.                         foreach ($product->getMedia() as $productMedia) {
  132.                             if ($product->getCover() && $productMedia->getMedia() && $product->getCover()->getMediaId() && $product->getCover()->getMediaId() !== $productMedia->getMediaId()) {
  133.                                 $product->addExtension("hover"$productMedia->getMedia());
  134.                                 break;
  135.                             }
  136.                         }
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.     }
  142. }