如何使下拉菜单 Opencart 中的所有子类别都可扩展?

How to make all subcategories expandable in dropdown menu Opencart?

我有这样的类别: 类别>sub1>sub2 在下拉菜单中,sub2 不可展开。

我必须点击 sub1 才能看到 sub2。如何解决这个问题?

我的opencart版本是3.0.2

OpenCart的主菜单只显示2级。顶层和子层。第二个子级别不会显示。你需要自己开发它。

您可以在 catalog/controller/common/menu.php

中找到菜单逻辑的代码
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) {
            $filter_data = array(
                'filter_category_id'  => $child['category_id'],
                'filter_sub_category' => true
            );

            $children_data[] = array(
                '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'])
        );
    }
}

as you can see only 2 levels. you can add the code to extand the second level in a similar way include it also as part of the children in the 1 level.

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) {
            $filter_data = array(
                'filter_category_id'  => $child['category_id'],
                'filter_sub_category' => true
            );

            $children_data[] = array(
                '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 3

            $children2 = $this->model_catalog_category->getCategories($child['category_id']);

            foreach ($children2 as $child2) {
                $filter_data = array(
                    'filter_category_id'  => $child2['category_id'],
                    'filter_sub_category' => true
                );
                 //adding everything to the second level
                $children_data[] = array(
                    'name'  => $child2['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'] . '_' .$child2['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'])
        );
    }
}

or you can add the children to the second level as the third level and then in twig file add the code for displaying children of the second level.

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) {
            $filter_data = array(
                'filter_category_id'  => $child['category_id'],
                'filter_sub_category' => true
            );



            //level 3
            $children_data2 = array();

            $children2 = $this->model_catalog_category->getCategories($child['category_id']);

            foreach ($children2 as $child2) {
                $filter_data = array(
                    'filter_category_id'  => $child2['category_id'],
                    'filter_sub_category' => true
                );

                $children_data2[] = array(
                    'name'  => $child2['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'] . '_' .$child2['category_id'])
                );
            }

            $children_data[] = array(
                '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']),
                'children' => $children_data2
            );
        }

        // 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'])
        );
    }
}

并在 twig 文件中 catalog/view/theme/default/template/common/menu.twig 将此代码替换为 line 16

<li><a href="{{ child.href }}">{{ child.name }}</a></li>

有了这个

<li><a href="{{ child.href }}">{{ child.name }}</a>
    {% for child2 in child['children'] %}
         <li><a class="small" href="{{ child2.href }}"> > {{ child2.name }}</a></li>
    {% endfor %}
</li>