5

I want a custom price range for filter like this:

  • 0-1999.
  • 2000-4999.
  • 5000-8999.

How can I do that?

4
  • This method works great, except I am having trouble with the counts being wrong. I believe that is because this line: $entityCount = count($entityStorage->getSource()); This line doesn't return an accurate count of the entities. Any ideas on what might be the issue? Oct 3, 2018 at 18:05
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – sv3n
    Oct 3, 2018 at 19:13
  • I found the answer: Replace: $entityCount = count($entityStorage->getSource()); with: $aggregations = $this->dataProvider->getAggregations($entityStorage); $entityCount = $aggregations['count']; Oct 3, 2018 at 21:16
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review Oct 3, 2018 at 21:51

2 Answers 2

5

On the Admin Panel, Stores > Settings > Configuration.

In the left panel, under Catalog, select Catalog. Open the Layered Navigation section. In the Display Product Count field, enable to enter the number of products that is included in parentheses after each filtered item. Make Price Navigation Steps Calculation by two methods as the following:

Method 1: Automatic (equalize price ranges) This method is to use the standard algorithm for price navigation. In the Price Navigation Steps Calculation, select the Automatic (Equalize Price Ranges).

enter image description here

Method 2: Automatic (equalize product counts)

In the Price Navigation Steps Calculation field, select Automatic (equalize product counts) option.

In the Display Price Interval as One Price field, allow showing a single price for many products that are both at the same price if you choose Yes. In the Interval Division Limit field, offer the threshold for a number of products within a price range. The range cannot be further split beyond this limit. If ignore it, the default value (9) is applied.

enter image description here

Save Config to complete.

Refer this for more detail - Configure-price-filter-price-navigation

1
  • i know but step of me different of you. How can i fix it Feb 14, 2017 at 9:37
5

There's a few steps to adding a custom range algorithm:
1. Create custom range class implementing Magento\Framework\Search\Dynamic\Algorithm\AlgorithmInterface
2. Add algorithm class to Magento\Framework\Search\Dynamic\Algorithm\Repository
3. Create plugin on Magento\Catalog\Model\Config\Source\Price\Step::toOptionArray so your custom range option is available in the admin panel.

Vendor/Module/Search/Dynamic/Algorithm/MyCustom.php

<?php

namespace Vendor\Module\Search\Dynamic\Algorithm;

use Magento\Framework\Search\Dynamic\DataProviderInterface;
use Magento\Framework\Search\Dynamic\EntityStorage;
use Magento\Framework\Search\Request\BucketInterface;

class MyCustom implements \Magento\Framework\Search\Dynamic\Algorithm\AlgorithmInterface
{
    const RANGE_CALCULATION_MY_CUSTOM = 'my_custom';
    const RANGES = [
        [null, 2000],
        [2000, 5000],
        [5000, 9000],
        [9000, null],
    ];

    /**
     * @var DataProviderInterface
     */
    private $dataProvider;

    /**
     * @param DataProviderInterface $dataProvider
     */
    public function __construct(
        DataProviderInterface $dataProvider
    ) {
        $this->dataProvider = $dataProvider;
    }

    /**
     * {@inheritdoc}
     */
    public function getItems(
        BucketInterface $bucket,
        array $dimensions,
        EntityStorage $entityStorage
    ) {
        $entityCount = count($entityStorage->getSource());
        if ($entityCount < 1) {
            return [];
        }

        $data = [];
        $interval = $this->dataProvider->getInterval($bucket, $dimensions, $entityStorage);
        foreach (self::RANGES as $range) {
            $rangeResult = $interval->load($entityCount, null, $range[0], $range[1]);

            $count = count($rangeResult);
            if ($count < 1) {
                continue;
            }

            $data [] = [
                'count' => $count,
                'from' => $range[0],
                'to' => $range[1],
            ];
        }

        return $data;
    }
}

Vendor/Module/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\Framework\Search\Dynamic\Algorithm\Repository">
        <arguments>
            <argument name="algorithms" xsi:type="array">
                <item name="my_custom" xsi:type="string">Vendor\Module\Search\Dynamic\Algorithm\MyCustom</item>
            </argument>
        </arguments>
    </type>
</config>

Vendor/Module/Plugin/Config/Source/Price/Step.php

<?php

namespace Vendor\Module\Plugin\Config\Source\Price;

use Vendor\Module\Search\Dynamic\Algorithm\MyCustom;

class Step
{
    public function afterToOptionArray(
        \Magento\Catalog\Model\Config\Source\Price\Step $subject,
        array $result
    ) {
        $result[] = [
            'label' => __('My Custom Ranges'),
            'value' => MyCustom::RANGE_CALCULATION_MY_CUSTOM,
        ];

        return $result;
    }
}

Vendor/Module/etc/adminhtml/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\Model\Config\Source\Price\Step">
        <plugin name="vendor_custom_price_range" type="Vendor\Module\Plugin\Config\Source\Price\Step" sortOrder="10" />
    </type>
</config>
1
  • I much appreciate the answer... but I got an error report stating... Something went wrong, check and test your Search Engine configurations, reindex catalog and clear cache. Thrown error: Warning: count(): Parameter must be an array or an object that implements Countable in Vendor\/Module\/Search\/Dynamic\/Algorithm\/MyCustom.php on line 45 which is $entityCount = count($entityStorage->getSource());
    – Barry
    Sep 20, 2019 at 16:32

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.