custom/plugins/MbdusArticleSort/src/Subscriber/ListingCriteria.php line 59

Open in your IDE?
  1. <?php
  2. namespace MbdusArticleSort\Subscriber;
  3. use MbdusArticleSort\Product\CategoryIdHelper;
  4. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Doctrine\DBAL\Connection;
  14. class ListingCriteria implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var CategoryIdHelper
  18.      */
  19.     private $categoryIdHelper;
  20.     /**
  21.      * @var EntityRepositoryInterface
  22.      */
  23.     private $categoryRepository;
  24.     
  25.     /**
  26.      * @var SystemConfigService
  27.      */
  28.     private $systemConfigService;
  29.     
  30.     /**
  31.      * @var Connection
  32.      */
  33.     private $connection;
  34.     public function __construct(
  35.         CategoryIdHelper $categoryIdHelper,
  36.         EntityRepositoryInterface $categoryRepository,
  37.         SystemConfigService $systemConfigService,
  38.         Connection $connection
  39.         )
  40.     {
  41.         $this->categoryIdHelper $categoryIdHelper;
  42.         $this->categoryRepository $categoryRepository;
  43.         $this->systemConfigService $systemConfigService;
  44.         $this->connection $connection;
  45.     }
  46.     public static function getSubscribedEvents()
  47.     {
  48.         return [
  49.             ProductListingCriteriaEvent::class => 'addCriteria'
  50.         ];
  51.     }
  52.     public function addCriteria(ProductListingCriteriaEvent $productListingCriteriaEvent)
  53.     {
  54.         $categoryId $this->getNavigationId($productListingCriteriaEvent->getRequest(), $productListingCriteriaEvent->getSalesChannelContext());
  55.        
  56.         if (empty($categoryId)) {
  57.             return;
  58.         }
  59.         $criteria = new Criteria();
  60.         $criteria->addFilter(new EqualsFilter('categoryId'$categoryId));
  61.         $this->categoryIdHelper->setCategoryId($categoryId);
  62.         
  63.         $request $productListingCriteriaEvent->getRequest();
  64.         
  65.         $defaultChannelOrder $this->getSystemDefaultSorting($productListingCriteriaEvent->getSalesChannelContext());
  66.         
  67.         $defaultSortOrder $this->systemConfigService->get('MbdusArticleSort.config.mbdusDefaultSortOrder') ?? $defaultChannelOrder;
  68.         
  69.         if($defaultSortOrder == "null"){
  70.             $defaultSortOrder $defaultChannelOrder;
  71.         }
  72.         
  73.         $order $request->query->get('order');
  74.         
  75.         $isManualSorted $this->isManualSorted($categoryId);
  76.         
  77.         echo $order."|".$defaultSortOrder."|".$defaultChannelOrder."<br>";
  78.        
  79.         if((!$order || $defaultSortOrder == $order) && $isManualSorted){
  80.             $productListingCriteriaEvent->getCriteria()->resetSorting();
  81.             $productListingCriteriaEvent->getCriteria()->addSorting(
  82.                 new FieldSorting('mbdusSort.sort'FieldSorting::ASCENDING)
  83.             );
  84.         }
  85.     }
  86.     
  87.     public function isManualSorted($categoryId){
  88.         $query $this->connection->createQueryBuilder();
  89.         $query->select('id');
  90.         $query->from('mbdus_productcategoriessort''sorting');
  91.         $query->where('HEX(sorting.category_id) LIKE :categoryId');
  92.         $query->setParameter('categoryId'$categoryId);
  93.         $result $query->execute()->fetchAll();
  94.         
  95.         if(empty($result)){
  96.             return false;
  97.         }
  98.         return true;
  99.     }
  100.     
  101.     private function getSystemDefaultSorting(SalesChannelContext $context): string
  102.     {
  103.         return $this->systemConfigService->getString(
  104.             'core.listing.defaultSorting',
  105.             $context->getSalesChannel()->getId()
  106.             );
  107.     }
  108.     private function getNavigationId(Request $requestSalesChannelContext $salesChannelContext): string
  109.     {
  110.         $params $request->attributes->get('_route_params');
  111.         if ($params && isset($params['navigationId'])) {
  112.             return $params['navigationId'];
  113.         }
  114.         return $salesChannelContext->getSalesChannel()->getNavigationCategoryId();
  115.     }
  116. }