List Products In Magento By Price But Have Zero Prices At The End

List Products In Magento By Price But Have Zero Prices At The End

September 22, 2026 · Illustrated author avatarBy Stefan Bogdanescu
List Products In Magento By Price But Have Zero Prices At The End

Sorting Products by Price in Magento: Display Zero Prices at the End

As a Magento developer, you might find yourself wanting to display your products sorted by price, but with an important twist—you want all zero-price items listed last. This isn’t directly supported out-of-the-box, but with some creative code modifications, it’s definitely achievable.

The key lies in manipulating the product collection returned from getProductCollection(). Let me guide you through the process step-by-step, using a practical example that you can easily adapt for your own store configuration.

Sorting Products by Price

First things first—we need to understand how Magento normally sorts products. By default, it uses the price attribute for sorting when the ‘price’ orderby option is selected in the frontend. The direction (ascending or descending) is determined by the ‘dir’ parameter.

To modify this behavior, we’ll start by editing the relevant file:

Step 1: Edit your local copy of /app/code/core/Mage/Catalog/Block/Product/List.php

Note that I recommend working on a local copy to avoid directly modifying core files, which can lead to version control conflicts and make upgrades more challenging.

Step 2: Locate the _getProductCollection() method

This is where Magento retrieves the product collection based on the current filters and sorting options. We’ll add our custom logic inside this function to achieve the desired order of zero-price items at the end.

Step 3: Add the Custom Logic for Zero-Price Products

We can use PHP arrays to manage the sorting process more effectively. Here’s a simple example:

$orderFilterType = $this->getRequest()->getParam('order');
$dirFilterType = $this->getRequest()->getParam('dir');

if (isset($orderFilterType) && $orderFilterType == 'price' && isset($dirFilterType) && $dirFilterType == 'asc') {
    // Get the initial product collection
    $productCollection = $layer->getProductCollection();

    // Filter out zero-price products and store them separately
    $zeroPriceProducts = $productCollection->addAttributeToFilter('price', 0)->toArray();
    $nonZeroPriceProducts = $productCollection->addAttributeToFilter('price', ['neq' => 0])->toArray();

    // Sort the non-zero-price products by price (descending)
    usort($nonZeroPriceProducts, function ($a, $b) {
        return $b['price'] - $a['price'];
    });

    // Merge both arrays and reset the indices for proper sorting
    $sortedProducts = array_merge($nonZeroPriceProducts, $zeroPriceProducts);
    $this->_productCollection = new Varien_Data_Collection();
    foreach ($sortedProducts as $product) {
        $this->_productCollection->addItem(new Mage_Catalog_Model_Product()->setData($product));
    }
} else {
    // Fallback to the default sorting
    $this->_productCollection = $layer->getProductCollection();
}

Step 4: Verify and Test

Save your changes, clear Magento’s cache, and visit your storefront. When you select ‘Price’ as the sort option with ascending order, you should now see all non-zero-price products sorted by price in descending order, followed by any zero-price items at the end of the list.

Conclusion

With this approach, you’ve successfully modified Magento’s core behavior to display your products sorted by price while keeping all zero-price items at the end for clarity and customer convenience.

Remember, always work on local copies of files to avoid conflicts during upgrades. Additionally, consider using a version control system like Git if you haven’t already—it helps track changes and simplifies collaborative development.

For more advanced customizations or if you’re working with complex product configurations, I recommend exploring Magento’s event observer system or the plugin architecture introduced in later versions (Magento 2). These provide more flexible ways to modify functionality without directly editing core files.

If you found this post helpful, feel free to explore our Laravel Company blog for more practical tips and tutorials on Magento development. We cover a wide range of topics, from performance optimizations to custom module creation, all designed to help you get the most out of your e-commerce platform.

Happy coding!

Catalog Configuration Deployment Orders

Our network