custom/static-plugins/BaumarktTheme/src/Subscriber/ListingSubscriber.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace BaumarktTheme\Subscriber;
  3. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaEntity;
  4. use Shopware\Core\Content\Product\Events\ProductCrossSellingIdsCriteriaEvent;
  5. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  8. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  9. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ListingSubscriber implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return [
  16.             ProductListingCriteriaEvent::class => 'onProductListingCriteria',
  17.             ProductSearchCriteriaEvent::class => 'onProductSearchCriteria',
  18.             ProductListingResultEvent::class => 'handleResult',
  19.             ProductSearchResultEvent::class => 'handleResult',
  20.             ProductCrossSellingIdsCriteriaEvent::class => 'onCrossSellingProductListCriteria'
  21.         ];
  22.     }
  23.     public function onProductSearchCriteria(ProductSearchCriteriaEvent $event)
  24.     {
  25.         $criteria $event->getCriteria();
  26.         $criteria->addAssociation('media');
  27.     }
  28.     public function onProductListingCriteria(ProductListingCriteriaEvent $event)
  29.     {
  30.         $criteria $event->getCriteria();
  31.         $criteria->addAssociation('media');
  32.     }
  33.     public function onCrossSellingProductListCriteria(ProductCrossSellingIdsCriteriaEvent $event)
  34.     {
  35.         $criteria $event->getCriteria();
  36.         $criteria->addAssociation('media');
  37.     }
  38.     public function handleResult(ProductListingResultEvent $event): void
  39.     {
  40.         /** @var SalesChannelProductEntity $product */
  41.         foreach ($event->getResult()->getEntities() as $product) {
  42.             if (!$product->getMedia()) {
  43.                 continue;
  44.             }
  45.             if ($product->getMedia()->count() < 2) {
  46.                 continue;
  47.             }
  48.             $hoverPosition 0;
  49.             if ($product->getCover() && $product->getCover()->getPosition() == 0) {
  50.                 $hoverPosition 1;
  51.             }
  52.             /** @var ProductMediaEntity $productMedia */
  53.             foreach ($product->getMedia() as $productMedia) {
  54.                 if ($product->getCover() && $product->getCover()->getMediaId()) {
  55.                     if ($productMedia->getPosition() == $hoverPosition) {
  56.                         $product->addExtension("hover"$productMedia->getMedia());
  57.                         break;
  58.                     }
  59.                 }
  60.             }
  61.             // Product media position is sometimes broken and its value is 1 for all product media
  62.             if (!$product->hasExtension("hover")) {
  63.                 /** @var ProductMediaEntity $productMedia */
  64.                 foreach ($product->getMedia() as $productMedia) {
  65.                     if ($product->getCover() && $product->getCover()->getMediaId() && $productMedia->getMedia()) {
  66.                         if ($product->getCover()->getMediaId() != $productMedia->getMediaId()) {
  67.                             $product->addExtension("hover"$productMedia->getMedia());
  68.                             break;
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.     }
  75. }