After upgrading magento 2.3 and enabling magento new feature elastic search 6, My custom module for sort by is not working. It is perfectly fine working with MySql option but not working with elastic search.
Below is my custom module code:
CS/SortBy/Plugin/Product/ProductList/Toolbar.php
<?php
namespace CS\SortBy\Plugin\Product\ProductList;
class Toolbar
{
/**
* Plugin
*
* @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject
* @param \Closure $proceed
* @param \Magento\Framework\Data\Collection $collection
* @return \Magento\Catalog\Block\Product\ProductList\Toolbar
*/
public function aroundSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $toolbar,
\Closure $proceed,
$collection
) {
$this->_collection = $collection;
$currentOrder = $toolbar->getCurrentOrder();
$currentDirection = $toolbar->getCurrentDirection();
$result = $proceed($collection);
if ($currentOrder) {
switch ($currentOrder) {
case 'newest':
$this->_collection
->getSelect()
->order('e.created_at DESC');
break;
case 'price_desc':
$this->_collection
->getSelect()
->order('price_index.min_price DESC');
break;
case 'price_asc':
$this->_collection
->getSelect()
->order('price_index.min_price ASC');
break;
default:
$this->_collection
->setOrder($currentOrder, $currentDirection);
break;
}
}
return $result;
}
}
CS/SortBy/Plugin/Model/Config.php
<?php
namespace CS\SortBy\Plugin\Model;
class Config
{
/**
* Adding custom options and changing labels
*
* @param \Magento\Catalog\Model\Config $catalogConfig
* @param [] $options
* @return []
*/
public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
{
//Remove default sorting options
unset($options['name']);
unset($options['price']);
//New sorting options
$options['position'] = __('Recommended');
$options['price_desc'] = __('Price High');
$options['price_asc'] = __('Price Low');
$options['newest'] = __('Newest First');
return $options;
}
}