custom/plugins/zenitPlatformSphere/src/Subscriber/ProductListingSubscriber.php line 74

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