<?php
declare(strict_types=1);
namespace ZweiPunktSeoKampfpreise\Subscriber;
use Shopware\Core\Content\ProductExport\Event\ProductExportRenderBodyContextEvent;
use Shopware\Core\Content\ProductExport\ProductExportEntity;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use ZweiPunktSeoKampfpreise\Service\ConfigService;
/**
* 1. Set correct SalesChannelId
* The SalesChannelId in the SalesChannelContext will be overwritten in the
* product export generation to reflect the product informations from the
* storefront saleschannel, but our plugin needs the salesChannelId from the
* product export and not the corresponding storefront.
*/
class ProductExportSubscriber implements EventSubscriberInterface
{
private ConfigService $configService;
public function __construct(ConfigService $configService)
{
$this->configService = $configService;
}
/**
* @return array<string, string>
*/
public static function getSubscribedEvents(): array
{
return [
ProductExportRenderBodyContextEvent::class => "onProductExport"
];
}
public function onProductExport(ProductExportRenderBodyContextEvent $event): void
{
$context = $event->getContext();
$productExport = $context['productExport'] ?? false;
if (!$productExport instanceof ProductExportEntity) {
return;
}
$this->configService->setProductExportId(
$productExport->getSalesChannelId()
);
}
}