/** * Apply sort params to the collection. * * @param \Magento\Catalog\Model\Layer $layer Catalog / search layer. * @param \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection Product collection. * * @return $this */ private function setSortParams(\Magento\Catalog\Model\Layer $layer, \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection) { $searchQuery = $this->queryFactory->get(); if (!$searchQuery->getQueryText() && $layer->getCurrentCategory()) { $categoryId = $layer->getCurrentCategory()->getId(); $collection->addSortFilterParameters('position', 'category.position', 'category', ['category.category_id' => $categoryId]); } return $this; }