How to change Magento Enterprise "sort by price" to descending?

How to change Magento Enterprise "sort by price" to descending?

August 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to change Magento Enterprise "sort by price" to descending?

Resolving the “Sort By Price” Issue in Magento Enterprise

As a seasoned e-commerce developer, I often encounter situations where the default sorting options in Magento don’t quite meet our clients’ needs. One such issue involves changing the ‘sort by price’ option to display products in descending order. The provided code snippet is a good starting point, but it seems there’s a subtle error that prevents the expected behavior.

Let me walk you through how to resolve this issue effectively and efficiently.

Understanding Magento’s Sorting Mechanisms

Before diving into the solution, it’s crucial to understand how Magento handles product sorting. By default, Magento provides several built-in sorting options, including ‘sort by price.’ This option is controlled by the Mage_Catalog_Block_Product_List_Toolbar block and its associated templates.

The provided code is a portion of the toolbar.phtml template located in your Magento theme directory (usually app/design/frontend/base/default/template/catalog/toolbar.phtml). This template is responsible for rendering the sorting dropdown menu that appears on category pages.

Identifying the Problem

Upon examining the given code, I noticed two key areas of interest:

  1. The foreach loop generates options for each available order (sorting rule) defined in the Magento configuration.
  2. The conditional statement checks if the current sorting direction is ‘desc’ or ‘asc.’ If it’s ‘desc,’ it displays a link to change the direction to ascending; otherwise, it shows a link to switch to descending.

The problem arises from the fact that the getOrderUrl method returns the URL for the specified order and direction. In your code, you’re setting the value of the option to be the URL with ‘desc’ as the direction, which is correct for generating the sorting dropdown options.

However, when you change asc to desc in the link href attribute (<a class="category-asc v-middle" href="&lt;?php echo $this->getOrderUrl(null, 'desc') ?&gt;" title="&lt;?php echo $this->__('Set Descending Direction') ?&gt;">), you’re essentially creating a link that will sort the products in descending order again. This is why changing asc to desc doesn’t seem to have any effect.

The Solution: Overriding the getOrderUrl Method

To resolve this issue, we need to override the getOrderUrl method and modify its behavior to generate the correct URL for switching between ascending and descending directions. In order to do this, you’ll need to create a new module.

Create a new folder in your app/code/local directory with the following structure:

app/code/local/LaravelCompany/CustomToolbar/

Inside the CustomToolbar folder, create the following files and directories:

/CustomToolbar
    /etc
        config.xml
    /Block
        Product_List_Toolbar.php

Step 1: Create the Configuration File (config.xml)

Open the config.xml file located in the etc directory of your new module, and add the following content:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <LaravelCompany_CustomToolbar>
            <version>1.0.0</version>
        </LaravelCompany_CustomToolbar>
    </modules>

    <global>
        <blocks>
            <catalog>
                <rewrite>
                    <product_list_toolbar>
                        LaravelCompany_CustomToolbar_Block_Product_List_Toolbar
                    </product_list_toolbar>
                </rewrite>
            </catalog>
        </blocks>
    </global>
</config>

This configuration file tells Magento to rewrite the product_list_toolbar block with our custom implementation.

Step 2: Create the Custom Block (Product_List_Toolbar.php)

Open the Product_List_Toolbar.php file located in the Block directory of your new module, and add the following content:

<?php

class LaravelCompany_CustomToolbar_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
    public function getOrderUrl($orderField = null, $direction = null)
    {
        if ($direction === 'asc' || $direction === 'desc') {
            return Mage::helper('catalogurl')->getOrderUrl($this->getRequest()->getParam('dir'), $this->getRequest()->getParam('order'));
        }

        return parent::getOrderUrl($orderField, $direction);
    }
}

In this custom block class, we override the getOrderUrl method to check if a direction is specified. If it is (‘asc’ or ‘desc’), we use Magento’s built-in helper function Mage_CatalogUrl_Helper_Data::getOrderUrl() to generate the correct URL for switching between directions.

Step 3: Compile and Clean Magento

After creating the module files, you need to compile your Magento installation to apply the changes. Navigate to System > Tools > Compilation in your Magento backend. Click on the “Clear Cache” button followed by the “Compile All Files” button. This process may take some time depending on the size of your catalog.

Testing the Solution

After completing these steps, visit a category page in your Magento store that uses the default template. You should now see that the ‘sort by price’ option displays products in descending order by default. When you click the link to switch directions, it will correctly change the sorting direction between ascending and descending.

Conclusion

In this blog post, we’ve explored how to resolve a common issue encountered when working with Magento’s built-in product sorting functionality - changing the ‘sort by price’ option to display products in descending order. By understanding the underlying mechanisms of Magento’s sorting system and leveraging the power of block overrides, we were able to create a clean and effective solution.

The key takeaway is that when dealing with Magento’s core blocks, it’s essential to use the correct approach for overriding their behavior. In this case, creating a custom module and rewriting the product_list_toolbar block allowed us to modify the desired functionality without modifying core files directly.

Remember, when working with Magento, it’s always recommended to follow best practices such as using proper configuration files, keeping your code organized, and ensuring compatibility with future Magento updates. By adhering to these principles, you can create robust and maintainable solutions for your e-commerce projects.

For more insights into Magento development and customization, make sure to check out our blog at Laravel Company. We regularly publish articles on various topics related to e-commerce development, including Magento customizations, performance optimization, and best practices.

Catalog API Themes Configuration

Our network