Для этого настраиваем сортировку по полям. Создаём в инфоблоке свойства с типом число:
MINIMUM_PRICE
MAXIMUM_PRICE
Этот код добавляем в init.php (если нужно автоматом к уже загруженным товарам сгенерировать максимальную и минимальную цену, то код следующий)
<?
AddEventHandler("iblock", "OnAfterIBlockElementUpdate", "DoIBlockAfterSave");
AddEventHandler("iblock", "OnAfterIBlockElementAdd", "DoIBlockAfterSave");
AddEventHandler("catalog", "OnPriceAdd", "DoIBlockAfterSave");
AddEventHandler("catalog", "OnPriceUpdate", "DoIBlockAfterSave");
function DoIBlockAfterSave($arg1, $arg2 = false){
global $USER;
$ELEMENT_ID = false;
$IBLOCK_ID = false;
$OFFERS_IBLOCK_ID = false;
$OFFERS_PROPERTY_ID = false;
$BX_SITE_ID = "s1"; //you site id
if (CModule::IncludeModule('currency'))
$strDefaultCurrency = CCurrency::GetBaseCurrency();
//Check for catalog event
if(is_array($arg2) && $arg2["PRODUCT_ID"] > 0){
//Get iblock element
$rsPriceElement = CIBlockElement::GetList(
array(),
array(
"ID" => $arg2["PRODUCT_ID"],
),
false,
false,
array("ID", "IBLOCK_ID")
);
if($arPriceElement = $rsPriceElement->Fetch()){
$arCatalog = CCatalog::GetByID($arPriceElement["IBLOCK_ID"]);
if(is_array($arCatalog)){
//Check if it is offers iblock
if($arCatalog["OFFERS"] == "Y"){
//Find product element
$rsElement = CIBlockElement::GetProperty(
$arPriceElement["IBLOCK_ID"],
$arPriceElement["ID"],
"sort",
"asc",
array("ID" => $arCatalog["SKU_PROPERTY_ID"])
);
$arElement = $rsElement->Fetch();
if($arElement && $arElement["VALUE"] > 0){
$ELEMENT_ID = $arElement["VALUE"];
$IBLOCK_ID = $arCatalog["PRODUCT_IBLOCK_ID"];
$OFFERS_IBLOCK_ID = $arCatalog["IBLOCK_ID"];
$OFFERS_PROPERTY_ID = $arCatalog["SKU_PROPERTY_ID"];
}
}
//or iblock which has offers
elseif($arCatalog["OFFERS_IBLOCK_ID"] > 0){
$ELEMENT_ID = $arPriceElement["ID"];
$IBLOCK_ID = $arPriceElement["IBLOCK_ID"];
$OFFERS_IBLOCK_ID = $arCatalog["OFFERS_IBLOCK_ID"];
$OFFERS_PROPERTY_ID = $arCatalog["OFFERS_PROPERTY_ID"];
}
//or it's regular catalog
else{
$ELEMENT_ID = $arPriceElement["ID"];
$IBLOCK_ID = $arPriceElement["IBLOCK_ID"];
$OFFERS_IBLOCK_ID = false;
$OFFERS_PROPERTY_ID = false;
}
}
}
}
//Check for iblock event
elseif(is_array($arg1) && $arg1["ID"] > 0 && $arg1["IBLOCK_ID"] > 0){
//Check if iblock has offers
$arOffers = CIBlockPriceTools::GetOffersIBlock($arg1["IBLOCK_ID"]);
if(is_array($arOffers)){
$ELEMENT_ID = $arg1["ID"];
$IBLOCK_ID = $arg1["IBLOCK_ID"];
$OFFERS_IBLOCK_ID = $arOffers["OFFERS_IBLOCK_ID"];
$OFFERS_PROPERTY_ID = $arOffers["OFFERS_PROPERTY_ID"];
}
}
if($ELEMENT_ID){
static $arPropCache = array();
if(!array_key_exists($IBLOCK_ID, $arPropCache)){
//Check for MINIMAL_PRICE property
$rsProperty = CIBlockProperty::GetByID("MINIMUM_PRICE", $IBLOCK_ID);
$arProperty = $rsProperty->Fetch();
if($arProperty)
$arPropCache[$IBLOCK_ID] = $arProperty["ID"];
else
$arPropCache[$IBLOCK_ID] = false;
}
if($arPropCache[$IBLOCK_ID]){
//Compose elements filter
if($OFFERS_IBLOCK_ID){
$rsOffers = CIBlockElement::GetList(
array(),
array(
"IBLOCK_ID" => $OFFERS_IBLOCK_ID,
"PROPERTY_".$OFFERS_PROPERTY_ID => $ELEMENT_ID,
),
false,
false,
array("ID")
);
while($arOffer = $rsOffers->Fetch())
$arProductID[] = $arOffer["ID"];
if (!is_array($arProductID))
$arProductID = array($ELEMENT_ID);
}
else
$arProductID = array($ELEMENT_ID);
$minPrice = false;
$maxPrice = false;
foreach ($arProductID as $productID) {
$arDiscountPrice = CCatalogProduct::GetOptimalPrice($productID, 1, $USER->GetUserGroupArray(), false, false, $BX_SITE_ID);
if (CModule::IncludeModule('currency') && $strDefaultCurrency != $arDiscountPrice['RESULT_PRICE']['CURRENCY'])
$arDiscountPrice['DISCOUNT_PRICE'] = CCurrencyRates::ConvertCurrency($arDiscountPrice['DISCOUNT_PRICE'], $arDiscountPrice['RESULT_PRICE']["CURRENCY"], $strDefaultCurrency);
$PRICE = $arDiscountPrice['DISCOUNT_PRICE'];
if($minPrice === false || $minPrice > $PRICE)
$minPrice = $PRICE;
if($maxPrice === false || $maxPrice < $PRICE)
$maxPrice = $PRICE;
}
//Save found minimal price into property
if($minPrice !== false){
CIBlockElement::SetPropertyValuesEx(
$ELEMENT_ID,
$IBLOCK_ID,
array(
"MINIMUM_PRICE" => $minPrice,
"MAXIMUM_PRICE" => $maxPrice,
)
);
}
}
}
}
?>
Для генерации уже добавленных товаров:
В $IBLOCK_ID = 18 - указываем ID инфоблока
<?
CModule::IncludeModule('iblock');
CModule::IncludeModule('catalog');
CModule::IncludeModule('currency');
$IBLOCK_ID = 18; //Инфоблок с которым работаем
//Получаем ID всех элементов
$addProps = CIBlockElement::GetList (
Array("ID" => "ASC"),
Array("IBLOCK_ID" => $IBLOCK_ID),
false,
false,
Array(
'ID'
)
);
while($ar_fields = $addProps->GetNext())
{
$arOffers = CIBlockPriceTools::GetOffersIBlock($IBLOCK_ID);
if(is_array($arOffers)){
$ELEMENT_ID = $ar_fields['ID'];
$OFFERS_IBLOCK_ID = $arOffers["OFFERS_IBLOCK_ID"];
$OFFERS_PROPERTY_ID = $arOffers["OFFERS_PROPERTY_ID"];
}
if($ELEMENT_ID){
$arPropCache = array();
if(!array_key_exists($IBLOCK_ID, $arPropCache)){
//Check for MINIMAL_PRICE property
$rsProperty = CIBlockProperty::GetByID("MINIMUM_PRICE", $IBLOCK_ID);
$arProperty = $rsProperty->Fetch();
if($arProperty)
$arPropCache[$IBLOCK_ID] = $arProperty["ID"];
else
$arPropCache[$IBLOCK_ID] = false;
}
if($arPropCache[$IBLOCK_ID]){
//Compose elements filter
if($OFFERS_IBLOCK_ID){
$rsOffers = CIBlockElement::GetList(
array(),
array(
"IBLOCK_ID" => $OFFERS_IBLOCK_ID,
"PROPERTY_".$OFFERS_PROPERTY_ID => $ELEMENT_ID,
),
false,
false,
array("ID")
);
while($arOffer = $rsOffers->Fetch()){
$arProductID[] = $arOffer["ID"];
}
if (!is_array($arProductID))
$arProductID = array($ELEMENT_ID);
}
else{$arProductID = array($ELEMENT_ID);}
$minPrice = false;
$maxPrice = false;
foreach ($arProductID as $productID) {
$arDiscountPrice = CCatalogProduct::GetOptimalPrice($productID, 1, $USER->GetUserGroupArray(), false, false, $BX_SITE_ID);
if (CModule::IncludeModule('currency') && $strDefaultCurrency != $arDiscountPrice['RESULT_PRICE']['CURRENCY']){
$arDiscountPrice['DISCOUNT_PRICE'] = CCurrencyRates::ConvertCurrency($arDiscountPrice['DISCOUNT_PRICE'], $arDiscountPrice['RESULT_PRICE']["CURRENCY"], $strDefaultCurrency);
}
$PRICE = $arDiscountPrice['PRICE']['PRICE'];
if($minPrice === false || $minPrice > $PRICE){
$minPrice = $PRICE;
}
if($maxPrice === false || $maxPrice < $PRICE)
$maxPrice = $PRICE;
}
//Save found minimal price into property
if($minPrice !== false){
CIBlockElement::SetPropertyValuesEx(
$ELEMENT_ID,
$IBLOCK_ID,
array(
"MINIMUM_PRICE" => $minPrice,
"MAXIMUM_PRICE" => $maxPrice,
)
);
}
}
}
$arPropCache = null;
unset($minPrice,$maxPrice,$arPropCache,$arProductID);
}
?>