1

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;
 }
 }
}
asked Jul 20, 2019 at 15:54

2 Answers 2

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.

answered Jul 20, 2019 at 17:38
1
  • I tried this solution but not working. Commented Jul 22, 2019 at 7:13
0

try this code

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

<?php
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':
 $result->getCollection()->setOrder('price', 'asc');
 break;
 case 'high_to_low':
 $result->getCollection()->setOrder('price', 'desc');
 break;
 default:
 // Add a default sorting here if needed
 break;
 }
 return $result;
 }
}

I removed unnecessary calls to $result->getCollection(). The collection is already available as a parameter, so you can directly use it.

I added break; statements after each case to exit the switch statement once a case is matched. This prevents fall-through to the default case, ensuring that only one sorting option is applied.

After making these changes, clear the cache and test the "low to high" and "high to low" sorting options in your Magento store.

answered Dec 28, 2023 at 12:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.