I want to custom sort by option filter attribute add in tool bar section. Currently Magento 2 provide Position , Product Name , Price And Sorting option to low to high. But i need to change all sorting option Like Price High to Low , Price Low to High.
Magento – How to add custom sort by option filter in tool bar in magento2
attributescustom-optionsmagento2sortingtoolbar
Related Solutions
Step 1: Create plugins in
app/code/Vendor/Module/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\Block\Product\ProductList\Toolbar">
<plugin name="custom_custom_block_toolbar" type="Vendor\Module\Plugin\Catalog\Block\Toolbar" />
</type>
<type name="Magento\Catalog\Model\Config">
<plugin name="custom_catalog_model_config" type="Vendor\Module\Plugin\Catalog\Model\Config" />
</type>
</config>
Step 2: Create Config.php in
app/code/Vendor/Module/Plugin/Catalog/Model/Config.php
<?php
namespace Vendor\Module\Plugin\Catalog\Model;
class Config
{
public function afterGetAttributeUsedForSortByArray(
\Magento\Catalog\Model\Config $catalogConfig,
$options
) {
$options['low_to_high'] = __('Price - Low To High');
$options['high_to_low'] = __('Price - High To Low');
return $options;
}
}
Step 3: Create Toolbar.php in
app/code/Vendor/Module/Plugin/Catalog/Block/Toolbar.php
<?php
namespace Vendor\Module\Plugin\Catalog\Block;
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();
$result = $proceed($collection);
if ($currentOrder) {
if ($currentOrder == 'high_to_low') {
$subject->getCollection()->setOrder('price', 'desc');
} elseif ($currentOrder == 'low_to_high') {
$subject->getCollection()->setOrder('price', 'asc');
}
}
return $result;
}
}
Yes , Native Three Option position, name, price comes and by default Sort by position is selected
Added Size 4th criteria to sort
Suppose you want to make Short by Size by default
Go to below path and uncheck the USE SYSTEM VALUE then select your value
Admin > Stores > Configuration > Catalog > Catalog > Store Front > Product Listing Sort by > size
If you added custom value that should be here in drop down list and then you can select that for default shorting via admin
I hope this will help you out, Tested on default LUMA Theme
Thanks
Best Answer
I created these custom sort by options:
by creating a plugin to bypass the default functionality of the product sorter which can be found here:
and
You start by creating your own
module
and registering it.Then create:
Create
Create