0

I have created a custom plugin for sorting options for high to low & low to high price. High to low sort works fine but "low to high" sort order not showing correct results.

app/code/Digital/CustomSort/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="digital_customsort_block_product_productlist_toolbar" type="Digital\CustomSort\Plugin\Catalog\Block\Product\ProductList\Toolbar" />
    </type>
    <type name="Magento\Catalog\Model\Config">
        <plugin name="digital_customsort_model_config" type="Digital\CustomSort\Plugin\Catalog\Model\Config" />
    </type>
</config>

app/code/Digital/CustomSort/Plugin/Catalog/Model/Config.php

<?php
//namespace Digital\CustomSort\Plugin\Model;
//class Config extends \Magento\Catalog\Model\Config

namespace Digital\CustomSort\Plugin\Catalog\Model;
use Magento\Catalog\Model\Config as CatalogConfig;

class Config
{
    public function afterGetAttributeUsedForSortByArray(CatalogConfig $subject, $result)
    {
        $result['low_to_high'] = __('Price - Low To High');
        $result['high_to_low'] = __('Price - High To Low');
        return $result;
    }
}

Digital/CustomSort/Plugin/Catalog/Block/Product/ProductList/Toolbar.php

<?php
 //namespace Digital\CustomSort\Plugin\Block\Product\ProductList;
 //class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar

namespace Digital\CustomSort\Plugin\Catalog\Block\Product\ProductList;

use Magento\Catalog\Block\Product\ProductList\Toolbar as ProductListToolbar;

class Toolbar
{
    public function afterSetCollection(ProductListToolbar $subject, $result, $collection)
    {
        switch ($subject->getCurrentOrder()) {
            case 'low_to_high':
                return $result->getCollection()->setOrder('price', 'asc');
            case 'high_to_low':
                return $result->getCollection()->setOrder('price', 'desc');
            default:
                return $result;
        }
    }
}
0

Your solution is really similar to this

https://magento.stackexchange.com/a/163433/70343

Have you seen this answer? Makes sense to me although some users have said it doesnt work.

Plus it doesnt factor special pricing.

  • I tried this solution but not working. – Shoaib Saleem Jul 22 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.