我尝试在 opencart 3.x 的子类别中获取子,我在 menu.php 控制器和 menu.twig 文件中更改了代码,但是没有子类别的显示子。我在 catalogcontroller/common/menu.php 中有编辑代码
public function index() {
$this->load->language('common/menu');
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$data['categories'] = array();
$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
$children_data2 = array();
$children2 = $this->model_catalog_category->getCategories($child['category_id']);
foreach ($children2 as $child2) {
$children_data2[] = array(
'name' => $child2['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'].'_'.$child2['category_id'])
);
}
$filter_data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$children_data[] = array(
'children'=>$children_data2,
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
// Level 1
$data['categories'][] = array(
'name' => $category['name'],
'children' => $children_data,
'column' => $category['column'] ? $category['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
}
return $this->load->view('common/menu', $data);
}
并更改 common/menu.twig 文件。
{% if child.children %}
<ul class="dropdown-menu sub-menu">
{% for child in child.children %}
<li>
<a href="{{ child.href }}" >{{ child.name }}</a>
</li>
{% endfor %}
</ul>
{% endif %}