custom/plugins/KplngiProductOrder/src/Subscriber/ListingCriteria.php line 64

Open in your IDE?
  1. <?php
  2. namespace Kplngi\ProductOrder\Subscriber;
  3. use Kplngi\ProductOrder\Position\CategoryIdHelper;
  4. use Kplngi\ProductOrder\Position\DisplayInStorefront;
  5. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  11. use Shopware\Core\Framework\Event\ShopwareEvent;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\ParameterBag;
  15. use Symfony\Component\HttpFoundation\Request;
  16. class ListingCriteria implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var CategoryIdHelper
  20.      */
  21.     private $categoryIdHelper;
  22.     /**
  23.      * @var EntityRepositoryInterface
  24.      */
  25.     private $categoryRepository;
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     private $orderActiveRepository;
  30.     public function __construct(
  31.         CategoryIdHelper $categoryIdHelper,
  32.         EntityRepositoryInterface $categoryRepository,
  33.         EntityRepositoryInterface $orderActiveRepository
  34.     )
  35.     {
  36.         $this->categoryIdHelper $categoryIdHelper;
  37.         $this->categoryRepository $categoryRepository;
  38.         $this->orderActiveRepository $orderActiveRepository;
  39.     }
  40.     public static function getSubscribedEvents()
  41.     {
  42.         return [
  43.             ProductListingCriteriaEvent::class => 'addCriteria',
  44.             ProductListingResultEvent::class => 'handleCustomSorting',
  45.         ];
  46.     }
  47.     public function handleCustomSorting(ProductListingResultEvent $event)
  48.     {
  49.         $categoryId $this->getNavigationId($event->getRequest(), $event->getSalesChannelContext());
  50.         if ($this->isCustomOrderListing($event$categoryId)) {
  51.             $event->getResult()->setSorting('');
  52.         }
  53.     }
  54.     public function addCriteria(ProductListingCriteriaEvent $productListingCriteriaEvent)
  55.     {
  56.         $categoryId $this->getNavigationId($productListingCriteriaEvent->getRequest(), $productListingCriteriaEvent->getSalesChannelContext());
  57.         if (empty($categoryId)) {
  58.             return;
  59.         }
  60.         if (!$this->isCustomOrderListing($productListingCriteriaEvent$categoryId)) {
  61.             return;
  62.         }
  63.         $productListingCriteriaEvent->getSalesChannelContext()->addExtension('kplngi_displaySorting', (new DisplayInStorefront())->assign(['displaySorting' => false]));
  64.         $this->categoryIdHelper->setCategoryId($categoryId);
  65.         $productListingCriteriaEvent->getCriteria()->resetSorting();
  66.         $productListingCriteriaEvent->getCriteria()->addSorting(new FieldSorting('kplngiPositions.position'FieldSorting::DESCENDING));
  67.         $productListingCriteriaEvent->getCriteria()->addSorting(new FieldSorting('product.name'FieldSorting::ASCENDING));
  68.     }
  69.     private function isCustomOrderListing(ShopwareEvent $event$categoryId): bool
  70.     {
  71.         /** @var ParameterBag */
  72.         $query $event->getRequest()->query;
  73.         if ($query->get('order')) {
  74.             return false;
  75.         }
  76.         $criteria = new Criteria();
  77.         $criteria->addFilter(new EqualsFilter('categoryId'$categoryId));
  78.         $orderActive $this->orderActiveRepository->search($criteria$event->getContext());
  79.         if ($orderActive->getTotal() === 0) {
  80.             return false;
  81.         }
  82.         return true;
  83.     }
  84.     private function getNavigationId(Request $requestSalesChannelContext $salesChannelContext): string
  85.     {
  86.         $params $request->attributes->get('_route_params');
  87.         if ($params && isset($params['navigationId'])) {
  88.             return $params['navigationId'];
  89.         } elseif ($params && isset($params['categoryId'])) {
  90.             return $params['categoryId'];
  91.         }
  92.         return $salesChannelContext->getSalesChannel()->getNavigationCategoryId();
  93.     }
  94. }