[Solved] Custom Sort options for Catalog Dropdown

Lerry Asks: Custom Sort options for Catalog Dropdown
I created: di.xml on app/code/Filter/Sorting/etc

Code:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">    
    <type name="MagentoCatalogModelConfig">
        <plugin name="Filter_Sorting::addCustomSortOptions" type="FilterSortingPluginModelConfig" />
    </type>
    <type name="MagentoCatalogBlockProductProductListToolbar">
        <plugin name="Filter_Sorting::implementCustomSortOptions" type="FilterSortingPluginProductProductListToolbar" />
    </type>
</config>

for my Config.php on app/code/Filter/Sorting/Plugin:

Code:
<?php
namespace FilterSortingPluginModel;
 
class Config
{    
    public function afterGetAttributeUsedForSortByArray(MagentoCatalogModelConfig $catalogConfig, $options)
    {
        //Remove default sorting options
        unset($options['position']);
        unset($options['name']);
        unset($options['price']);
 
        //New sorting options
        $options['newest'] = __('New product');        
        $options['name'] = __('Name');        
        $options['price_desc'] = __('Price High to Low');
        $options['price_asc'] = __('Price Low to High');
        $options['name_asc'] = __('Title: A-Z');
        $options['name_desc'] = __('Title: Z-Z');
        $options['release_date'] = __('Release Date');
        $options['sales_popularity'] = __('Bestselling');
        $options['rating_summary'] = __('Top Rated');
        $options['reviews_count'] = __('Most Reviewed');
 
        return $options;
    }
}

and file Toolbar.php on app/code/Filter/Sorting/Product/ProductList:

Code:
<?php
namespace FilterSortingPluginProductProductList;
 
class Toolbar
{    
    public function aroundSetCollection(
        MagentoCatalogBlockProductProductListToolbar $toolbar,
        Closure $proceed,
        $collection
    ) {
        $this->_collection = $collection;
        $currentOrder = $toolbar->getCurrentOrder();
        $currentDirection = $toolbar->getCurrentDirection();
        $result = $proceed($collection);
 
        if ($currentOrder) {
            switch ($currentOrder) {
 
            case 'newest':
                if ($currentDirection == 'desc') {
                    $this->_collection->getSelect()->order('e.created_at ASC');
                } elseif ($currentDirection == 'asc') {
                    $this->_collection->getSelect()->order('e.created_at DESC');
                }
            break;
 
                      
 
            case 'name':
                if ($currentDirection == 'desc') {
                    $this->_collection->getSelect()->order('name ASC');
                } elseif ($currentDirection == 'asc') {
                    $this->_collection->getSelect()->order('name DESC');
                }
            break;
 
            case 'price_desc':            
                if ($currentDirection == 'desc') {
                    $this->_collection->getSelect()->order('price ASC');
                } elseif ($currentDirection == 'asc') {
                    $this->_collection->getSelect()->order('price DESC');
                }
            break;
 
            case 'price_asc':
                if ($currentDirection == 'desc') {
                    $this->_collection->getSelect()->order('price DESC');
                } elseif ($currentDirection == 'asc') {
                    $this->_collection->getSelect()->order('price ASC');
                }
            break;   

            case 'name_desc':
                if ($currentDirection == 'desc') {
                    $this->_collection->getSelect()->order('name DESC');
                } elseif ($currentDirection == 'asc') {
                    $this->_collection->getSelect()->order('name ASC');
                }
            break;  

            case 'name_asc':
                if ($currentDirection == 'desc') {
                    $this->_collection->getSelect()->order('name ASC');
                } elseif ($currentDirection == 'asc') {
                    $this->_collection->getSelect()->order('name DESC');
                }
            break; 

            case 'sales_popularity':
                $this->_collection->getSelect()->order('sales_popularity', 'desc');
                break;

            case 'rating_summary':
                $storeId = Mage::app()->getStore()->getId();
                $this->_collection->getSelect()->join('review_entity_summary',
                    'review_entity_summary.entity_pk_value=e.entity_id and review_entity_summary.store_id=' . $storeId,
                    ['rating' => 'ceiling(rating_summary/10)', 'reviews_count'])->order(['rating desc', 'reviews_count desc']);
                $this->_collection->setOrder('rating_summary', 'desc');
                break;

            case 'reviews_count':
                $storeId = Mage::app()->getStore()->getId();
                $this->_collection->getSelect()->join('review_entity_summary',
                    'review_entity_summary.entity_pk_value=e.entity_id and review_entity_summary.store_id=' . $storeId,
                    ['rating_summary', 'reviews_count'])->order(['reviews_count desc']);
                $this->_collection->setOrder('reviews_count', 'desc');
                break;


 
            default:        
                $this->_collection
                    ->setOrder($currentOrder, $currentDirection);
            break;
 
            }
        }        
        //var_dump($currentDirection);
        return $result;
    }
}

But latest switch case: rating_summary and reviews_count – not working, where i wrong? Thanks.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.