Files
esun3d/app/code/Esun/Catalog/Plugin/WeltPixel/PriceStepCountsPlugin.php
T
2026-09-15 10:35:56 +08:00

62 lines
1.8 KiB
PHP

<?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];
}
}