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
Now add this line
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.
Find the block code
Add the same code
Save the file.
Final output will look similar to this.
<catalog_category_default translate="label"> <label>Catalog Category (Non-Anchor)</label> <reference name="left"> <block type="catalog/navigation" name="catalog.leftnav" after="currency" template="catalog/navigation/left.phtml"/> </reference> <reference name="content"> <block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml"> <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml"> <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml"> <!--NEW ADDED LINE--> <action method="setDefaultDirection"><direction>desc</direction></action> <block type="page/html_pager" name="product_list_toolbar_pager"/> <!-- The following code shows how to set your own pager increments --> <!-- <action method="setDefaultListPerPage"><limit>4</limit></action> <action method="setDefaultGridPerPage"><limit>9</limit></action> <action method="addPagerLimit"><mode>list</mode><limit>2</limit></action> <action method="addPagerLimit"><mode>list</mode><limit>4</limit></action> <action method="addPagerLimit"><mode>list</mode><limit>6</limit></action> <action method="addPagerLimit"><mode>list</mode><limit>8</limit></action> <action method="addPagerLimit" translate="label"><mode>list</mode><limit>all</limit><label>All</label></action> --> </block>
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.