custom/static-plugins/EfbStorefront/Subscriber/ProductListingSubscriber.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Uandi\EFB\Storefront\Subscriber;
  3. use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
  4. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  5. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  6. use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingCollection;
  7. use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingEntity;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ProductListingSubscriber implements EventSubscriberInterface
  10. {
  11.     private const PRICE_FILTER_NAME 'price';
  12.     private const DISABLE_SORTING_CUSTOM_FIELD_KEY 'product_listing_disable_sorting';
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             ProductListingCollectFilterEvent::class => 'removePriceFilter',
  17.             ProductListingResultEvent::class => 'removeSortingForNotLoggedInUser',
  18.             ProductSearchResultEvent::class => 'removeSortingForNotLoggedInUser',
  19.         ];
  20.     }
  21.     public function removePriceFilter(ProductListingCollectFilterEvent $event): void
  22.     {
  23.         if ($event->getSalesChannelContext()->getCustomer()) {
  24.             return;
  25.         }
  26.         $filters $event->getFilters();
  27.         if ($filters->has(self::PRICE_FILTER_NAME)) {
  28.             $filters->remove(self::PRICE_FILTER_NAME);
  29.         }
  30.     }
  31.     public function removeSortingForNotLoggedInUser(ProductListingResultEvent $event): void
  32.     {
  33.         if ($event->getSalesChannelContext()->getCustomer()) {
  34.             return;
  35.         }
  36.         $customFields $event->getSalesChannelContext()->getSalesChannel()->getTranslation('customFields');
  37.         if (null === $customFields) {
  38.             return;
  39.         }
  40.         $sortingKeys $customFields[self::DISABLE_SORTING_CUSTOM_FIELD_KEY] ?? null;
  41.         if (null === $sortingKeys) {
  42.             return;
  43.         }
  44.         $sortingKeys array_map('trim'explode(';'$sortingKeys));
  45.         $listingResult $event->getResult();
  46.         $this->filterAndReduceSorting($listingResult->getAvailableSortings(), $sortingKeys);
  47.         if (false === in_array($listingResult->getSorting(), $sortingKeys)) {
  48.             return;
  49.         }
  50.         $listingResult->setSorting(null);
  51.     }
  52.     private function filterAndReduceSorting(ProductSortingCollection $availableSortings, array $sortingKeys): void
  53.     {
  54.         $matchingSortings $availableSortings->filter(static function (ProductSortingEntity $sorting) use ($sortingKeys) {
  55.             return in_array($sorting->getKey(), $sortingKeys);
  56.         });
  57.         if (>= $matchingSortings->count())  {
  58.             return;
  59.         }
  60.         foreach ($matchingSortings as $sorting) {
  61.             $availableSortings->remove($sorting->getId());
  62.         }
  63.     }
  64. }