vendor/shopware/core/Content/Product/SalesChannel/Listing/ProductListingFeaturesSubscriber.php line 166

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Listing;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
  5. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  8. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  9. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  10. use Shopware\Core\Content\Product\SalesChannel\Exception\ProductSortingNotFoundException;
  11. use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingCollection;
  12. use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingEntity;
  13. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
  14. use Shopware\Core\Framework\Context;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\RepositoryIterator;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\FetchModeHelper;
  17. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\FilterAggregation;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\TermsAggregation;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\EntityAggregation;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MaxAggregation;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\StatsAggregation;
  24. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Bucket\TermsResult;
  25. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Metric\EntityResult;
  26. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  27. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  28. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  29. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  30. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  31. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  32. use Shopware\Core\Framework\Uuid\Uuid;
  33. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  34. use Shopware\Core\System\SystemConfig\SystemConfigService;
  35. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  36. use Symfony\Component\HttpFoundation\Request;
  37. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  38. class ProductListingFeaturesSubscriber implements EventSubscriberInterface
  39. {
  40.     public const DEFAULT_SEARCH_SORT 'score';
  41.     public const PROPERTY_GROUP_IDS_REQUEST_PARAM 'property-whitelist';
  42.     /**
  43.      * @var EntityRepositoryInterface
  44.      */
  45.     private $optionRepository;
  46.     /**
  47.      * @var EntityRepositoryInterface
  48.      */
  49.     private $sortingRepository;
  50.     /**
  51.      * @var Connection
  52.      */
  53.     private $connection;
  54.     /**
  55.      * @var SystemConfigService
  56.      */
  57.     private $systemConfigService;
  58.     /**
  59.      * @var EventDispatcherInterface
  60.      */
  61.     private $dispatcher;
  62.     public function __construct(
  63.         Connection $connection,
  64.         EntityRepositoryInterface $optionRepository,
  65.         EntityRepositoryInterface $productSortingRepository,
  66.         SystemConfigService $systemConfigService,
  67.         EventDispatcherInterface $dispatcher
  68.     ) {
  69.         $this->optionRepository $optionRepository;
  70.         $this->sortingRepository $productSortingRepository;
  71.         $this->connection $connection;
  72.         $this->systemConfigService $systemConfigService;
  73.         $this->dispatcher $dispatcher;
  74.     }
  75.     public static function getSubscribedEvents(): array
  76.     {
  77.         return [
  78.             ProductListingCriteriaEvent::class => [
  79.                 ['handleListingRequest'100],
  80.                 ['handleFlags', -100],
  81.             ],
  82.             ProductSuggestCriteriaEvent::class => [
  83.                 ['handleFlags', -100],
  84.             ],
  85.             ProductSearchCriteriaEvent::class => [
  86.                 ['handleSearchRequest'100],
  87.                 ['handleFlags', -100],
  88.             ],
  89.             ProductListingResultEvent::class => [
  90.                 ['handleResult'100],
  91.                 ['removeScoreSorting', -100],
  92.             ],
  93.             ProductSearchResultEvent::class => 'handleResult',
  94.         ];
  95.     }
  96.     public function handleFlags(ProductListingCriteriaEvent $event): void
  97.     {
  98.         $request $event->getRequest();
  99.         $criteria $event->getCriteria();
  100.         if ($request->get('no-aggregations')) {
  101.             $criteria->resetAggregations();
  102.         }
  103.         if ($request->get('only-aggregations')) {
  104.             // set limit to zero to fetch no products.
  105.             $criteria->setLimit(0);
  106.             // no total count required
  107.             $criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_NONE);
  108.             // sorting and association are only required for the product data
  109.             $criteria->resetSorting();
  110.             $criteria->resetAssociations();
  111.         }
  112.     }
  113.     public function handleListingRequest(ProductListingCriteriaEvent $event): void
  114.     {
  115.         $request $event->getRequest();
  116.         $criteria $event->getCriteria();
  117.         $context $event->getSalesChannelContext();
  118.         if (!$request->get('order')) {
  119.             $request->request->set('order'$this->getSystemDefaultSorting($context));
  120.         }
  121.         $criteria->addAssociation('options');
  122.         $this->handlePagination($request$criteria$event->getSalesChannelContext());
  123.         $this->handleFilters($request$criteria$context);
  124.         $this->handleSorting($request$criteria$context);
  125.     }
  126.     public function handleSearchRequest(ProductSearchCriteriaEvent $event): void
  127.     {
  128.         $request $event->getRequest();
  129.         $criteria $event->getCriteria();
  130.         $context $event->getSalesChannelContext();
  131.         if (!$request->get('order')) {
  132.             $request->request->set('order'self::DEFAULT_SEARCH_SORT);
  133.         }
  134.         $this->handlePagination($request$criteria$event->getSalesChannelContext());
  135.         $this->handleFilters($request$criteria$context);
  136.         $this->handleSorting($request$criteria$context);
  137.     }
  138.     public function handleResult(ProductListingResultEvent $event): void
  139.     {
  140.         $this->groupOptionAggregations($event);
  141.         $this->addCurrentFilters($event);
  142.         $result $event->getResult();
  143.         /** @var ProductSortingCollection $sortings */
  144.         $sortings $result->getCriteria()->getExtension('sortings');
  145.         $currentSortingKey $this->getCurrentSorting($sortings$event->getRequest())->getKey();
  146.         $result->setSorting($currentSortingKey);
  147.         $result->setAvailableSortings($sortings);
  148.         $result->setPage($this->getPage($event->getRequest()));
  149.         $result->setLimit($this->getLimit($event->getRequest(), $event->getSalesChannelContext()));
  150.     }
  151.     public function removeScoreSorting(ProductListingResultEvent $event): void
  152.     {
  153.         $sortings $event->getResult()->getAvailableSortings();
  154.         $defaultSorting $sortings->getByKey(self::DEFAULT_SEARCH_SORT);
  155.         if ($defaultSorting !== null) {
  156.             $sortings->remove($defaultSorting->getId());
  157.         }
  158.         $event->getResult()->setAvailableSortings($sortings);
  159.     }
  160.     private function handleFilters(Request $requestCriteria $criteriaSalesChannelContext $context): void
  161.     {
  162.         $criteria->addAssociation('manufacturer');
  163.         $filters $this->getFilters($request$context);
  164.         $aggregations $this->getAggregations($request$filters);
  165.         foreach ($aggregations as $aggregation) {
  166.             $criteria->addAggregation($aggregation);
  167.         }
  168.         foreach ($filters as $filter) {
  169.             if ($filter->isFiltered()) {
  170.                 $criteria->addPostFilter($filter->getFilter());
  171.             }
  172.         }
  173.         $criteria->addExtension('filters'$filters);
  174.     }
  175.     private function getAggregations(Request $requestFilterCollection $filters): array
  176.     {
  177.         $aggregations = [];
  178.         if ($request->get('reduce-aggregations') === null) {
  179.             foreach ($filters as $filter) {
  180.                 $aggregations array_merge($aggregations$filter->getAggregations());
  181.             }
  182.             return $aggregations;
  183.         }
  184.         foreach ($filters as $filter) {
  185.             $excluded $filters->filtered();
  186.             if ($filter->exclude()) {
  187.                 $excluded $excluded->blacklist($filter->getName());
  188.             }
  189.             foreach ($filter->getAggregations() as $aggregation) {
  190.                 if ($aggregation instanceof FilterAggregation) {
  191.                     $aggregation->addFilters($excluded->getFilters());
  192.                     $aggregations[] = $aggregation;
  193.                     continue;
  194.                 }
  195.                 $aggregation = new FilterAggregation(
  196.                     $aggregation->getName(),
  197.                     $aggregation,
  198.                     $excluded->getFilters()
  199.                 );
  200.                 $aggregations[] = $aggregation;
  201.             }
  202.         }
  203.         return $aggregations;
  204.     }
  205.     private function handlePagination(Request $requestCriteria $criteriaSalesChannelContext $context): void
  206.     {
  207.         $limit $this->getLimit($request$context);
  208.         $page $this->getPage($request);
  209.         $criteria->setOffset(($page 1) * $limit);
  210.         $criteria->setLimit($limit);
  211.         $criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT);
  212.     }
  213.     private function handleSorting(Request $requestCriteria $criteriaSalesChannelContext $context): void
  214.     {
  215.         /** @var ProductSortingCollection $sortings */
  216.         $sortings $criteria->getExtension('sortings') ?? new ProductSortingCollection();
  217.         $sortings->merge($this->getAvailableSortings($request$context->getContext()));
  218.         $currentSorting $this->getCurrentSorting($sortings$request);
  219.         $criteria->addSorting(
  220.             ...$currentSorting->createDalSorting()
  221.         );
  222.         $criteria->addExtension('sortings'$sortings);
  223.     }
  224.     private function getCurrentSorting(ProductSortingCollection $sortingsRequest $request): ProductSortingEntity
  225.     {
  226.         $key $request->get('order');
  227.         $sorting $sortings->getByKey($key);
  228.         if ($sorting !== null) {
  229.             return $sorting;
  230.         }
  231.         throw new ProductSortingNotFoundException($key);
  232.     }
  233.     private function getAvailableSortings(Request $requestContext $context): EntityCollection
  234.     {
  235.         $criteria = new Criteria();
  236.         $availableSortings $request->get('availableSortings');
  237.         $availableSortingsFilter = [];
  238.         if ($availableSortings) {
  239.             arsort($availableSortings\SORT_DESC \SORT_NUMERIC);
  240.             $availableSortingsFilter array_keys($availableSortings);
  241.             $criteria->addFilter(new EqualsAnyFilter('key'$availableSortingsFilter));
  242.         }
  243.         $criteria
  244.             ->addFilter(new EqualsFilter('active'true))
  245.             ->addSorting(new FieldSorting('priority''DESC'));
  246.         /** @var ProductSortingCollection $sortings */
  247.         $sortings $this->sortingRepository->search($criteria$context)->getEntities();
  248.         if ($availableSortings) {
  249.             $sortings->sortByKeyArray($availableSortingsFilter);
  250.         }
  251.         return $sortings;
  252.     }
  253.     private function getSystemDefaultSorting(SalesChannelContext $context): string
  254.     {
  255.         return $this->systemConfigService->getString(
  256.             'core.listing.defaultSorting',
  257.             $context->getSalesChannel()->getId()
  258.         );
  259.     }
  260.     private function collectOptionIds(ProductListingResultEvent $event): array
  261.     {
  262.         $aggregations $event->getResult()->getAggregations();
  263.         /** @var TermsResult|null $properties */
  264.         $properties $aggregations->get('properties');
  265.         /** @var TermsResult|null $options */
  266.         $options $aggregations->get('options');
  267.         $options $options $options->getKeys() : [];
  268.         $properties $properties $properties->getKeys() : [];
  269.         return array_unique(array_filter(array_merge($options$properties)));
  270.     }
  271.     private function groupOptionAggregations(ProductListingResultEvent $event): void
  272.     {
  273.         $ids $this->collectOptionIds($event);
  274.         if (empty($ids)) {
  275.             return;
  276.         }
  277.         $criteria = new Criteria($ids);
  278.         $criteria->setLimit(500);
  279.         $criteria->addAssociation('group');
  280.         $criteria->addAssociation('media');
  281.         $criteria->addFilter(new EqualsFilter('group.filterable'true));
  282.         $criteria->setTitle('product-listing::property-filter');
  283.         $criteria->addSorting(new FieldSorting('id'FieldSorting::ASCENDING));
  284.         $mergedOptions = new PropertyGroupOptionCollection();
  285.         $repositoryIterator = new RepositoryIterator($this->optionRepository$event->getContext(), $criteria);
  286.         while (($result $repositoryIterator->fetch()) !== null) {
  287.             $mergedOptions->merge($result->getEntities());
  288.         }
  289.         // group options by their property-group
  290.         $grouped $mergedOptions->groupByPropertyGroups();
  291.         $grouped->sortByPositions();
  292.         $grouped->sortByConfig();
  293.         $aggregations $event->getResult()->getAggregations();
  294.         // remove id results to prevent wrong usages
  295.         $aggregations->remove('properties');
  296.         $aggregations->remove('configurators');
  297.         $aggregations->remove('options');
  298.         $aggregations->add(new EntityResult('properties'$grouped));
  299.     }
  300.     private function addCurrentFilters(ProductListingResultEvent $event): void
  301.     {
  302.         $result $event->getResult();
  303.         $filters $result->getCriteria()->getExtension('filters');
  304.         if (!$filters instanceof FilterCollection) {
  305.             return;
  306.         }
  307.         foreach ($filters as $filter) {
  308.             $result->addCurrentFilter($filter->getName(), $filter->getValues());
  309.         }
  310.     }
  311.     private function getManufacturerIds(Request $request): array
  312.     {
  313.         $ids $request->query->get('manufacturer''');
  314.         if ($request->isMethod(Request::METHOD_POST)) {
  315.             $ids $request->request->get('manufacturer''');
  316.         }
  317.         if (\is_string($ids)) {
  318.             $ids explode('|'$ids);
  319.         }
  320.         return array_filter((array) $ids);
  321.     }
  322.     private function getPropertyIds(Request $request): array
  323.     {
  324.         $ids $request->query->get('properties''');
  325.         if ($request->isMethod(Request::METHOD_POST)) {
  326.             $ids $request->request->get('properties''');
  327.         }
  328.         if (\is_string($ids)) {
  329.             $ids explode('|'$ids);
  330.         }
  331.         return array_filter((array) $ids);
  332.     }
  333.     private function getLimit(Request $requestSalesChannelContext $context): int
  334.     {
  335.         $limit $request->query->getInt('limit'0);
  336.         if ($request->isMethod(Request::METHOD_POST)) {
  337.             $limit $request->request->getInt('limit'$limit);
  338.         }
  339.         $limit $limit $limit $this->systemConfigService->getInt('core.listing.productsPerPage'$context->getSalesChannel()->getId());
  340.         return $limit <= 24 $limit;
  341.     }
  342.     private function getPage(Request $request): int
  343.     {
  344.         $page $request->query->getInt('p'1);
  345.         if ($request->isMethod(Request::METHOD_POST)) {
  346.             $page $request->request->getInt('p'$page);
  347.         }
  348.         return $page <= $page;
  349.     }
  350.     private function getFilters(Request $requestSalesChannelContext $context): FilterCollection
  351.     {
  352.         $filters = new FilterCollection();
  353.         $filters->add($this->getManufacturerFilter($request));
  354.         $filters->add($this->getPriceFilter($request));
  355.         $filters->add($this->getRatingFilter($request));
  356.         $filters->add($this->getShippingFreeFilter($request));
  357.         $filters->add($this->getPropertyFilter($request));
  358.         if (!$request->request->get('manufacturer-filter'true)) {
  359.             $filters->remove('manufacturer');
  360.         }
  361.         if (!$request->request->get('price-filter'true)) {
  362.             $filters->remove('price');
  363.         }
  364.         if (!$request->request->get('rating-filter'true)) {
  365.             $filters->remove('rating');
  366.         }
  367.         if (!$request->request->get('shipping-free-filter'true)) {
  368.             $filters->remove('shipping-free');
  369.         }
  370.         if (!$request->request->get('property-filter'true)) {
  371.             $filters->remove('properties');
  372.             if (\count($propertyWhitelist $request->request->all(self::PROPERTY_GROUP_IDS_REQUEST_PARAM))) {
  373.                 $filters->add($this->getPropertyFilter($request$propertyWhitelist));
  374.             }
  375.         }
  376.         $event = new ProductListingCollectFilterEvent($request$filters$context);
  377.         $this->dispatcher->dispatch($event);
  378.         return $filters;
  379.     }
  380.     private function getManufacturerFilter(Request $request): Filter
  381.     {
  382.         $ids $this->getManufacturerIds($request);
  383.         return new Filter(
  384.             'manufacturer',
  385.             !empty($ids),
  386.             [new EntityAggregation('manufacturer''product.manufacturerId''product_manufacturer')],
  387.             new EqualsAnyFilter('product.manufacturerId'$ids),
  388.             $ids
  389.         );
  390.     }
  391.     private function getPropertyFilter(Request $request, ?array $groupIds null): Filter
  392.     {
  393.         $ids $this->getPropertyIds($request);
  394.         $propertyAggregation = new TermsAggregation('properties''product.properties.id');
  395.         $optionAggregation = new TermsAggregation('options''product.options.id');
  396.         if ($groupIds) {
  397.             $propertyAggregation = new FilterAggregation(
  398.                 'properties-filter',
  399.                 $propertyAggregation,
  400.                 [new EqualsAnyFilter('product.properties.groupId'$groupIds)]
  401.             );
  402.             $optionAggregation = new FilterAggregation(
  403.                 'options-filter',
  404.                 $optionAggregation,
  405.                 [new EqualsAnyFilter('product.options.groupId'$groupIds)]
  406.             );
  407.         }
  408.         if (empty($ids)) {
  409.             return new Filter(
  410.                 'properties',
  411.                 false,
  412.                 [$propertyAggregation$optionAggregation],
  413.                 new MultiFilter(MultiFilter::CONNECTION_OR, []),
  414.                 [],
  415.                 false
  416.             );
  417.         }
  418.         $grouped $this->connection->fetchAll(
  419.             'SELECT LOWER(HEX(property_group_id)) as property_group_id, LOWER(HEX(id)) as id
  420.              FROM property_group_option
  421.              WHERE id IN (:ids)',
  422.             ['ids' => Uuid::fromHexToBytesList($ids)],
  423.             ['ids' => Connection::PARAM_STR_ARRAY]
  424.         );
  425.         $grouped FetchModeHelper::group($grouped);
  426.         $filters = [];
  427.         foreach ($grouped as $options) {
  428.             $options array_column($options'id');
  429.             $filters[] = new MultiFilter(
  430.                 MultiFilter::CONNECTION_OR,
  431.                 [
  432.                     new EqualsAnyFilter('product.optionIds'$options),
  433.                     new EqualsAnyFilter('product.propertyIds'$options),
  434.                 ]
  435.             );
  436.         }
  437.         return new Filter(
  438.             'properties',
  439.             true,
  440.             [$propertyAggregation$optionAggregation],
  441.             new MultiFilter(MultiFilter::CONNECTION_AND$filters),
  442.             $ids,
  443.             false
  444.         );
  445.     }
  446.     private function getPriceFilter(Request $request): Filter
  447.     {
  448.         $min $request->get('min-price');
  449.         $max $request->get('max-price');
  450.         $range = [];
  451.         if ($min !== null && $min >= 0) {
  452.             $range[RangeFilter::GTE] = $min;
  453.         }
  454.         if ($max !== null && $max >= 0) {
  455.             $range[RangeFilter::LTE] = $max;
  456.         }
  457.         return new Filter(
  458.             'price',
  459.             !empty($range),
  460.             [new StatsAggregation('price''product.cheapestPrice'truetruefalsefalse)],
  461.             new RangeFilter('product.cheapestPrice'$range),
  462.             [
  463.                 'min' => (float) $request->get('min-price'),
  464.                 'max' => (float) $request->get('max-price'),
  465.             ]
  466.         );
  467.     }
  468.     private function getRatingFilter(Request $request): Filter
  469.     {
  470.         $filtered $request->get('rating');
  471.         return new Filter(
  472.             'rating',
  473.             $filtered !== null,
  474.             [
  475.                 new FilterAggregation(
  476.                     'rating-exists',
  477.                     new MaxAggregation('rating''product.ratingAverage'),
  478.                     [new RangeFilter('product.ratingAverage', [RangeFilter::GTE => 0])]
  479.                 ),
  480.             ],
  481.             new RangeFilter('product.ratingAverage', [
  482.                 RangeFilter::GTE => (int) $filtered,
  483.             ]),
  484.             $filtered
  485.         );
  486.     }
  487.     private function getShippingFreeFilter(Request $request): Filter
  488.     {
  489.         $filtered = (bool) $request->get('shipping-free'false);
  490.         return new Filter(
  491.             'shipping-free',
  492.             $filtered === true,
  493.             [
  494.                 new FilterAggregation(
  495.                     'shipping-free-filter',
  496.                     new MaxAggregation('shipping-free''product.shippingFree'),
  497.                     [new EqualsFilter('product.shippingFree'true)]
  498.                 ),
  499.             ],
  500.             new EqualsFilter('product.shippingFree'true),
  501.             $filtered
  502.         );
  503.     }
  504. }