How to change the default sort direction in category pages?

The best way to achieve this is by adding the following lines to the category pages layout file catalog_category_view.xml. This file can be under the Magento_Catalog\layout folder inside your theme folder.

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
		<referenceBlock name="product_list_toolbar">
			<action method="setDefaultDirection">
				<argument name="dir" xsi:type="string">desc</argument>
			</action>
		</referenceBlock>
    </body>
</page>

These instructions call the setDefaultDirection method in class Toolbar, and pass the value desc or asc to the argument dir. This sets the default sort direction to descending or ascending respectively.

Don’t forget to clear your cache if you are in developer mode or deploy static content in production.

Leave a Reply

Your email address will not be published. Required fields are marked *