custom/plugins/CbaxModulManufacturers/src/Subscriber/FrontendSubscriber.php line 67

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