Magento Category Product List Sort By Bestseller Options

Magento Category Product List Sort By Bestseller Options

August 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Category Product List Sort By Bestseller Options

Enhancing Category Product Listing: Implementing Best Seller Sort Option in Magento

As an experienced developer, I understand your challenge in integrating the “Best Seller” sort option into the category product listing in Magento. Let’s dive into the solution step by step.

Setting Up the Foundation: Extending the Core Block

You’ve already done a great job by extending the Mage_Catalog_Block_Product_List_Toolbar class to create your custom block Mymodule_Catalog_Block_Product_List_Toolbar. In your _construct() method, you’re correctly calling the parent constructor.

class Mymodule_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{

    protected function _construct()
    {
        parent::_construct();
    }

    // ...
}

Adding the Best Seller Option

Your next step is to add the “Best Seller” option to the available orders in the getAvailableOrders() method. You’ve done this by adding a new key-value pair to the $_availableOrder array.

public function getAvailableOrders()
{
    $this->$_availableOrder['bestseller'] = $this->__('Best Seller');
    return $this->$_availableOrder;
}

Implementing the Sort Logic

Now, let’s implement the sort logic based on the “Best Seller” option. We’ll need to override the applySort() method in your custom block.

protected function _construct()
{
    parent::_construct();
}

public function applySort()
{
    if ($this->getCurrentOrder() == 'bestseller') {
        $collection = $this->getProductCollection();
        $collection->getSelect()->columns(array('total_sale' => new Zend_Db_Expr("SUM(sales_flat_order_item.qty_ordered)")));
        $collection->getSelect()->joinLeft(
            array('sales_flat_order_item' => 'sales_flat_order_item'),
            "main_table.entity_id = sales_flat_order_item.product_id AND sales_flat_order_item.order_id IN (SELECT entity_id FROM sales_flat_order WHERE state != '" . Mage_Sales_Model_Order::STATE_CANCELED . "')",
            array('total_sale' => new Zend_Db_Expr("SUM(sales_flat_order_item.qty_ordered)"))
        );
        $collection->getSelect()->group('main_table.entity_id');
        $this->setProductCollection($collection);
    } else {
        parent::applySort();
    }
}

In the applySort() method, we first check if the current order is set to “bestseller”. If it is, we retrieve the product collection and join it with the sales flat order item table. We then calculate the total quantity sold for each product using a subquery. The result is a sorted collection based on the best seller count.

Conclusion: Best Seller Sorting Made Easy

By following these steps, you’ve successfully added and implemented the “Best Seller” sort option in your category product listing. This enhancement provides a more intuitive user experience for your customers by allowing them to filter products based on popularity.

Remember that this is just one example of how you can extend Magento’s core functionality to meet specific business requirements. Always strive to maintain a clean and organized codebase, and follow best practices for optimization and performance.

For further guidance on Magento development and customization, I recommend checking out our Magento Development Guide at LaravelCompany. It provides comprehensive insights into Magento architecture, configuration, and advanced customizations to help you build powerful e-commerce platforms.

Happy coding!

Catalog Extensions Configuration Orders

Our network