<?php
declare(strict_types=1);
namespace WbfkExtensions\Subscriber;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WbfkExtensions\Core\Content\ProductComparison\ProductComparisonService;
class AddComparisonListToStorefrontSubscriber implements EventSubscriberInterface
{
private const COMPARISON_LIST_REQUIRED_ROUTES = [
"frontend.detail.page",
"frontend.cms.buybox.switch",
];
public function __construct(private readonly ProductComparisonService $productComparisonService)
{
}
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => 'addComparisonListToStorefront'
];
}
public function addComparisonListToStorefront(StorefrontRenderEvent $event): void
{
$currentRoute = $event->getRequest()->attributes->get('_route');
if (!in_array($currentRoute, self::COMPARISON_LIST_REQUIRED_ROUTES)) {
return;
}
$event->setParameter(
'comparisonList',
$this->productComparisonService->getComparisonList($event->getRequest()->getSession())
);
}
}