How to implement sort by "Best Seller" products in Magento 2?
Tags: php, css, html, javascript
Published: 2019-06-07 06:59:47


<h2>What are “Best Seller” Products?</h2>

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.

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

<h3>Encourage Sales and Conversions</h3>

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.

<h3>Quick Buying Decision</h3>

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.

<h3>Enhanced Promotion Campaigns</h3>

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

<h2>Guide to implement sort by "Best Seller" products in Magento 2?</h2>

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

<strong>Step 1: Create a di.xml file</strong>
<pre> 
&lt;preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Company\Module\Block\Product\ProductList\Toolbar"/&gt;
&lt;preference for="Magento\Catalog\Model\Config" type="Company\Module\Model\Config"/&gt; </pre>

<strong>Step 2:  Create a configuration model</strong>
<pre>
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;
    }
}
</pre>

<strong>Step 3: Create a Block.</strong>
<pre>
class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
{
    public function setCollection($collection)
    {
        if($this->getCurrentOrder()=="bestseller")
        {
              $collection->getSelect()->joinLeft( 
                'sales_order_item', 
                'e.entity_id = sales_order_item.product_id', 
                array('qty_ordered'=>'SUM(sales_order_item.qty_ordered)')) 
                ->group('e.entity_id') 
                ->order('qty_ordered '.$this->getCurrentDirectionReverse());
        }

        $this->_collection = $collection;

        $this->_collection->setCurPage($this->getCurrentPage());

        $limit = (int)$this->getLimit();
        if ($limit) {
            $this->_collection->setPageSize($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();
            }
        }

}

</pre>

<h2>Want to have sort by “Best seller” feature in your store?</h2>

If your e-commerce store is deprived from sort by “Best seller” products or other features, talk to our expert team of <b><a href="https://www.planetwebsolution.com/magento-development-services">Magento 2 Developers</a></b>.