Custom Sorting option not working in Magetno 2.4 search
app/code/Arun/NewSorting/etc/di.xml
<?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="Magento\Catalog\Model\Config">
<plugin name="Arun_NewSorting::addCustomSortOptions" type="Arun\NewSorting\Plugin\Model\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="Arun_NewSorting::implementCustomSortOptions" type="Arun\NewSorting\Plugin\Product\ProductList\Toolbar" />
</type>
app/code/Arun/NewSorting/Plugin/Model/Config.php
<?php
namespace Arun\NewSorting\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)
{
//Change label of default sorting options if needed
//$options['position'] = __('Relevance');
$options['newest'] = __('Newest');
return $options;
}
}
app/code/Arun/NewSorting/Plugin/Product/ProductList/Toolbar.php
<?php
namespace Arun\NewSorting\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;
default:
$this->_collection
->setOrder($currentOrder, $currentDirection);
break;
}
}
//var_dump((string) $this->_collection->getSelect());
return $result;
}
}
When i select the Newest filter it shows the message No Result Found.
Any one tell me what is the issue or any other things which i have missed.
Any help is appriciated.