<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Planet Web Solutions</title>
    <description>The latest articles on DEV Community by Planet Web Solutions (@planetwebsol).</description>
    <link>https://dev.to/planetwebsol</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F176736%2F562cb0b9-9ffb-477e-a83a-f261ca0ecc1d.jpg</url>
      <title>DEV Community: Planet Web Solutions</title>
      <link>https://dev.to/planetwebsol</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/planetwebsol"/>
    <language>en</language>
    <item>
      <title>How to implement sort by "Best Seller" products in Magento 2?</title>
      <dc:creator>Planet Web Solutions</dc:creator>
      <pubDate>Fri, 07 Jun 2019 06:59:47 +0000</pubDate>
      <link>https://dev.to/planetwebsol/how-to-implement-sort-by-best-seller-products-in-magento-2-4cp8</link>
      <guid>https://dev.to/planetwebsol/how-to-implement-sort-by-best-seller-products-in-magento-2-4cp8</guid>
      <description>&lt;h2&gt;What are “Best Seller” Products?&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;Why there is a need to implement sort by “Best Seller” products in Magento 2?&lt;/h2&gt;

&lt;h3&gt;Encourage Sales and Conversions&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;Quick Buying Decision&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;Enhanced Promotion Campaigns&lt;/h3&gt;

&lt;p&gt;When the store has a list of bestseller products, it can be used at the time of promotion and running campaigns on social media&lt;/p&gt;

&lt;h2&gt;Guide to implement sort by "Best Seller" products in Magento 2?&lt;/h2&gt;

&lt;p&gt;The sort by bestseller block can be installed in the desired product page, following the below set of coding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create a di.xml file&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt; 
&amp;lt;preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Company\Module\Block\Product\ProductList\Toolbar"/&amp;gt;
&amp;lt;preference for="Magento\Catalog\Model\Config" type="Company\Module\Model\Config"/&amp;gt; &lt;/pre&gt;

&lt;strong&gt;Step 2:  Create a configuration model&lt;/strong&gt;
&lt;pre&gt;
class Config extends \Magento\Catalog\Model\Config
{
    public function getAttributeUsedForSortByArray()
    {
       $options = ['bestseller' =&amp;gt; __('Best seller')];
        foreach ($this-&amp;gt;getAttributesUsedForSortBy() as $attribute) {
            /* &lt;a class="comment-mentioned-user" href="https://dev.to/var"&gt;@var&lt;/a&gt;
 $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
            $options[$attribute-&amp;gt;getAttributeCode()] = $attribute-&amp;gt;getStoreLabel();
        }

       return $options;
    }
}
&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Create a Block.&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;
class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
{
    public function setCollection($collection)
    {
        if($this-&amp;gt;getCurrentOrder()=="bestseller")
        {
              $collection-&amp;gt;getSelect()-&amp;gt;joinLeft( 
                'sales_order_item', 
                'e.entity_id = sales_order_item.product_id', 
                array('qty_ordered'=&amp;gt;'SUM(sales_order_item.qty_ordered)')) 
                -&amp;gt;group('e.entity_id') 
                -&amp;gt;order('qty_ordered '.$this-&amp;gt;getCurrentDirectionReverse());
        }

        $this-&amp;gt;_collection = $collection;

        $this-&amp;gt;_collection-&amp;gt;setCurPage($this-&amp;gt;getCurrentPage());

        $limit = (int)$this-&amp;gt;getLimit();
        if ($limit) {
            $this-&amp;gt;_collection-&amp;gt;setPageSize($limit);
        }
        if ($this-&amp;gt;getCurrentOrder()) {
            $this-&amp;gt;_collection-&amp;gt;setOrder($this-&amp;gt;getCurrentOrder(), $this-&amp;gt;getCurrentDirection());
        }
        return $this;
    }

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

}

&lt;/pre&gt;

&lt;h2&gt;Want to have sort by “Best seller” feature in your store?&lt;/h2&gt;

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

</description>
      <category>php</category>
      <category>css</category>
      <category>html</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
