1

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="Magento\Catalog\Block\Product\ProductList\Toolbar">
       <plugin name="custom_custom_block_toolbar" type="Vendor\Catalog\Plugin\Catalog\Block\ToolbarPlugin"/>
   </type>
   <type name="Magento\Catalog\Model\Config">
       <plugin name="custom_catalog_model_config" type="Vendor\Catalog\Plugin\Catalog\Model\ConfigPlugin"/>
   </type>
</config>
  1. ConfigPlugin.php
<?php

declare(strict_types=1);

namespace Vendor\Catalog\Plugin\Catalog\Model;

use Magento\Catalog\Model\Config;

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 Vendor\Catalog\Plugin\Catalog\Block;

use Closure;
use Magento\Catalog\Block\Product\ProductList\Toolbar;
use Vendor\Catalog\Plugin\Catalog\Model\ConfigPlugin;

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!

2
  • ... it seems this issue is quite difficult :(( Commented Aug 14, 2022 at 13:57
  • @Nguyen Hoang Tuan Did you get the solution? Commented Mar 9, 2023 at 8:31

1 Answer 1

0

i'm also facing this issue, does anyone have updates?

Your Answer

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

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