I want to custom sort by option in category Filter For price like this Price: Low to High & Price: High to Low.
Please suggest me how to change sorting option in my site. i am using magento 2.1.2
- magento.stackexchange.com/questions/96095/…Jackson– Jackson2016-12-16 06:58:53 +00:00Commented Dec 16, 2016 at 6:58
- Use my module github.com/springimport/magento2-module-catalog-dropdown-sortMykhailo Shatilov– Mykhailo Shatilov2017-10-02 13:56:00 +00:00Commented Oct 2, 2017 at 13:56
- Your module look exactly what I need. Could you give me instructions on how to install it?Greg– Greg2017-12-08 14:15:07 +00:00Commented Dec 8, 2017 at 14:15
- @MykhailoShatilov your module showing invalid template error for me.Kowsigan Atsayam– Kowsigan Atsayam2019-09-13 15:31:48 +00:00Commented Sep 13, 2019 at 15:31
Add a comment |
1 Answer
Step 1: Create plugins
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
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
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; } } - Its not working. I tried to apply it in Magento 2.1.6Indian– Indian2017-06-09 05:34:34 +00:00Commented Jun 9, 2017 at 5:34
- No. Its not working in firefox as well. Your mention jquery function is not called.Indian– Indian2017-06-09 06:08:21 +00:00Commented Jun 9, 2017 at 6:08
- 1
- 2It doesn't work for me for products with special_price. Can you check if it is working with a mixed set of products, without special price and with special price?amitshree– amitshree2019-02-08 06:26:38 +00:00Commented Feb 8, 2019 at 6:26
- 1Thanks bro :) working fine with 2.3.0 and testedAdarsh Ediyottil– Adarsh Ediyottil2019-08-21 17:23:35 +00:00Commented Aug 21, 2019 at 17:23