Files
vinoteca/local/modules/jinove.vinoteca/lib/catalog/pricerange.php
T

49 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Jinove\Vinoteca\Catalog;
use Bitrix\Catalog\PriceTable;
use Bitrix\Currency\CurrencyManager;
use Bitrix\Main\Entity\ExpressionField;
use Bitrix\Main\Loader;
use Jinove\Vinoteca\Config;
use Jinove\Vinoteca\Cached;
class PriceRange
{
private const CACHE_TTL = 86400;
private const CACHE_DIR = "/jinove.vinoteca/pricerange";
public static function get(string $priceCode = "BASE"): string
{
$catalogId = Config::iblockId("catalog");
return (string)Cached::remember(self::CACHE_DIR, "range-" . $catalogId . "-" . $priceCode, self::CACHE_TTL, ["iblock_id_" . $catalogId], static fn(): string => self::collect($priceCode));
}
private static function collect(string $priceCode): string
{
if (!Loader::includeModule("catalog") || !Loader::includeModule("currency")) {
return "";
}
$groupId = PricePresets::groupId($priceCode);
if ($groupId === 0) {
return "";
}
$row = PriceTable::getList([
"select" => ["MIN_PRICE", "MAX_PRICE"],
"filter" => ["=CATALOG_GROUP_ID" => $groupId, ">PRICE" => 0],
"runtime" => [
new ExpressionField("MIN_PRICE", "MIN(%s)", "PRICE"),
new ExpressionField("MAX_PRICE", "MAX(%s)", "PRICE"),
],
])->fetch();
if (!$row || $row["MIN_PRICE"] <= 0) {
return "";
}
return (int)round((float)$row["MIN_PRICE"]) . "-" . (int)round((float)$row["MAX_PRICE"]) . " " . CurrencyManager::getBaseCurrency();
}
}