Skip to content

Magento Cloud – Cannot sorting products list by min_price DESC or ASC

I’m developing a website using Magento Cloud.
I created 2 new sort-by options as attachments below:

enter image description here

When I select sort-by “Price Low to High” or “Price High to Low”. I always get messages:

We can’t find products matching the selection.


This’s my code:

  1. di.xml file
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <type name="MagentoCatalogBlockProductProductListToolbar">
       <plugin name="custom_custom_block_toolbar" type="VendorCatalogPluginCatalogBlockToolbarPlugin"/>
   </type>
   <type name="MagentoCatalogModelConfig">
       <plugin name="custom_catalog_model_config" type="VendorCatalogPluginCatalogModelConfigPlugin"/>
   </type>
</config>
  1. ConfigPlugin.php
<?php

declare(strict_types=1);

namespace VendorCatalogPluginCatalogModel;

use MagentoCatalogModelConfig;

class ConfigPlugin
{
    const LOW_TO_HIGH = 'low_to_high';
    const HIGH_TO_LOW = 'high_to_low';

    /**
     * @param Config $catalogConfig
     * @param $options
     * @return mixed
     */
    public function afterGetAttributeUsedForSortByArray(Config $catalogConfig, $options)
    {
        $options[self::LOW_TO_HIGH] = __('Price Low to High');
        $options[self::HIGH_TO_LOW] = __('Price High To Low');
        return $options;
    }
}
  1. ToolbarPlugin.php
<?php

declare(strict_types=1);

namespace VendorCatalogPluginCatalogBlock;

use Closure;
use MagentoCatalogBlockProductProductListToolbar;
use VendorCatalogPluginCatalogModelConfigPlugin;

class ToolbarPlugin
{
    /**
     * @param Toolbar $subject
     * @param Closure $proceed
     * @param $collection
     * @return mixed
     */
    public function aroundSetCollection(
        Toolbar $subject,
        Closure $proceed,
        $collection
    ) {
        $currentOrder = $subject->getCurrentOrder();
        $result = $proceed($collection);

        if (in_array($currentOrder, [ConfigPlugin::LOW_TO_HIGH, ConfigPlugin::HIGH_TO_LOW])) {
            if ($currentOrder == ConfigPlugin::LOW_TO_HIGH) {
                $subject->getCollection()->getSelect()->order(
                    new Zend_Db_Expr("price_index.min_price = 0, price_index.min_price ASC")
                );
            }
            if ($currentOrder == ConfigPlugin::HIGH_TO_LOW) {
                $subject->getCollection()->getSelect()->order(
                    new Zend_Db_Expr("price_index.min_price = 0, price_index.min_price DESC")
                );
            }
        }

        return $result;
    }
}

Does anyone know what’s happened?
Thanks in advance!