vendor/crehler/additional-article-mb/src/Subscriber/ProductSubscriber.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\AdditionalArticle\Subscriber;
  3. use Crehler\AdditionalArticle\Service\AdditionalArticlesService;
  4. use Crehler\AdditionalArticle\Struct\AdditionalArticleStruct;
  5. use Crehler\MojeBambinoEngine\Service\CrehlerContextService;
  6. use Shopware\Core\Content\Product\ProductCollection;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  11. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  14. use Shopware\Core\Content\Product\ProductEvents;
  15. class ProductSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var AdditionalArticlesService
  19.      */
  20.     private $additionalArticlesService;
  21.     /**
  22.      * @var CrehlerContextService
  23.      */
  24.     private $crehlerContextService;
  25.     /**
  26.      * ProductSubscriber constructor.
  27.      * @param AdditionalArticlesService $additionalArticlesService
  28.      * @param CrehlerContextService $crehlerContextService
  29.      */
  30.     public function __construct(AdditionalArticlesService $additionalArticlesServiceCrehlerContextService $crehlerContextService)
  31.     {
  32.         $this->additionalArticlesService $additionalArticlesService;
  33.         $this->crehlerContextService $crehlerContextService;
  34.     }
  35.     /**
  36.      * @return array
  37.      */
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             ProductPageLoadedEvent::class => 'onProductsLoaded',
  42.             ProductPageCriteriaEvent::class => 'onProductLoaderCriteria',
  43.             ProductEvents::PRODUCT_SEARCH_RESULT_LOADED_EVENT => 'onSearchResult',
  44.             ProductEvents::PRODUCT_LOADED_EVENT => 'onLoadResult'
  45.         ];
  46.     }
  47.     /**
  48.      * @param ProductPageLoadedEvent $event
  49.      */
  50.     public function onProductsLoaded(ProductPageLoadedEvent $event): void
  51.     {
  52.         if($this->crehlerContextService->get(CrehlerContextService::SITEMAP_GENERATE) || $this->crehlerContextService->get(CrehlerContextService::PRODUCT_EXPORT_GENERATE)) return;
  53.         $page $event->getPage();
  54.         $product $page->getProduct();
  55.         $context $event->getSalesChannelContext();
  56.         /** @var ProductCollection $additionalArticles */
  57.         $additionalArticles $this->additionalArticlesService->getPreparedAdditionalArticles($product$context,$event->getRequest());
  58.         if(!$additionalArticles){
  59.             return;
  60.         };
  61.         $additionalArticlesDescription $this->additionalArticlesService->getDescriptionAdditionalArticles($product$context$event->getRequest());
  62.         if ($additionalArticles->count() > 0) {
  63.             $additionalArticlesStruct = (new AdditionalArticleStruct())
  64.                 ->setProducts($additionalArticles)
  65.                 ->setDescription($additionalArticlesDescription);
  66.             $page->addExtension('additionalArticles'$additionalArticlesStruct);
  67.         }
  68.     }
  69.     /**
  70.      * @param ProductPageCriteriaEvent $event
  71.      */
  72.     public function onProductLoaderCriteria(ProductPageCriteriaEvent $event)
  73.     {
  74.         if($this->crehlerContextService->get(CrehlerContextService::SITEMAP_GENERATE) || $this->crehlerContextService->get(CrehlerContextService::PRODUCT_EXPORT_GENERATE)) return;
  75.         try {
  76.             $event
  77.                 ->getCriteria()
  78.                 ->addAssociation('additionalProducts');
  79.         } catch (\Exception $e) {
  80.         }
  81.     }
  82.     /**
  83.      * @param EntityLoadedEvent $event
  84.      */
  85.     public function onLoadResult(EntityLoadedEvent $event): void
  86.     {
  87.         if($this->crehlerContextService->get(CrehlerContextService::SITEMAP_GENERATE) || $this->crehlerContextService->get(CrehlerContextService::PRODUCT_EXPORT_GENERATE)) return;
  88.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  89.             /** @var ProductEntity $entity */
  90.             foreach ($event->getEntities() as $key => $entity) {
  91.                 $extensions $this->prepareExtensions($entity);
  92.                 if (is_array($extensions)) {
  93.                     $event->getEntities()[$key]->setExtensions($extensions);
  94.                 }
  95.             }
  96.         }
  97.     }
  98.     /**
  99.      * @param EntitySearchResultLoadedEvent $event
  100.      */
  101.     public function onSearchResult(EntitySearchResultLoadedEvent $event): void
  102.     {
  103.         if($this->crehlerContextService->get(CrehlerContextService::SITEMAP_GENERATE) || $this->crehlerContextService->get(CrehlerContextService::PRODUCT_EXPORT_GENERATE)) return;
  104.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  105.             /** @var ProductEntity $entity */
  106.             foreach ($event->getResult()->getEntities() as $key => $entity) {
  107.                 $extensions $this->prepareExtensions($entity);
  108.                 if (is_array($extensions)) {
  109.                     $event->getResult()->getEntities()->get($key)->setExtensions($extensions);
  110.                 }
  111.             }
  112.         }
  113.     }
  114.     /**
  115.      * @param ProductEntity $entity
  116.      * @return array|null
  117.      */
  118.     private function prepareExtensions(ProductEntity $entity): ?array
  119.     {
  120.         /** @var ProductCollection $additionalProductsExtension */
  121.         $additionalProductsExtension $entity->getExtension('additionalProducts');
  122.         if ($additionalProductsExtension instanceof ProductCollection && count($additionalProductsExtension->getElements()) < 1) {
  123.             $product $this->additionalArticlesService->loadAdditionalProducts($entity);
  124.             $extensions $entity->getExtensions();
  125.             $extensions['additionalProducts'] = $product->getExtension('additionalProducts');
  126.             return $extensions;
  127.         }
  128.         return null;
  129.     }
  130. }