$counts =
[];
foreach ($levels as $category) { if (!
$category->
getActive() || !
$category->
getVisible()) { continue;
} $parentId =
$category->
getParentId();
$counts[$parentId] ??= 0;
++
$counts[$parentId];
} foreach ($levels as $category) { $category->
setVisibleChildCount($counts[$category->
getId()] ?? 0
);
} // Fetch additional level of categories for counting visible children that are NOT included in the original query
$criteria =
new Criteria();
$criteria->
addFilter( new ContainsFilter('path', '|' .
$rootId . '|'
),
new EqualsFilter('level',
$rootLevel +
$depth + 1
),
new EqualsFilter('active', true
),
new EqualsFilter('visible', true
) );