Magento _ Change default order by in category list

03 Aug Magento _ Change default order by in category list

Magento category listing is ordered by ascending by default installation. Under the attribute settings, we can easily manage the element listing used to sort the products, but changing the sort order to descending requires to add a line of code in layout.xml file.

Category page layout is handled by the catalog.xml file. Just an action tag can change the game.

Start Editing

Follow these steps. Open the file app/design/frontend/package_name/theme_name/layout/catalog.xml.

Find the block code <block type=”catalog/product_list_toolbar” name=”product_list_toolbar”template=”catalog/product/list/toolbar.phtml”> somewhere near line 122. Its under the <catalog_category_layered translate=”label”> handle.

Now add this line <action method=”setDefaultDirection”><direction>desc</direction></action> beneath the above searched block and its done.

We have added an action tag inside the block.

This is for the category type anchor. We need to repeat the task for the default category layout as well.

Whats next.

Clear you cache and re-index data. The products will be ordered descending in catalog.

How it works

Catalog listing toolbar template is handled by the block app/code/core/Mage/Catalog/Product/Block/Product/List/Toolbar.php. This class has a variable protected $_direction = ‘asc';. A method setDefaultDirection() is used to set the default direction. Using the above given code, we are calling the same method and assigning the desc to $_direction variable.