custom/plugins/GrimmTheme/src/Storefront/Subscriber/ProductListingSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace GrimmTheme\Storefront\Subscriber;
  3. use Shopware\Core\Content\Product\ProductEvents;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  7. class ProductListingSubscriber implements EventSubscriberInterface
  8. {
  9.     private static array $mimeTypeFilter = [
  10.         'application/pdf'
  11.     ];
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [
  15. //            ProductEvents::PRODUCT_LOADED_EVENT => 'onProductList',
  16.             ProductListingCriteriaEvent::class => 'handleListingRequest',
  17.         ];
  18.     }
  19.     public function handleListingRequest(ProductListingCriteriaEvent $event): void
  20.     {
  21.         $event->getCriteria()->addAssociation('properties.group');
  22.     }
  23.     public function onProductList(EntityLoadedEvent $event): void
  24.     {
  25.         if (!($event->getContext()->getSource() instanceof \Shopware\Core\Framework\Api\Context\SalesChannelApiSource)) {
  26.             return;
  27.         }
  28.         foreach ($event->getEntities() as $productEntity) {
  29.             /** @var SalesChannelProductEntity $productEntity */
  30.             $properties $productEntity->getProperties();
  31.             $grouped $properties->groupByPropertyGroups();
  32.             $grouped->sortByPositions();
  33.             $grouped->sortByConfig();
  34.             $productEntity->setSortedProperties($grouped);
  35.         }
  36.     }
  37. }