Magento 2.2 Price Sorting – Fix Incorrect Product Sorting by Price

magento2pricesorting

I'm using Magento CE 2.2.6
In front-end when we sort the category pages by price, the sorting happens wrongly.
enter image description here
Any suggestions are appreciated.

Best Answer

As I investigated this seems to be a Magento core issue. https://github.com/magento/magento2/issues/18264

The fix for this is provided in below link. https://github.com/magento/magento2/commit/14ab8ace12f0e95a476675548d0712d5ecaf9a26#diff-5a30a751305f50e9d7bbd8a2576a2b46

Fix:

Create a patch file in 'patches' folder inside your project directory and add the below content there.

--- /vendor/magento/module-catalog/Model/ResourceModel/Product/Indexer/Price/Query/BaseFinalPrice.php   2018-10-25 17:11:00.749047680 +0530
+++ /vendor/magento/module-catalog/Model/ResourceModel/Product/Indexer/Price/Query/BaseFinalPrice.php   2018-10-25 17:15:19.657517309 +0530
@@ -190,7 +190,7 @@
         $specialFromExpr = "{$specialFrom} IS NULL OR {$specialFromDate} <= {$currentDate}";
         $specialToExpr = "{$specialTo} IS NULL OR {$specialToDate} >= {$currentDate}";
         $specialPriceExpr = $connection->getCheckSql(
-            "{$specialPrice} IS NOT NULL AND {$specialFromExpr} AND {$specialToExpr}",
+            "{$specialPrice} IS NOT NULL AND ({$specialFromExpr}) AND ({$specialToExpr})",
             $specialPrice,
             $maxUnsignedBigint
         );

Once you create the patch file, then run a composer update (or install if you prefer) and then do a full reindex.

That's it. Hope this will help someone in need.

NOTE: I have done this with Magento CE 2.2.6

Related Topic