We can achieve it by using Plugins.
Please create following files in your module.
app/code/Package/CustomToolbar/etc/di.xml
<type name="Magento\Catalog\Model\Config">
<plugin name="Package_CustomToolbar::addCustomOptions" type="Package\CustomToolbar\Plugin\Model\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="Package_CustomToolbar::addPriceDecendingFilterInToolbar" type="Package\CustomToolbar\Plugin\Product\ProductList\Toolbar" />
</type>
app/code/Package/CustomToolbar/Plugin/Model/Config.php
namespace Jjcommerce\CustomToolbar\Plugin\Model;
use Magento\Store\Model\StoreManagerInterface;
class Config
{
protected $_storeManager;
public function __construct(
StoreManagerInterface $storeManager
) {
$this->_storeManager = $storeManager;
}
/**
* Adding custom options and changing labels
*
* @param \Magento\Catalog\Model\Config $catalogConfig
* @param [] $options
* @return []
*/
public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
{
$store = $this->_storeManager->getStore();
$currencySymbol = $store->getCurrentCurrency()->getCurrencySymbol();
//Remove specific default sorting options
unset($options['position']);
unset($options['name']);
unset($options['price']);
//Changing label
$customOption['position'] = __('Relevance');
//New sorting options
$customOption['price_desc'] = __($currencySymbol.' (High to Low)');
$customOption['price'] = __($currencySymbol.' (Low to High)');
//Merge default sorting options with custom options
$options = array_merge($customOption, $options);
return $options;
}
}
app/code/Package/CustomToolbar/Plugin/Product/ProductList/Toolbar.php
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 $subject,
\Closure $proceed,
$collection
) {
$currentOrder = $subject->getCurrentOrder();
if ($currentOrder && ($currentOrder == 'price_desc')) {
$result = $proceed($collection);
$subject->getCollection()->setOrder('price', 'desc');
return $result;
} else {
return $proceed($collection);
}
}
}
This is working fine for me without rewriting any Magento class.