Step 1: First you should create registration.php
Vendor name: Arun
Module name: NewSorting
Vendor/Modulename/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Arun_NewSorting',
__DIR__
);?>
Step 2: You create module.xml
Vendor/Modulename/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Arun_NewSorting" setup_version="0.0.1">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
Step 3: You create plugin
Vendor/Modulename/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::addCustomOptions" type="Arun\NewSorting\Plugin\Model\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="Arun_NewSorting::addPriceDecendingFilterInToolbar" type="Arun\NewSorting\Plugin\Product\ProductList\Toolbar" />
</type>
</config>
Step 4: then create config.php
Vendor/Modulename/Plugin/Model/Config.php
<?php
namespace Arun\NewSorting\Plugin\Model;
use Magento\Store\Model\StoreManagerInterface;
class Config {
protected $_storeManager;
public function __construct(
StoreManagerInterface $storeManager
) {
$this->_storeManager = $storeManager;
}
public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
{
$store = $this->_storeManager->getStore();
$currencySymbol = $store->getCurrentCurrency()->getCurrencySymbol();
// Remove specific default sorting options
$default_options = [];
$default_options['name'] = $options['name'];
unset($options['position']);
unset($options['name']);
unset($options['price']);
//Changing label
$customOption['position'] = __( 'Relevance' );
//New sorting options
$customOption['created_at'] = __( ' New' );
$customOption['name'] = $default_options['name'];
//Merge default sorting options with custom options
$options = array_merge($customOption, $options);
return $options;
}
}
Step 5: Override the Toolbar.php***
Vendor/Modulename/Plugin/Product/ProductList/Toolbar.php
<?php
namespace Arun\NewSorting\Plugin\Product\ProductList;
class Toolbar
{
public function aroundSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
\Closure $proceed,
$collection
) {
$currentOrder = $subject->getCurrentOrder();
$result = $proceed($collection);
if ($currentOrder) {
if ($currentOrder == 'created_at') {
$subject->getCollection()->setOrder('created_at', 'desc');
}
}
return $result;
}
}
it's work perfectly