vendor/store.shopware.com/nimbitsarticlequestionsnext/src/Subscriber/Subscriber.php line 54

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nimbits\NimbitsArticleQuestionsNext\Subscriber;
  3. use Nimbits\NimbitsArticleQuestionsNext\Setting\Service\SettingService;
  4. use Shopware\Core\Content\Mail\Service\AbstractMailService;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Struct\StructCollection;
  10. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  11. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class Subscriber implements EventSubscriberInterface
  14. {
  15.     /** @var EntityRepositoryInterface $articleQuestionsRepository */
  16.     protected $articleQuestionsRepository;
  17.     private $settingsService;
  18.     private $mailService;
  19.     private $salesChannelRepository;
  20.     private $twig;
  21.     public function __construct(
  22.         SettingService            $settingsService,
  23.         EntityRepositoryInterface $articleQuestionsRepository,
  24.         AbstractMailService       $mailService,
  25.         EntityRepositoryInterface $salesChannelRepository,
  26.         \Twig\Environment         $twig,
  27.         EntityRepositoryInterface $productRepository
  28.     )
  29.     {
  30.         $this->settingsService $settingsService;
  31.         $this->articleQuestionsRepository $articleQuestionsRepository;
  32.         $this->mailService $mailService;
  33.         $this->salesChannelRepository $salesChannelRepository;
  34.         $this->twig $twig;
  35.         $this->productRepository $productRepository;
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  40.         return [
  41.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  42.             FooterPageletLoadedEvent::class => 'onFooterLoaded'
  43.         ];
  44.     }
  45.     public function onFooterLoaded(FooterPageletLoadedEvent $event)
  46.     {
  47.         $event->getPagelet()->addExtension('nimbitsArticleQuestionsSettings',
  48.             $this->settingsService->getSettingsAsStruct($event->getSalesChannelContext()->getSalesChannel()->getId())
  49.         );
  50.     }
  51.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  52.     {
  53.         $settingsarr $this->settingsService->getSettingsAsArray();
  54.         $this->checkSalesChannel($event);
  55.         $productIds = [$event->getPage()->getProduct()->getId()];
  56.         //if we need to display the questions to all variants of a product
  57.         if ($settingsarr['showQuestionsOnAllVariants']) {
  58.             $parentId $event->getPage()->getProduct()->getParentId();
  59.             //only if we have a parent
  60.             if(!empty($parentId)){
  61.                 $productIds = [];
  62.                 //find all variants ids
  63.                 $criteria = (new Criteria)
  64.                     ->addFilter(new EqualsFilter('parentId'$parentId));
  65.                 $articleIds $this->productRepository->searchIds(
  66.                     $criteria,
  67.                     \Shopware\Core\Framework\Context::createDefaultContext()
  68.                 );
  69.                 foreach($articleIds->getData() AS $dataSet){
  70.                     $productIds[] = $dataSet["id"];
  71.                 }
  72.             }
  73.         }
  74.         $event->getPage()->getProduct()->addExtension('nimbitsArticleQuestionsSettings'$this->settingsService->getSettingsAsStruct($event->getSalesChannelContext()->getSalesChannel()->getId()));
  75.         $criteria = (new Criteria)
  76.             ->addFilter(new EqualsAnyFilter('article_id'$productIds))
  77.             ->addFilter(new EqualsFilter('active'1));
  78.         if(array_key_exists("onlyShowCurrentLanguage"$settingsarr)){
  79.              if ($settingsarr['onlyShowCurrentLanguage']) {
  80.                  $criteria->addFilter(new EqualsFilter('language_id'$event->getContext()->getLanguageId()));
  81.              }
  82.         }
  83.         $questions $this->articleQuestionsRepository->search(
  84.             $criteria,
  85.             \Shopware\Core\Framework\Context::createDefaultContext()
  86.         );
  87.         $questions $questions->getElements();
  88.         $event->getPage()->getProduct()->addExtension('nimbitsArticleQuestions', (new StructCollection())->assign(['questions' => $questions]));
  89.     }
  90.     public function checkSalesChannel(ProductPageLoadedEvent $event)
  91.     {
  92.         $productId $event->getPage()->getProduct()->getId();
  93.         $criteria = (new Criteria)
  94.             ->addFilter(new EqualsFilter('article_id'$productId))
  95.             ->addFilter(new EqualsFilter('active'1));
  96.         $settingsarr $this->settingsService->getSettingsAsArray();
  97.         $allowedStorefronts = [];
  98.         if(array_key_exists("defaultQuestionVisibilities"$settingsarr))
  99.         {
  100.             $allowedStorefronts $settingsarr['defaultQuestionVisibilities'];
  101.         }
  102.         $currentStorefront $event->getContext()->getSource()->getSalesChannelId();
  103.         $showInSalesChannel false;
  104.         foreach ($allowedStorefronts as $allowedStorefront) {
  105.             if ($currentStorefront == $allowedStorefront) {
  106.                 $showInSalesChannel true;
  107.             }
  108.             $event->getPage()->addExtension('nimbitsArticleQuestions', (new StructCollection())->assign(['showInSalesChannel' => $showInSalesChannel]));
  109.         }
  110.     }
  111. }