diff --git a/app/code/Magento/Elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php b/app/code/Magento/Elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php index 97cb92ab3b06..5b6103a65314 100644 --- a/app/code/Magento/Elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php +++ b/app/code/Magento/Elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php @@ -211,7 +211,6 @@ private function categoryProductByCustomSortOrder(int $categoryId): array $searchCriteria = $this->searchResult->getSearchCriteria(); $sortOrders = $searchCriteria->getSortOrders() ?? []; $sortOrders = array_merge(['is_salable' => \Magento\Framework\DB\Select::SQL_DESC], $sortOrders); - $connection = $this->collection->getConnection(); $query = clone $connection->select() ->reset(\Magento\Framework\DB\Select::ORDER) @@ -231,6 +230,14 @@ private function categoryProductByCustomSortOrder(int $categoryId): array . ' AND cat_index.store_id = ' . $storeId, ['cat_index.position'] ); + + $productIds = []; + foreach ($this->searchResult->getItems() as $item) { + $productIds[] = $item->getId(); + } + + $query->where('e.entity_id IN(?)', $productIds); + foreach ($sortOrders as $field => $dir) { if ($field === 'name') { $entityTypeId = $this->collection->getEntity()->getTypeId();