Set the default direction or ordering for Magento category-pages

On Magento category-pages you can select how to view the available products: By price, name or relevance, and ascending or descending. Within the Magento backend, the default ordering (price, name or relevance) can be configured but strangely enough the default direction (ascending or descending) not. Here is a XML layout update that allows you to do this anyway.

What is a XML layout update?

XML-layouts are part of the Magento theming system. If you are not familiar with this, check the Magento Designers Guide for more information. The XML-layout defined in various XML-files is processed by Magento but it can be altered through so-called XML-updates.

XML-updates can be applied in the GUI (for products and categories) but also through your own XML-files within the Magento theming folders. In our tutorial Build your own Magento override-module you can read how to access your own custom XML-layout file.

The toolbar and its methods

product_list_sortorder

The selection-box which allows you to choose the direction and sorting order, is part of the toolbar (with its template-file toolbar.phtml). While the existence of this toolbar is defined in the XML-file catalog.xml, it's logic is defined in a Block-class and that PHP class is containing two methods of our interest:

  • setDefaultDirection
  • setDefaultOrder

By applying our own XML-update we can call these methods and there for modify the default behaviour of XML-pages.

Definitions for category pages

The following XML-code is modifying the default direction to asc (ascending) while the default ordering is set to relevance:

<layout>
    <catalog_category_default>
        <reference name="product_list_toolbar">
            <action method="setDefaultDirection"><string>asc</string></action>
            <action method="setDefaultOrder"><string>relevance</string></action>
        </reference>
    </catalog_category_default>
    <catalog_category_layered>
        <reference name="product_list_toolbar">
            <action method="setDefaultDirection"><string>asc</string></action>
            <action method="setDefaultOrder"><string>relevance</string></action>
        </reference>
    </catalog_category_layered>
</layout>

Definitions for search pages

On search-pages (the regular search and the advanced search) the results also can be modified using the same selection-box as on category-pages. However, the default values are defined through a different Block-class (search_result_list instead of product_list_toolbar). There for the XML-code is slightly different:

<layout>
    <catalogsearch_result_index>
        <reference name="search_result_list">
            <action method="setDefaultDirection"><string>asc</string></action>
            <action method="setDefaultOrder"><string>relevance</string></action>
        </reference>
    </catalogsearch_result_index>
    <catalogsearch_advanced_result>
        <reference name="search_result_list">
            <action method="setDefaultDirection"><string>asc</string></action>
            <action method="setDefaultOrder"><string>relevance</string></action>
        </reference>
    </catalogsearch_advanced_result>
</layout>