0

I'm trying sort products in category page by position. But Magento2 does not react to changes.

enter image description here

Any ideas how can I fix it?

  • How do you take the collection? Try adding addAttributeToSort('attribute_code', 'sort_order') – Evgeniy Kapelko Feb 24 '18 at 16:21
2

It is Magento2.2 default bug. Please follow this to solve your problem.

Create etc/di.xml file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection" type="Vendor\Module\Model\ResourceModel\Fulltext\Collection" />
</config>

Create Vendor\Module\Model\ResourceModel\Fulltext\Collection.php file. Copy core Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection.php file in your module and put below code in that file.

Replace line no. 366 to 388 in Collection.php.

            'search_result.'. TemporaryStorage::FIELD_SCORE . ' ' . $this->relevanceOrderDirection
        );
    }
    return parent::_renderFiltersBefore();
}

/**
 * @inheritdoc
 */
protected function _beforeLoad()
{
    /*
    * This order is required to force search results be the same
    * for the same requests and products with the same relevance
    * NOTE: this does not replace existing orders but ADDs one more
    */
    $this->setOrder('entity_id');

    return parent::_renderFiltersBefore();
    return parent::_beforeLoad();
 }

/**
  • Please update full file – jafar pinjar Sep 10 '18 at 12:29
  • It doesn't work. Just tested on mag 2.2.3. Plz share your complete code. – ahsan Jan 1 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.