How to sort by most viewed in Magento?

How to sort by most viewed in Magento?

October 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to sort by most viewed in Magento?

Sorting Products by Most Viewed in Magento

As a senior developer specializing in e-commerce platforms like Magento, I often receive queries about customizing product sorting functionality. In this post, we’ll address how to sort products based on the number of views—in this case, for the Bindaaslo store.

Understanding Magento’s Viewing Tracking

Magento 1.x tracks product views through a table called catalog_product_viewed. Each time a customer visits a product page (even if they don’t add it to the cart), Magento logs this activity. By leveraging this data, we can create a sort order that prioritizes products based on their popularity.

Customizing the Product List Toolbar

To achieve our goal, we’ll extend the Mage_Catalog_Block_Product_List_Toolbar class and override its default behavior.

  1. Create an extension module in app/code/local/Sugarcode/Catalog/etc/config.xml:
<config>
    <modules>
        <Sugarcode_Catalog>
            <version>0.1.0</version>
        </Sugarcode_Catalog>
    </modules>
    <global>
        <!-- Override the toolbar block -->
        <blocks>
            <catalog>
                <rewrite>
                    <product_list_toolbar>
                        Sugarcode_Catalog_Block_Product_List_Toolbar
                    </product_list_toolbar>
                </rewrite>
            </catalog>
        </blocks>
    </global>
</config>
  1. Create the extended toolbar block in app/code/local/Sugarcode/Catalog/block/Product/List/Toolbar.php:
class Sugarcode_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
    protected function _getProductCollection()
    {
        $collection = parent::_getProductCollection();

        // Sort products by view count in descending order
        $collection->getSelect()->order('(SELECT COUNT(*) FROM `catalog_product_viewed` WHERE `viewed_id` = `e`.entity_id) DESC');

        return $collection;
    }
}

Understanding the Code

In our custom block, we override the _getProductCollection() method to add an ORDER BY clause to the product collection’s select statement. This clause sorts products based on the count of views stored in the catalog_product_viewed table.

The subquery (SELECT COUNT(*) FROM catalog_product_viewed WHERE viewed_id = e.entity_id) counts the number of views for each product (e), and the ORDER BY clause sorts this count in descending order (from highest to lowest).

Conclusion

By extending Magento’s core toolbar block, we’ve successfully implemented a custom sorting mechanism that prioritizes products based on their view counts. Remember, it’s crucial to follow best practices like creating custom extensions instead of modifying core files.

This approach ensures your changes survive Magento updates and keeps your codebase clean and maintainable. As an experienced developer, I recommend using this method for any customizations you need in your Magento store.

For more advanced e-commerce development techniques and expert insights, be sure to check out the articles on Laravel Company’s blog.

Magento 1 Checkout Catalog Database

Our network