custom/plugins/WbfkExtensions/src/Subscriber/AddComparisonListToStorefrontSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Shopware\Storefront\Event\StorefrontRenderEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use WbfkExtensions\Core\Content\ProductComparison\ProductComparisonService;
  7. class AddComparisonListToStorefrontSubscriber implements EventSubscriberInterface
  8. {
  9.     private const COMPARISON_LIST_REQUIRED_ROUTES = [
  10.         "frontend.detail.page",
  11.         "frontend.cms.buybox.switch",
  12.     ];
  13.     public function __construct(private readonly ProductComparisonService $productComparisonService)
  14.     {
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             StorefrontRenderEvent::class => 'addComparisonListToStorefront'
  20.         ];
  21.     }
  22.     public function addComparisonListToStorefront(StorefrontRenderEvent $event): void
  23.     {
  24.         $currentRoute $event->getRequest()->attributes->get('_route');
  25.         if (!in_array($currentRouteself::COMPARISON_LIST_REQUIRED_ROUTES)) {
  26.             return;
  27.         }
  28.         $event->setParameter(
  29.             'comparisonList',
  30.             $this->productComparisonService->getComparisonList($event->getRequest()->getSession())
  31.         );
  32.     }
  33. }