пользователь
lazuren представил неверное решение. Таким образом, в его логике, при выведении через twig суб-категорий, они каждый раз повторяются первые несколько. Правильный код:
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
if ($category['top']) {
// Level 2
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
// Level 3
$grandchildren_data = array();
$grandchildren = $this->model_catalog_category->getCategories($child['category_id']);
foreach ($grandchildren as $grandchild) {
$grandchild_filter_data = array(
'filter_category_id' => $grandchild['category_id'],
'filter_sub_category' => true
);
$grandchildren_data[] = array(
'name' => $grandchild['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'] . '_' . $grandchild['category_id']),
);
}
$filter_data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$children_data[] = array(
'name' => $child['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
'children' => $grandchildren_data,
);
}
// Level 1
$data['categories'][] = array(
'name' => $category['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id']),
'children' => $children_data,
'column' => $category['column'] ? $category['column'] : 1,
);
}
}
в шаблоне:
{% if categories %}
<ul>
{% for category in categories %}
{% if category.children %}
<li class="dropdown"><a href="{{ category.href }}">{{ category.name }}</a><ul class="sub_level2">
{% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}
{% for child in children %}
{% if child.children %}
<li><a href="{{ child.href }}">{{ child.name }}</a>
<ul class="sub_level3">
{% for children in child.children %}
<li><a href="{{ children.href }}">{{ children.name }}</a></li>
{% endfor %}
</ul>
</li>
{% else %}
<li><a href="{{ child.href }}">{{ child.name }}</a></li>
{% endif %}
{% endfor %}
</ul>
</li>
{% endfor %}
{% else %}
<li><a href="{{ category.href }}">{{ category.name }}</a></li>
{% endif %}
{% endfor %}
</ul>
{% endif %}