<?php
declare(strict_types=1);
namespace EsmComputer\Storefront\Subscriber;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ListingTextSubscriber implements EventSubscriberInterface
{
protected EntityRepository $listingTextRepository;
public function __construct(EntityRepository $listingTextRepository)
{
$this->listingTextRepository = $listingTextRepository;
}
public static function getSubscribedEvents()
{
return [
ProductListingResultEvent::class => 'onProductListingResult'
];
}
/**
* Add seo listing text to view
*
* @param ProductListingResultEvent $event
*/
public function onProductListingResult(ProductListingResultEvent $event)
{
$categoryId = $event->getRequest()->get('navigationId');
if(!$categoryId) {
return;
}
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('categoryId', $categoryId));
$seoListing = $this->listingTextRepository->search($criteria, $event->getContext());
$event->getResult()->addExtension('seoListingTexts', $seoListing);
}
}