$this->load->model('catalog/category');
$this->load->model('catalog/product');
$this->data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
//Показывать или нет количество товаров
$show_product_count = $this->config->get('config_product_count');
foreach ($categories as $category) {
//Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
$PIDs=array();
if ($show_product_count) {
$res = $this->model_catalog_product->getTotalProductsID(array('filter_category_id' => $category['category_id']));
foreach ($res as $key=>$value) {
$PIDs[$value['product_id']]=$value['product_id'];
}
}
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
//Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
if ($show_product_count) {
$data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$res = $this->model_catalog_product->getTotalProductsID($data);
$product_total=count($res);
foreach ($res as $key=>$value) {
$PIDs[$value['product_id']]=$value['product_id'];
}
// $total += count($PIDs);
}
$children_data[] = array(
'category_id' => $child['category_id'],
'name' => $child['name'] . ($show_product_count ? ' (' . $product_total . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
'image' => $child['image'],
);
}
$total = count($PIDs);
$this->data['categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'] . ($show_product_count ? ' (' . $total . ')' : ''),
'children' => $children_data,
'href' => $this->url->link('product/category', 'path=' . $category['category_id']),
'image' => $category['image'],
);
}
Пока написал, пришло озарение. Вот получилось.