Created
October 27, 2017 03:27
-
-
Save SergeyZaigraev/fa7f8b69df9e61201e6d1074b0713619 to your computer and use it in GitHub Desktop.
Smart filter links for sitemap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? require_once($_SERVER['DOCUMENT_ROOT'] . "/bitrix/modules/main/include/prolog_before.php"); | |
use Bitrix\Main\Loader; | |
use Bitrix\Iblock\PropertyIndex; | |
Loader::includeModule("iblock"); | |
CBitrixComponent::includeComponentClass('bitrix:catalog.smart.filter'); | |
$iblockID = 3; | |
$iblockSkuID = 4; | |
$arSections = CIBlockSection::GetList( | |
array( | |
"ID" => "DESC" | |
), | |
array( | |
"IBLOCK_ID" => $iblockID, | |
"ACTIVE" => "Y", | |
), | |
false, | |
array( | |
"ID", | |
), | |
false | |
); | |
$arSectionID = array(); | |
while ($arSectionsRes = $arSections->Fetch()){ | |
$arSectionID[] = $arSectionsRes['ID']; | |
} | |
$FilterObj = new CBitrixCatalogSmartFilter; | |
$Facet = new PropertyIndex\Facet($iblockID); | |
// раздел с товарами | |
$arrResultSmartLink = array(); | |
if(!empty($arSectionID)){ | |
foreach ($arSectionID as $sectId){ | |
$arResultLink = CIBlockSectionPropertyLink::GetArray($iblockID,$sectId); | |
$arResultLinkSku = CIBlockSectionPropertyLink::GetArray($iblockSkuID,$sectId); | |
// массив со свойствами раздела | |
$arResultProperty = array(); | |
//TODO Product | |
foreach ($arResultLink as $PID => $arLink){ | |
if ($arLink["SMART_FILTER"] !== "Y") | |
continue; | |
if ($arLink["ACTIVE"] === "N") | |
continue; | |
if ($arLink['FILTER_HINT'] <> '') | |
{ | |
$arLink['FILTER_HINT'] = CTextParser::closeTags($arLink['FILTER_HINT']); | |
} | |
$rsProperty = CIBlockProperty::GetByID($PID); | |
$arProperty = $rsProperty->Fetch(); | |
$arResultProperty[$arProperty['ID']] = $arProperty; | |
} | |
//TODO Sku | |
foreach ($arResultLinkSku as $PID => $arLink){ | |
if ($arLink["SMART_FILTER"] !== "Y") | |
continue; | |
if ($arLink["ACTIVE"] === "N") | |
continue; | |
if ($arLink['FILTER_HINT'] <> '') | |
{ | |
$arLink['FILTER_HINT'] = CTextParser::closeTags($arLink['FILTER_HINT']); | |
} | |
$rsProperty = CIBlockProperty::GetByID($PID); | |
$arProperty = $rsProperty->Fetch(); | |
$arResultProperty[$arProperty['ID']] = $arProperty; | |
} | |
if(!empty($arResultProperty)){ | |
$arSectionInfo = CIBlockSection::GetByID($sectId)->GetNext(); | |
$sefSmartUrl = $arSectionInfo['SECTION_PAGE_URL'] . "filter/#SMART_FILTER_PATH#/"; | |
$arFacetFilter = array( | |
'ACTIVE_DATE' => 'Y', | |
'CHECK_PERMISSIONS' => 'Y', | |
'CATALOG_AVAILABLE' => 'Y' | |
); | |
if($Facet->isValid()){ | |
$Facet->setSectionId($sectId); | |
$res = $Facet->query( | |
$arFacetFilter | |
); | |
while($row = $res->fetch()){ | |
$facetId = $row["FACET_ID"]; | |
if (\Bitrix\Iblock\PropertyIndex\Storage::isPropertyId($facetId)) { | |
$PID = \Bitrix\Iblock\PropertyIndex\Storage::facetIdToPropertyId($facetId); | |
if ($arResultProperty[$PID]["PROPERTY_TYPE"] == "N") { | |
continue; | |
} elseif ($arResultProperty[$PID]["DISPLAY_TYPE"] == "U") { | |
continue; | |
} elseif ($arResultProperty[$PID]["PROPERTY_TYPE"] == "S") { | |
$addedKey = $FilterObj->fillItemValues($arResultProperty[$PID], $Facet->lookupDictionaryValue($row["VALUE"]), true); | |
if (strlen($addedKey) > 0) { | |
$arResultProperty[$PID]["VALUES"][$addedKey]["FACET_VALUE"] = $row["VALUE"]; | |
$arResultProperty[$PID]["VALUES"][$addedKey]["ELEMENT_COUNT"] = $row["ELEMENT_COUNT"]; | |
} | |
} else { | |
$addedKey = $FilterObj->fillItemValues($arResultProperty[$PID], $row["VALUE"], true); | |
if (strlen($addedKey) > 0) { | |
$arResultProperty[$PID]["VALUES"][$addedKey]["FACET_VALUE"] = $row["VALUE"]; | |
$arResultProperty[$PID]["VALUES"][$addedKey]["ELEMENT_COUNT"] = $row["ELEMENT_COUNT"]; | |
} | |
} | |
} | |
} | |
foreach ($arResultProperty as $PID => $item){ | |
$code = null; | |
if ($item["CODE"]){ | |
$code = toLower($item["CODE"]); | |
}else{ | |
$code = $item["ID"]; | |
} | |
if(!empty($item["VALUES"]) && is_array($item["VALUES"])){ | |
foreach ($item["VALUES"] as $key => $val){ | |
$smartLink = str_replace("#SMART_FILTER_PATH#", implode("/", $FilterObj->encodeSmartParts( | |
array( | |
array( | |
$code,$val["URL_ID"]) | |
) | |
) | |
), $sefSmartUrl); | |
$arrResultSmartLink[] = $smartLink; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
// pr($arrResultSmartLink); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
генерация ссылок умного фильтра