custom/plugins/WeloEanDetailPage/src/Storefront/Subscriber/ProductSubscriber.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Welo\EanDetailPage\Storefront\Subscriber;
  3. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  4. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Welo\EanDetailPage\Service\Configuration;
  7. class ProductSubscriber implements EventSubscriberInterface
  8. {
  9.     /**
  10.      * @var Configuration
  11.      */
  12.     private $configuration;
  13.     
  14.     /**
  15.      * ProductSubscriber constructor.
  16.      *
  17.      * @param Configuration $configuration
  18.      */
  19.     public function __construct(Configuration $configuration)
  20.     {
  21.         $this->configuration $configuration;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  27.         ];
  28.     }
  29.     
  30.     /**
  31.      * @param ProductPageLoadedEvent $event
  32.      * @throws \Exception
  33.      */
  34.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  35.     {
  36.         $saleChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  37.         $eanActive = (bool)$this->configuration->getActive('EAN'$saleChannelId);
  38.         $manufacturerActive = (bool)$this->configuration->getActive('ManufacturerNumber',$saleChannelId);
  39.         $manufacturerNameActive = (bool)$this->configuration->getActive('ManufacturerName'$saleChannelId);
  40.         $weightActive = (bool)$this->configuration->getActive('Weight'$saleChannelId);
  41.         $lengthActive = (bool)$this->configuration->getActive('Length'$saleChannelId);
  42.         $heightActive = (bool)$this->configuration->getActive('Height'$saleChannelId);
  43.         $widthActive = (bool)$this->configuration->getActive('Width'$saleChannelId);
  44.         $eanPosition = (int)$this->configuration->getPosition('EAN'$saleChannelId);
  45.         $manufacturerPosition = (int)$this->configuration->getPosition('ManufacturerNumber'$saleChannelId);
  46.         $manufacturerNamePosition = (int)$this->configuration->getPosition('ManufacturerName'$saleChannelId);
  47.         $weightPosition = (int)$this->configuration->getPosition('Weight'$saleChannelId);
  48.         $lengthPosition = (int)$this->configuration->getPluginConfig('WWeloEanDetailPageLengthDetailPosition'$saleChannelId);
  49.         $heightPosition = (int)$this->configuration->getPosition('Height'$saleChannelId);
  50.         $widthPosition = (int)$this->configuration->getPosition('Width'$saleChannelId);
  51.         /** @var SalesChannelProductEntity $product */
  52.         $product $event->getPage()->getProduct();
  53.         $data = [];
  54.         if ($eanActive && $product->getEan()) {
  55.             $data[] = [
  56.                 'label' => $this->configuration->translate('welo-ean-detail-page.detail.ean'),
  57.                 'position' => $eanPosition,
  58.                 'content' => $product->getEan(),
  59.                 'itemprop' => 'ean',
  60.             ];
  61.         }
  62.         if ($manufacturerActive && $product->getManufacturerNumber()) {
  63.             $data[] = [
  64.                 'label' => $this->configuration->translate('welo-ean-detail-page.detail.manufacturerNumber'),
  65.                 'position' => $manufacturerPosition,
  66.                 'content' => $product->getManufacturerNumber(),
  67.                 'itemprop' => 'manufacturer number',
  68.             ];
  69.         }
  70.         if ($manufacturerNameActive && $product->getManufacturer() && $product->getManufacturer()->getName()) {
  71.             $data[] = [
  72.                 'label' => $this->configuration->translate('welo-ean-detail-page.detail.manufacturerName'),
  73.                 'position' => $manufacturerNamePosition,
  74.                 'content' => $product->getManufacturer()->getName(),
  75.                 'itemprop' => 'manufacturer',
  76.             ];
  77.         }
  78.         if ($weightActive && $product->getWeight()) {
  79.             $data[] = [
  80.                 'label' => $this->configuration->translate('welo-ean-detail-page.detail.weight'),
  81.                 'position' => $weightPosition,
  82.                 'content' => $product->getWeight() . ' kg',
  83.                 'itemprop' => 'weight',
  84.             ];
  85.         }
  86.         if ($lengthActive && $product->getLength()) {
  87.             $data[] = [
  88.                 'label' => $this->configuration->translate('welo-ean-detail-page.detail.length'),
  89.                 'position' => $lengthPosition,
  90.                 'content' => $product->getLength() . ' mm',
  91.                 'itemprop' => 'length',
  92.             ];
  93.         }
  94.         if ($heightActive && $product->getHeight()) {
  95.             $data[] = [
  96.                 'label' => $this->configuration->translate('welo-ean-detail-page.detail.height'),
  97.                 'position' => $heightPosition,
  98.                 'content' => $product->getHeight() . ' mm',
  99.                 'itemprop' => 'height',
  100.             ];
  101.         }
  102.         if ($widthActive && $product->getWidth()) {
  103.             $data[] = [
  104.                 'label' => $this->configuration->translate('welo-ean-detail-page.detail.width'),
  105.                 'position' => $widthPosition,
  106.                 'content' => $product->getWidth() . ' mm',
  107.                 'itemprop' => 'width',
  108.             ];
  109.         }
  110.         usort($data, function($a$b) {
  111.             return $a['position'] <=> $b['position'];
  112.         });
  113.         $event->getPage()->getProduct()->assign(
  114.             [
  115.                 'WeloEanDetailData' => $data,
  116.             ]
  117.         );
  118.     }
  119. }