custom/plugins/ZweiPunktSeoKampfpreise/src/Subscriber/ProductExportSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace ZweiPunktSeoKampfpreise\Subscriber;
  4. use Shopware\Core\Content\ProductExport\Event\ProductExportRenderBodyContextEvent;
  5. use Shopware\Core\Content\ProductExport\ProductExportEntity;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use ZweiPunktSeoKampfpreise\Service\ConfigService;
  8. /**
  9.  * 1. Set correct SalesChannelId
  10.  * The SalesChannelId in the SalesChannelContext will be overwritten in the
  11.  * product export generation to reflect the product informations from the
  12.  * storefront saleschannel, but our plugin needs the salesChannelId from the
  13.  * product export and not the corresponding storefront.
  14.  */
  15. class ProductExportSubscriber implements EventSubscriberInterface
  16. {
  17.     private ConfigService $configService;
  18.     public function __construct(ConfigService $configService)
  19.     {
  20.         $this->configService $configService;
  21.     }
  22.     /**
  23.      * @return array<string, string>
  24.      */
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             ProductExportRenderBodyContextEvent::class => "onProductExport"
  29.         ];
  30.     }
  31.     public function onProductExport(ProductExportRenderBodyContextEvent $event): void
  32.     {
  33.         $context $event->getContext();
  34.         $productExport $context['productExport'] ?? false;
  35.         if (!$productExport instanceof ProductExportEntity) {
  36.             return;
  37.         }
  38.         $this->configService->setProductExportId(
  39.             $productExport->getSalesChannelId()
  40.         );
  41.     }
  42. }