custom/plugins/zenitPlatformHorizon/src/Subscriber/ProductListingSubscriber.php line 68

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformHorizon\Subscriber;
  3. use Shopware\Core\Content\Product\Events\ProductCrossSellingStreamCriteriaEvent;
  4. use Shopware\Core\Content\Product\Events\ProductCrossSellingIdsCriteriaEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Storefront\Theme\ThemeConfigValueAccessor;
  11. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  12. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  13. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  14. use Shopware\Storefront\Page\Wishlist\WishListPageProductCriteriaEvent;
  15. class ProductListingSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var string
  19.      */
  20.     private $pluginName 'zenitPlatformHorizon';
  21.     /**
  22.      * @var ThemeConfigValueAccessor
  23.      */
  24.     private $themeConfigAccessor;
  25.     /**
  26.      * @var EntityRepository
  27.      */
  28.     private $themeRepository;
  29.     /**
  30.      * @var string
  31.      */
  32.     private $themeId;
  33.     public function __construct(ThemeConfigValueAccessor $themeConfigAccessorEntityRepository $themeRepository)
  34.     {
  35.         $this->themeConfigAccessor $themeConfigAccessor;
  36.         $this->themeRepository $themeRepository;
  37.     }
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             ProductSuggestCriteriaEvent::class => 'handleSuggestRequest',
  42.             ProductCrossSellingIdsCriteriaEvent::class => 'handleCrossSellingRequest',
  43.             ProductCrossSellingStreamCriteriaEvent::class => 'handleCrossSellingLoadRequest',
  44.             ProductListingCriteriaEvent::class => 'handleListingRequest',
  45.             ProductSearchCriteriaEvent::class => 'handleSearchRequest',
  46.             WishListPageProductCriteriaEvent::class => 'handleWishlistRequest'
  47.         ];
  48.     }
  49.     public function handleCrossSellingRequest(ProductCrossSellingIdsCriteriaEvent $event)
  50.     {
  51.         if ($this->hasCardImgSwitch($event)) {
  52.             $criteria $event->getCriteria();
  53.             $criteria->addAssociation('media');
  54.         }
  55.     }
  56.     public function handleCrossSellingLoadRequest(ProductCrossSellingStreamCriteriaEvent $event)
  57.     {
  58.         if ($this->hasCardImgSwitch($event)) {
  59.             $criteria $event->getCriteria();
  60.             $criteria->addAssociation('media');
  61.         }
  62.     }
  63.     public function handleListingRequest(ProductListingCriteriaEvent $event)
  64.     {
  65.         if ($this->hasCardImgSwitch($event)) {
  66.             $criteria $event->getCriteria();
  67.             $criteria->addAssociation('media');
  68.         }
  69.     }
  70.     public function handleSuggestRequest(ProductSuggestCriteriaEvent $event)
  71.     {
  72.         if ($this->hasCardImgSwitch($event)) {
  73.             $criteria $event->getCriteria();
  74.             $criteria->addAssociation('media');
  75.         }
  76.     }
  77.     public function handleSearchRequest(ProductSearchCriteriaEvent $event)
  78.     {
  79.         if ($this->hasCardImgSwitch($event)) {
  80.             $criteria $event->getCriteria();
  81.             $criteria->addAssociation('media');
  82.         }
  83.     }
  84.     public function handleWishlistRequest(WishListPageProductCriteriaEvent $event)
  85.     {
  86.         if ($this->hasCardImgSwitch($event)) {
  87.             $criteria $event->getCriteria();
  88.             $criteria->addAssociation('media');
  89.         }
  90.     }
  91.     private function getThemeIdByTechnicalName(string $technicalNameContext $context): ?string
  92.     {
  93.         return $this->themeRepository->searchIds(
  94.             (new Criteria())->addFilter(new EqualsFilter('technicalName'$technicalName)),
  95.             $context
  96.         )->firstId();
  97.     }
  98.     private function hasCardImgSwitch($event): ?bool
  99.     {
  100.         $context $event->getContext();
  101.         $salesChannelContext $event->getSalesChannelContext();
  102.         $themeId $this->getThemeIdByTechnicalName($this->pluginName$context);
  103.         $this->themeId $themeId;
  104.         $cardImgSwitchValue $this->themeConfigAccessor->get('zen-product-listing-card-img-switch'$salesChannelContext$this->themeId);
  105.         return $cardImgSwitchValue !== null and $cardImgSwitchValue !== 'none';
  106.     }
  107. }