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

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>
Posted on July 5, 2010

About the author

Author Jisse Reitsma

Jisse Reitsma is the founder of Yireo, extension developer, developer trainer and 3x Magento Master. His passion is for technology and open source. And he loves talking as well.

Sponsor Yireo

Looking for a training in-house?

Let's get to it!

We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.

Do not miss out on what we say

This will be the most interesting spam you have ever read

We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.