custom/plugins/EnetSubshop/src/Subscriber/HeaderPageletSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace EnetSubshop\Subscriber;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Shopware\Storefront\Page\PageLoadedEvent;
  13. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  14. use Shopware\Storefront\Pagelet\PageletLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class HeaderPageletSubscriber implements EventSubscriberInterface
  17. {
  18.     /** @var SalesChannelRepository $salesChannelRepository */
  19.     private $salesChannelRepository;
  20.     /** @var SystemConfigService $systemConfigService */
  21.     private $systemConfigService;
  22.     public function __construct(EntityRepository $salesChannelRepositorySystemConfigService $systemConfigService)
  23.     {
  24.         $this->salesChannelRepository $salesChannelRepository;
  25.         $this->systemConfigService $systemConfigService;
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             HeaderPageletLoadedEvent::class => 'onSalesChannelLoaded',
  31.             CheckoutConfirmPageLoadedEvent::class => 'onPageLoaded',
  32.         ];
  33.     }
  34.     public function onPageLoaded(PageLoadedEvent $event)
  35.     {
  36.         $isMasterStore $this->systemConfigService->get('EnetSubshop.config.isMaster');
  37.         if(!$isMasterStore) {
  38.             $event->getPage()->addExtension(
  39.                 'enetSubshop',
  40.                 $this->fetchSubshops($event->getContext())
  41.             );
  42.         }
  43.     }
  44.     public function onSalesChannelLoaded(PageletLoadedEvent $event)
  45.     {
  46.         $isMasterStore $this->systemConfigService->get('EnetSubshop.config.isMaster');
  47.         if(!$isMasterStore) {
  48.             $event->getPagelet()->addExtension(
  49.                 'enetSubshop',
  50.                 $this->fetchSubshops($event->getContext())
  51.             );
  52.         }
  53.     }
  54.     /**
  55.      * @param Context $context
  56.      * @return EntitySearchResult
  57.      */
  58.     public function fetchSubshops(Context $context)
  59.     {
  60.         $criteria = new Criteria();
  61. //        $criteria->addFilter(new EqualsFilter('sales_channel.active', true));
  62.         $criteria->addFilter(new EqualsFilter('sales_channel.maintenance'false));
  63.         // only entities which have "showasselection: 1"
  64.         $criteria->addFilter(new ContainsFilter('sales_channel.translations.customFields''"enet_adress_showasselection": "1"'));
  65.         $criteria->addAssociation('domains');
  66.         $criteria->addAssociation('type');
  67.         $criteria->addAssociation('type_translation');
  68.         $criteria->addAssociation('translation');
  69.         return $this->salesChannelRepository->search($criteria$context);
  70.     }
  71. }