I created:
di.xml on app/code/Filter/Sorting/etc
<?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:
<?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:
<?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.