custom/plugins/LeopardSearchExtension/src/Subscriber/ProductSearchCriteriaSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * LeopardSearchExtension
  5.  * Copyright (c) Die Leoparden GmbH
  6.  */
  7. namespace LeopardSearchExtension\Subscriber;
  8. use LeopardSearchExtension\LeopardSearchExtension;
  9. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  10. use Shopware\Core\Content\Product\ProductEntity;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. /**
  13.  * Class ProductSearchCriteriaSubscriber
  14.  */
  15. class ProductSearchCriteriaSubscriber extends AbstractSubscriber
  16. {
  17.     /**
  18.      * {@inheritDoc}
  19.      */
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             ProductSearchCriteriaEvent::class => 'onProductSearchCriteria',
  24.         ];
  25.     }
  26.     /**
  27.      * @param ProductSearchCriteriaEvent $event
  28.      *
  29.      * @return void
  30.      */
  31.     public function onProductSearchCriteria(ProductSearchCriteriaEvent $event): void
  32.     {
  33.         /** @var ProductEntity|null $productEntity */
  34.         $productEntity $this->onSearchCriteria($event);
  35.         if (!$productEntity instanceof ProductEntity) {
  36.             return;
  37.         }
  38.         $event
  39.             ->getRequest()
  40.             ->attributes
  41.             ->set(LeopardSearchExtension::KERNEL_RESPONSE_ATTRIBUTEtrue);
  42.         (new RedirectResponse($this->getRouter()
  43.             ->generate('frontend.detail.page', ['productId' => $productEntity->getId()])
  44.         ))->send();
  45.     }
  46. }