custom/plugins/SchilderSysteme/src/Subscriber/HeaderSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace SchilderSysteme\Subscriber;
  4. use Doctrine\DBAL\Connection;
  5. use SchilderSysteme\Traits\ThemeTrait;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class HeaderSubscriber implements EventSubscriberInterface
  11. {
  12.     use ThemeTrait;
  13.     private $productRepository;
  14.     private $connection;
  15.     private $themeRepository;
  16.     public function __construct(
  17.         EntityRepositoryInterface $productRepository,
  18.         Connection $connection,
  19.         EntityRepositoryInterface $themeRepository
  20.     )
  21.     {
  22.         $this->productRepository $productRepository;
  23.         $this->connection $connection;
  24.         $this->themeRepository $themeRepository;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             HeaderPageletLoadedEvent::class => 'onHeaderLoaded'
  30.         ];
  31.     }
  32.     public function onHeaderLoaded(HeaderPageletLoadedEvent $event)
  33.     {
  34.         if(!$this->isCorrectTheme($event->getContext(), $event->getSalesChannelContext(), $this->themeRepository)){
  35.             return;
  36.         }
  37.         $topsellerIds $this->getTopsellersIds();
  38.         $criteria = new Criteria($topsellerIds);
  39.         $products $this->productRepository->search($criteria$event->getContext());
  40.         $event->getPagelet()->addExtension('topsellerProducts'$products);
  41.     }
  42.     private function getTopsellersIds()
  43.     {
  44.         $queryBuilder $this->connection->createQueryBuilder();
  45.         return $queryBuilder->select('referenced_id, label, sum(quantity) as sales')
  46.             ->from('order_line_item''item')
  47.             ->join('item''product''product''item.product_id = product.id')
  48.             ->where('product.active = 1')
  49.             ->groupBy('item.product_id')
  50.             ->orderBy('sales''desc')
  51.             ->setMaxResults(20)
  52.             ->execute()->fetchFirstColumn();
  53.     }
  54. }