vendor/crehler/advanced-sort/src/Subscriber/ProductSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @copyright 2023 Crehler Sp. z o.o.
  5.  *
  6.  * https://crehler.com/
  7.  * support@crehler.com
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\AdvancedSort\Subscriber;
  13. use Crehler\AdvancedSort\Services\PositionConfigService;
  14. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  15. use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingCollection;
  16. use Shopware\Core\Framework\Context;
  17. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. use Symfony\Component\HttpFoundation\Request;
  25. class ProductSubscriber implements EventSubscriberInterface
  26. {
  27.     private PositionConfigService $positionConfigService;
  28.     private EntityRepository $sortingRepository;
  29.     public function __construct(
  30.         PositionConfigService $positionConfigService,
  31.         EntityRepository $productSortingRepository)
  32.     {
  33.         $this->positionConfigService $positionConfigService;
  34.         $this->sortingRepository $productSortingRepository;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             ProductListingCriteriaEvent::class => ['handleProductListingCriteria', -170]
  40.         ];
  41.     }
  42.     public function handleProductListingCriteria(ProductListingCriteriaEvent $event): void
  43.     {
  44.         $request $event->getRequest();
  45.         if (!$request->query->get('order'false) && $this->positionConfigService->getDefaultSorting($event->getSalesChannelContext())) {
  46.             $availableSorting $this->getAvailableSorting($request$event->getContext());
  47.             if ($this->sortingAvailable($availableSorting$this->positionConfigService->getDefaultSorting($event->getSalesChannelContext()))) {
  48. //                $request->query->set('order', $this->positionConfigService->getDefaultSorting($event->getSalesChannelContext()));
  49.             }
  50.         }
  51.     }
  52.     private function getAvailableSorting(Request $requestContext $context): EntityCollection
  53.     {
  54.         $criteria = new Criteria();
  55.         $availableSorting $request->get('availableSortings');
  56.         $availableSortingFilter = [];
  57.         if ($availableSorting) {
  58.             if ($request->get("order") === "position" && !isset($availableSorting['position'])) {
  59.                 $availableSorting += [ "position" => ];
  60.                 $request->request->set("availableSortings"$availableSorting);
  61.             }
  62.             arsort($availableSorting\SORT_DESC \SORT_NUMERIC);
  63.             $availableSortingFilter array_keys($availableSorting);
  64.             $criteria->addFilter(new EqualsAnyFilter('key'$availableSortingFilter));
  65.         }
  66.         $criteria
  67.             ->addFilter(new EqualsFilter('active'true))
  68.             ->addSorting(new FieldSorting('priority''DESC'));
  69.         /** @var ProductSortingCollection $sortings */
  70.         $sortings $this->sortingRepository->search($criteria$context)->getEntities();
  71.         if ($availableSorting) {
  72.             $sortings->sortByKeyArray($availableSortingFilter);
  73.         }
  74.         return $sortings;
  75.     }
  76.     private function sortingAvailable($availableSorting$defaultSorting): bool
  77.     {
  78.         if (!empty($availableSorting) and $availableSorting instanceof ProductSortingCollection) {
  79.             $elements $availableSorting->getElements();
  80.             foreach ($elements as $aSortElement) {
  81.                 if ($defaultSorting === $aSortElement->getKey()) {
  82.                     return true;
  83.                 }
  84.             }
  85.         }
  86.         return false;
  87.     }
  88. }