one magento developer

my experiences about magento
sorting-by-rating

Magento 2 listing products by rating

Sometimes we need to sort the product by price or position or some attribute. I did all of them before but today my customer asked me for a new feature. sort the products by rating. I want to show it to you at Magento 2 listing products by rating post.

Magento 2 listing products by rating

After many searches, I could not find any information or sample code because of that I start my exploration on the Magento core. As you know the Magento core is the best practice for learning. After several hours I found it. It was too easy and now I want to share it with you.

1- create di.xml and define the plugin

At the first, we need to change the default Magento sort by creating a plugin

Create di.xml file at the ‘etc’ directory or if it exists just add below code into it

 <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="chalack_Custom_implementCustomSortOptions" type="ChalakSoft\Theme\Plugin\Toolbar" />
    </type>

With above code we create a plugin for default Magento sorting.

2- create plugin’s class

After that, we should add our plugin’s class. As you know the plugin class defended by ‘type’ at the above code and I put it in ‘ChalakSoft\Theme\Plugin\Toolbar’ directory. So, create a toolbar class with the below code.

<?php

namespace ChalakSoft\Theme\Plugin;

use Magento\Framework\Registry;
use Magento\Catalog\Block\Product\ProductList\Toolbar as ProductListToolbar;
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection;

class Toolbar
{
    /**
     * @var Registry
     */
    private $registry;

    public function __construct(Registry $registry)
    {
        $this->registry = $registry;
    }

    public function beforeSetCollection()
    {
        if (func_num_args() != 2) {
            return;
        }

        /** @var ProductListToolbar $toolbar */
        $toolbar = func_get_arg(0);

        /** @var Collection $collection */
        $collection = func_get_arg(1);

        if (!($toolbar instanceof ProductListToolbar && $collection instanceof Collection)) {
            return;
        }

        /** @var  \Magento\Catalog\Model\Category $category */
        $category = $this->registry->registry('current_category');

        if (!$category->hasData('default_sort_by')) {
            $toolbar->setDefaultOrder('price');
            $toolbar->setDefaultDirection('asc');
        }

        $currentOrder = $toolbar->getCurrentOrder();

        $currentDirection = $toolbar->getCurrentDirection();

        if ($currentOrder) {
            switch ($currentOrder) {

                case 'price_high_to_low':
                    $collection->getSelect()->order('price_index.min_price DESC');
                    break;

                case 'price_low_to_high':
                    $collection->getSelect()->order('price_index.min_price ASC');
                    break;

                case 'top_rating':
                    $collection->getSelect()->order('review_summary.reviews_count desc');
                    break;

                default:
                    $collection->setOrder($currentOrder, $currentDirection);
                    break;
            }
        }
    }
}

3- clear cache and compile codes

Now you need to compile the code and clean the cache.

If you have any questions about Magento 2 listing products by rating feel free to write a comment, also I know the article is too short but I assume that you are at last a middle Magento developer or have some information about sorting and Magento and plugin.

Result should be something like below image:

magento 2 listing products by rating

2 thoughts on “Magento 2 listing products by rating

  • joie
    December 17, 2020 at 1:52 pm

    very nice
    it’s worked …thanks

  • manadna
    February 22, 2021 at 9:13 am

    can you create a module for it?
    if you can , i will pay for it
    send me an email

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>