Сниппет возвращает количество дочерних ресурсов, а также склоняет слово "товар" после числительного.
<?php
$parent = $modx->getOption('parent', $scriptProperties, $modx->resource->get('parent'));
$depth = $modx->getOption('depth', $scriptProperties, 5);
$template = $modx->getOption('template', $scriptProperties, false);
$toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, false);
if(!function_exists('goods_declension')) {
function goods_declension($number) {
$titles = array('%d товар', '%d товара', '%d товаров');
$cases = array(2, 0, 1, 1, 1, 2);
$format = $titles[($number%100 > 4 && $number %100 < 20) ? 2 : $cases[min($number%10, 5)]];
return sprintf($format, $number);
}
}
$children = $modx->getChildIds($parent, $depth, array('context' => 'web'));
$query = $modx->newQuery('modResource', array('id:IN' => $children, 'published' => true, 'deleted' => false));
if(!empty($template)) {
$query->where(array('template' => (int)$template));
}
$collection = $modx->getCollection('modResource', $query);
$resources = array();
if($collection) {
foreach($collection as $resource) {
$resources[] = $resource->toArray();
}
}
$count = count($resources);
$output = goods_declension($count);
if (!empty($toPlaceholder)) {
$modx->setPlaceholder($toPlaceholder, $output);
}else{
return $output;
}