vendor/store.shopware.com/cbaxmodulmanufacturers/src/Subscriber/FrontendSubscriber.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cbax\ModulManufacturers\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  5. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionCollection;
  6. use Cbax\ModulManufacturers\Components\ConfigReaderHelper;
  7. use Cbax\ModulManufacturers\Components\ManufacturersHelper;
  8. class FrontendSubscriber implements EventSubscriberInterface
  9. {
  10.     private $config null;
  11.     /**
  12.      * @var ConfigReaderHelper
  13.      */
  14.     private $configReader;
  15.     /**
  16.      * @var ManufacturersHelper
  17.      */
  18.     private $helperComponent;
  19.     public function __construct(
  20.             ConfigReaderHelper $configReader,
  21.             ManufacturersHelper $helperComponent)
  22.     {
  23.         $this->configReader $configReader;
  24.         $this->helperComponent $helperComponent;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  30.         ];
  31.     }
  32.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  33.     {
  34.         $this->config $this->config ?? $this->configReader->getConfig();
  35.         if ($this->config['active'] != true)
  36.         {
  37.             return;
  38.         }
  39.         $page $event->getPage();
  40.         $cmsPage $page->getCmsPage();
  41.         $cbaxManufacturersProductSlider null;
  42.         if (!empty($cmsPage))
  43.         {
  44.             $cbaxManufacturersProductSlider $cmsPage->getFirstElementOfType('cbax-manufacturers-product-slider');
  45.         }
  46.         $hasCmsProductSlider = !empty($cbaxManufacturersProductSlider);
  47.         $sliderConfig $hasCmsProductSlider $cbaxManufacturersProductSlider->getFieldConfig()->getElements() : [];
  48.         $cbaxProductSliderBlockId $hasCmsProductSlider $cbaxManufacturersProductSlider->getBlockId() : null;
  49.         if (($this->config['showProductSlider'] == true && empty($cmsPage)) || ($hasCmsProductSlider && !empty($cmsPage)))
  50.         {
  51.             $salesChannelContext $event->getSalesChannelContext();
  52.             $product $page->getProduct();
  53.             $currentProductId $product->getParentId() ?? $product->getId();
  54.             // Shopware übergibt hier nur einen Teil der nötigen Daten, daher später kompletten Hersteller holen
  55.             $thinManufacurer $product->getManufacturer();
  56.             // prüfen, ob ein Hersteller hinterlegt ist
  57.             if ($thinManufacurer !== null) {
  58.                 $manufaturerId $thinManufacurer->getId();
  59.                 $categoryId null;
  60.                 if (($this->config['sliderByCategory'] && !$hasCmsProductSlider) || ($hasCmsProductSlider && $sliderConfig['sliderByCategory']->getValue()))
  61.                 {
  62.                     $categoryTree $product->getCategoryTree();
  63.                     $categoryId end($categoryTree);
  64.                 }
  65.                 $manufacturerProducts $this->helperComponent->getProductsByManufacturer(
  66.                     $manufaturerId,
  67.                     $categoryId,
  68.                     $salesChannelContext,
  69.                     $hasCmsProductSlider $sliderConfig['productSliderSorting']->getValue() : $this->config['productSliderSorting'],
  70.                     $hasCmsProductSlider $sliderConfig['maxSliderProducts']->getValue() : $this->config['maxSliderProducts'],
  71.                     $currentProductId
  72.                 );
  73.                 if (($this->config['sliderWithManufacturer'] && !$hasCmsProductSlider) || ($hasCmsProductSlider && $sliderConfig['sliderWithManufacturer']->getValue()))
  74.                 {
  75.                     if (empty($thinManufacurer->getMedia()) && !empty($thinManufacurer->getMediaId())) {
  76.                         $manufacturer $this->helperComponent->getManufacturerById($manufaturerId$salesChannelContext);
  77.                     } else {
  78.                         $manufacturer = [$thinManufacurer];
  79.                     }
  80.                     $manufacturerProducts array_merge($manufacturer$manufacturerProducts);
  81.                 }
  82.             } else {
  83.                 $manufacturerProducts = [];
  84.             }
  85.             // leeren Slider entfernen
  86.             if (empty($manufacturerProducts) && !empty($cbaxProductSliderBlockId))
  87.             {
  88.                 $cmsSectionCollection = new CmsSectionCollection();
  89.                 foreach ($cmsPage->getSections() as $section) {
  90.                     $newBlocksCollection $section->getBlocks();
  91.                     $newBlocksCollection->filterAndReduceByProperty('id'$cbaxProductSliderBlockId);
  92.                     $section->setBlocks($newBlocksCollection);
  93.                     $cmsSectionCollection->add($section);
  94.                 }
  95.                 $cmsPage->setSections($cmsSectionCollection);
  96.                 $page->setCmsPage($cmsPage);
  97.             }
  98.             $page->assign( ['cbaxModulManufacturers' => $manufacturerProducts]);
  99.         }
  100.     }
  101. }