I want a custom price range for filter like this:
- 0-1999.
- 2000-4999.
- 5000-8999.
How can I do that?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI want a custom price range for filter like this:
How can I do that?
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).
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.
Save Config to complete.
Refer this for more detail - Configure-price-filter-price-navigation
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>
$entityCount = count($entityStorage->getSource());