如何在下拉菜单Opencart中扩展所有子类别?

问题描述 投票:0回答:1

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

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

我的opencart版本是3.0.2

opencart categories dropdown
1个回答
0
投票

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

因为你只能看到2个级别。您可以添加代码以类似的方式扩展第二级,包括它也作为1级子级的一部分。

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

或者您可以将子项添加到第二级作为第三级,然后在twig文件中添加用于显示第二级子项的代码。

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.twigline 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>
© www.soinside.com 2019 - 2024. All rights reserved.