How to add sort by "Best Seller" in tool bar in product listing page?

How to add sort by "Best Seller" in tool bar in product listing page?

July 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add sort by "Best Seller" in tool bar in product listing page?

Adding “Best Seller” Sorting Option to Magento Product Listing Toolbar

As an e-commerce developer, I often encounter requests to enhance the user experience by adding sorting options based on product popularity or sales performance. In this post, we’ll guide you through implementing a “Best Seller” sort option in the toolbar of your Magento store’s product listing page.

Understanding the Requirement

The client wants to display products that have been sold most frequently within their category. However, they haven’t set any specific attributes for “best seller” status. We need to calculate this based on order history.

The Current Challenge

The developer attempted to follow a tutorial found here. However, they encountered an error when trying to sort by the Best Seller option.

Our Approach

To achieve this, we’ll need to create a custom module that captures relevant data and extends the core Magento functionality. This involves creating an observer for the sales_order_save_after event and storing necessary information in an additional table.

Let’s dive into the code implementation step by step.

Step 1: Create the Database Table

First, we need to create a new database table to store the product sales count. You can use the following SQL query:

CREATE TABLE IF NOT EXISTS `sales_bestsellers` (
  `entity_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `product_id` INT(10) UNSIGNED NOT NULL DEFAULT '0',
  `count` INT(10) UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY (`entity_id`)
);

Step 2: Create the Observer

Next, we create an observer for the sales_order_save_after event. In this observer, we’ll increment the sale count for each product in the order.

class SalesOrderSaveAfterObserver {
    protected $_resource;

    public function __construct(Varien_Event_Observer $observer) {
        // Initialize the resource model
        $this->_resource = Mage::getSingleton('core/resource');
    }

    public function execute(Varien_Event_Observer $observer) {
        $orderId = $observer->getOrder()->getId();
        $orderItems = $observer->getOrder()->getAllVisibleItems();

        foreach ($orderItems as $item) {
            // Insert or update the sales count
            $this->_resource->getConnection('core_read')->query(
                "INSERT INTO `sales_bestsellers` (`product_id`, `count`)
                 VALUES (?, ?)
                 ON DUPLICATE KEY UPDATE `count` = `count` + 1",
                array((int)$item->getProductId(), 1)
            );
        }
    }
}

Step 3: Extend the Product Collection

Finally, we need to extend the product collection to fetch and sort by the sales count. Create a new model file:

class Company_Bestseller_Model_Mysql4_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract {
    public function _construct() {
        $this->_init('company_bestseller/bestseller');
    }

    protected function _beforeLoad() {
        parent::_beforeLoad();
        $this->getSelect()->joinLeft(
            array('count' => 'sales_bestsellers'),
            'main_table.entity_id = count.product_id',
            array('bestseller_count' => 'IFNULL(count.count, 0)')
        );
    }
}

Step 4: Create the Block for Sorting

Create a new block that will be added to the toolbar:

class Company_Bestseller_Block_Product_List extends Mage_Catalog_Block_Product_List {
    protected function _getProductCollection() {
        $collection = parent::_getProductCollection();

        if ($this->_isSortByBestSeller()) {
            $collection->getSelect()->order('main_table.entity_id DESC');
        }

        return $collection;
    }
}

Conclusion

Implementing a “Best Seller” sorting option in Magento involves extending the core functionality and creating custom logic to handle sales tracking. By following this guide, you can now display your most popular products based on actual sales data.

Don’t forget to check out our Magento e-commerce developer openings if you’re looking for professional opportunities in the field!

This solution provides a comprehensive approach while keeping the code clean and maintainable. If you encounter any issues or need further guidance, feel free to reach out in the comments below.

Catalog Database Performance Extensions

Our network