vendor/store.shopware.com/swagenterprisesearchplatform/src/Product/ProductSearchResultSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\EnterpriseSearch\Product;
  3. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  4. use Shopware\Core\Content\Product\ProductEntity;
  5. use Shopware\Core\Content\Product\ProductEvents;
  6. use Shopware\Core\Framework\Struct\ArrayEntity;
  7. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  8. use Swag\EnterpriseSearch\Common\SesExtension;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ProductSearchResultSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var SalesChannelRepositoryInterface
  14.      */
  15.     private $repository;
  16.     public function __construct(SalesChannelRepositoryInterface $repository)
  17.     {
  18.         $this->repository $repository;
  19.     }
  20.     /**
  21.      * {@inheritdoc}
  22.      */
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             ProductEvents::PRODUCT_SEARCH_RESULT => 'onProductSearchResult',
  27.         ];
  28.     }
  29.     public function onProductSearchResult(ProductSearchResultEvent $event): void
  30.     {
  31.         if (!SesExtension::isExplanation($event->getContext())) {
  32.             return;
  33.         }
  34.         $ids $this->repository->searchIds($event->getResult()->getCriteria(), $event->getSalesChannelContext());
  35.         $products $event->getResult();
  36.         /** @var string $id */
  37.         foreach ($ids->getIds() as $id) {
  38.             if (!$products->has($id)) {
  39.                 continue;
  40.             }
  41.             /** @var ProductEntity $product */
  42.             $product $products->get($id);
  43.             // get access to the data of the search result
  44.             $product->addExtension('search', new ArrayEntity($ids->getDataOfId($id)));
  45.         }
  46.     }
  47. }