Добавим товары из указанных разделов (включая подразделы) в новый целевой раздел, сохраняя при этом все существующие привязки товаров.
Код |
---|
<?php
require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_before.php");
CModule::IncludeModule("iblock");
$IBLOCK_ID = 2; // ID инфоблока
$SOURCE_SECTIONS = [10, 15]; // Разделы-источники
$TARGET_SECTION = 25; // Целевой раздел
$filter = [
'IBLOCK_ID' => $IBLOCK_ID,
'SECTION_ID' => $SOURCE_SECTIONS,
'INCLUDE_SUBSECTIONS' => 'Y'
];
$elements = CIBlockElement::GetList([], $filter, false, false, ['ID']);
$element = new CIBlockElement;
while ($item = $elements->Fetch()) {
$currentSections = [];
$sectionList = CIBlockElement::GetElementGroups($item['ID'], true);
while ($section = $sectionList->Fetch()) {
$currentSections[] = $section['ID'];
}
if (!in_array($TARGET_SECTION, $currentSections)) {
$currentSections[] = $TARGET_SECTION;
CIBlockElement::SetElementSection($item['ID'], $currentSections);
echo "Товар {$item['ID']} обновлен<br>";
}
}
require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/epilog_after.php"); |