init
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Plugin\WeltPixel;
|
||||
|
||||
use Magento\Catalog\Model\Layer\Filter\FilterInterface;
|
||||
use WeltPixel\LayeredNavigation\Block\Navigation\FilterRenderer;
|
||||
|
||||
class PriceStepCountsPlugin
|
||||
{
|
||||
public function beforeRender(FilterRenderer $subject, FilterInterface $filter): array
|
||||
{
|
||||
if ($filter->getRequestVar() !== 'price') {
|
||||
return [$filter];
|
||||
}
|
||||
|
||||
$collection = $filter->getLayer()->getProductCollection();
|
||||
|
||||
if (!method_exists($collection, 'getFacetedData')) {
|
||||
$subject->setData('esun_price_step_ranges', []);
|
||||
return [$filter];
|
||||
}
|
||||
|
||||
$ranges = [];
|
||||
foreach ($collection->getFacetedData('price') as $bucket) {
|
||||
$count = (int)($bucket['count'] ?? 0);
|
||||
if ($count <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = (string)($bucket['value'] ?? '');
|
||||
$from = $bucket['from'] ?? null;
|
||||
$to = $bucket['to'] ?? null;
|
||||
|
||||
if (($from === null || $to === null) && str_contains($value, '_')) {
|
||||
[$fromValue, $toValue] = explode('_', $value, 2);
|
||||
$from = $fromValue === '*' ? null : (float)$fromValue;
|
||||
$to = $toValue === '*' ? null : (float)$toValue;
|
||||
}
|
||||
|
||||
if ($from === null && $to === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ranges[] = [
|
||||
'from' => $from === null ? '' : (float)$from,
|
||||
'to' => $to === null ? '' : (float)$to,
|
||||
'count' => $count,
|
||||
'value' => $value,
|
||||
];
|
||||
}
|
||||
|
||||
usort($ranges, static function (array $left, array $right): int {
|
||||
return (float)$left['from'] <=> (float)$right['from'];
|
||||
});
|
||||
|
||||
$subject->setData('esun_price_step_ranges', $ranges);
|
||||
|
||||
return [$filter];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user