vendor/store.shopware.com/cbaxmoduladcell/src/Subscriber/BackendSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cbax\ModulAdcell\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  8. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Core\PlatformRequest;
  11. class BackendSubscriber implements EventSubscriberInterface
  12. {
  13.     const CONFIG_PATH 'CbaxModulAdcell.config';
  14.     private $config null;
  15.     /**
  16.      * @var SystemConfigService
  17.      */
  18.     private $systemConfigService;
  19.     /**
  20.      * @var EntityRepositoryInterface
  21.      */
  22.     private $productRepository;
  23.     public function __construct(
  24.         SystemConfigService $systemConfigService,
  25.         EntityRepositoryInterface $productRepository
  26.     )
  27.     {
  28.         $this->systemConfigService $systemConfigService;
  29.         $this->productRepository $productRepository;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return[
  34.             NavigationPageLoadedEvent::class => 'onNavigationPageLoaded'
  35.         ];
  36.     }
  37.     public function onNavigationPageLoaded(NavigationPageLoadedEvent $event)
  38.     {
  39.         $request $event->getRequest();
  40.         $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT)->getSalesChannelId();
  41.         $this->config $this->config ?? $this->systemConfigService->get(self::CONFIG_PATH$salesChannelId);
  42.         if (empty($this->config['active']) || empty($this->config['retargeting']) || empty($this->config['pubId'])) return;
  43.         if (!isset($_COOKIE['adcell-enabled'])) return;
  44.         $page $event->getPage();
  45.         $activeCategory $page->getHeader()->getNavigation()->getActive();
  46.         if (empty($activeCategory)) return;
  47.         if ($activeCategory->getTranslated()['name'] === 'Home') return;
  48.         $criteria = new Criteria();
  49.         $criteria->setLimit(20);
  50.         $criteria->addFilter(new EqualsFilter('parentId'null));
  51.         $criteria->addFilter(new ContainsFilter('categoryTree'$activeCategory->getId()));
  52.         $products $this->productRepository->search($criteria$event->getContext());
  53.         if (!empty($products) && !empty($products->getElements()))
  54.         {
  55.             $productNumbers = [];
  56.             foreach ($products->getElements() as $prod)
  57.             {
  58.                 $productNumbers[] = $prod->getProductNumber();
  59.             }
  60.             $productNumbersString implode(';'$productNumbers);
  61.             $page->assign( ['cbaxProductIdsForAdcell' => $productNumbersString]);
  62.         }
  63.     }
  64. }