custom/plugins/EsmPluginOverride/src/Subscriber/ArticleQuestionSubscriber.php line 56

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