<?php
namespace GrimmTheme\Storefront\Subscriber;
use Shopware\Core\Content\Product\ProductEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
class ProductListingSubscriber implements EventSubscriberInterface
{
private static array $mimeTypeFilter = [
'application/pdf'
];
public static function getSubscribedEvents(): array
{
return [
// ProductEvents::PRODUCT_LOADED_EVENT => 'onProductList',
ProductListingCriteriaEvent::class => 'handleListingRequest',
];
}
public function handleListingRequest(ProductListingCriteriaEvent $event): void
{
$event->getCriteria()->addAssociation('properties.group');
}
public function onProductList(EntityLoadedEvent $event): void
{
if (!($event->getContext()->getSource() instanceof \Shopware\Core\Framework\Api\Context\SalesChannelApiSource)) {
return;
}
foreach ($event->getEntities() as $productEntity) {
/** @var SalesChannelProductEntity $productEntity */
$properties = $productEntity->getProperties();
$grouped = $properties->groupByPropertyGroups();
$grouped->sortByPositions();
$grouped->sortByConfig();
$productEntity->setSortedProperties($grouped);
}
}
}