Created
August 11, 2020 10:19
-
-
Save livevasiliy/c5c10e868cca5c968d208c1b2ad5bfe4 to your computer and use it in GitHub Desktop.
Получить все товары из подразделов и сгруппировать по родительской категорий
This file contains 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
<?php | |
use Bitrix\Main\Loader; | |
use Bitrix\Main\LoaderException; | |
const CATALOG_IBLOCK_ID = 17; // ID Инфоблока Каталога | |
function fetchAllProducts() | |
{ | |
try { | |
$rootSections = []; // корневые категорий | |
Loader::includeModule('iblock'); | |
$rsParentSection = CIBlockSection::GetList( | |
array('ID' => 'ASC'), | |
array( | |
'IBLOCK_ID' => CATALOG_IBLOCK_ID, | |
'DEPTH_LEVEL' => 1, | |
), | |
true, | |
array( | |
'ID', | |
'NAME', | |
'LEFT_MARGIN', | |
'RIGHT_MARGIN', | |
'DEPTH_LEVEL', | |
'IBLOCK_SECTION_ID', | |
), | |
false | |
); | |
while ($arSection = $rsParentSection->Fetch()) { | |
$rootSections[$arSection['ID']] = $arSection; | |
} | |
$childrenSections = []; | |
foreach ($rootSections as $rootSection) { | |
$arFilter = array( | |
'SECTION_ID' => $rootSection['ID'], | |
'ACTIVE' => 'Y', | |
); | |
$arSelect = array('ID', 'NAME'); | |
$rsSection = CIBlockSection::GetTreeList($arFilter, $arSelect); | |
while ($arSection = $rsSection->Fetch()) { | |
$arSection['SECTION_NAME'] = $arSection['NAME']; | |
$childrenSections[$rootSection['ID']][] = $arSection; | |
} | |
} | |
$arProducts = []; | |
foreach ($childrenSections as $rootSectionId => $childrens) { | |
foreach ($childrens as $children) { | |
$product = CIBlockElement::GetList( | |
array('SORT' => 'ASC'), | |
array( | |
'SECTION_ID' => $children['ID'], | |
'IBLOCK_ID' => CATALOG_IBLOCK_ID, | |
), | |
false, | |
false, | |
array( | |
'ID', | |
'IBLOCK_ID', | |
'ACTIVE', | |
'NAME', | |
'CODE', | |
'IBLOCK_SECTION_ID', | |
'IBLOCK_SECTION', | |
) | |
); | |
while ($arProduct = $product->Fetch()) { | |
$arProduct['ROOT_SECTION_ID'] = $rootSectionId; | |
$arProducts[$rootSectionId]['SECTION_NAME'] | |
= $rootSections[$rootSectionId]['NAME']; | |
$arProducts[$rootSectionId]['PRODUCTS'][] = $arProduct; | |
} | |
} | |
} | |
print_r($arProducts); | |
} catch (LoaderException $exception) { | |
print_r($exception->getMessage()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment