DEV Community

Cover image for How to implement sort by "Best Seller" products in Magento 2?
Planet Web Solutions
Planet Web Solutions

Posted on

How to implement sort by "Best Seller" products in Magento 2?

What are “Best Seller” Products?

The products that have the top selling record are known as the ‘best seller’ products of the website. In other words, the available store’s products having the maximum quantity of sales are the ‘best seller’ products.

Why there is a need to implement sort by “Best Seller” products in Magento 2?

Encourage Sales and Conversions

It is always good to showcase your best seller products to the website’s visitors and customers. The more you will exhibit your popular products, higher would be their chance of getting acquired by other customers.

Quick Buying Decision

Modern shoppers have the habit of reading reviews before making purchase. Obviously, the best seller products of a store would have more no. of positive feedbacks and ratings by the customer. This will help the prospective buyers to make quick decision and cut down the shopping time.

Enhanced Promotion Campaigns

When the store has a list of bestseller products, it can be used at the time of promotion and running campaigns on social media

Guide to implement sort by "Best Seller" products in Magento 2?

The sort by bestseller block can be installed in the desired product page, following the below set of coding.

Step 1: Create a di.xml file

<preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Company\Module\Block\Product\ProductList\Toolbar"/>
<preference for="Magento\Catalog\Model\Config" type="Company\Module\Model\Config"/> 
Step 2: Create a configuration model
class Config extends \Magento\Catalog\Model\Config
    public function getAttributeUsedForSortByArray()
       $options = ['bestseller' => __('Best seller')];
        foreach ($this->getAttributesUsedForSortBy() as $attribute) {
            /* @var
 $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
            $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();

       return $options;

Step 3: Create a Block.

class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
    public function setCollection($collection)
                'e.entity_id = sales_order_item.product_id', 
                ->order('qty_ordered '.$this->getCurrentDirectionReverse());

        $this->_collection = $collection;


        $limit = (int)$this->getLimit();
        if ($limit) {
        if ($this->getCurrentOrder()) {
            $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        return $this;

    public function getCurrentDirectionReverse() {
            if ($this->getCurrentDirection() == 'asc') {
                return 'desc';
            } elseif ($this->getCurrentDirection() == 'desc') {
                return 'asc';
            } else {
                return $this->getCurrentDirection();


Want to have sort by “Best seller” feature in your store?

If your e-commerce store is deprived from sort by “Best seller” products or other features, talk to our expert team of Magento 2 Developers.

Discussion (0)