opencart3调用三级菜单level 3 sub categories

  Opencart 3的menu菜单默认只调用一级和二级菜单,但很多电商网站类目复杂,三级菜单一般都是需要的,甚至更深,那么如何调用三级菜单level 3 sub categories呢?ytkah有一些思路可以参考一下。

opencart3调用三级菜单level 3 sub categories

  打开Catalog/controller/common/menu.php文件,修改成如下代码

代码语言:javascript
复制
<?php
class ControllerCommonMenu extends Controller {
    public function index() {
        $this->load->language('common/menu');
    // Menu
    $this-&gt;load-&gt;model(&#39;catalog/category&#39;);

    $this-&gt;load-&gt;model(&#39;catalog/product&#39;);

    $data[&#39;categories&#39;] = array();

    $categories = $this-&gt;model_catalog_category-&gt;getCategories(0);

    foreach ($categories as $category) {
        if ($category[&#39;top&#39;]) {
            // Level 2
            $children_data = array();

            $children = $this-&gt;model_catalog_category-&gt;getCategories($category[&#39;category_id&#39;]);

            foreach ($children as $child) {

                // Level 3
                $grandchildren_data = array();

                $grandchildren = $this-&gt;model_catalog_category-&gt;getCategories($child[&#39;category_id&#39;]);

                foreach ($grandchildren as $grandchild) {

                    $grandchild_filter_data = array(
                        &#39;filter_category_id&#39;  =&gt; $grandchild[&#39;category_id&#39;],
                        &#39;filter_sub_category&#39; =&gt; true
                    );

                    $grandchildren_data[] = array(
                        &#39;name&#39;  =&gt; $grandchild[&#39;name&#39;] . ($this-&gt;config-&gt;get(&#39;config_product_count&#39;) ? &#39; (&#39; . $this-&gt;model_catalog_product-&gt;getTotalProducts($grandchild_filter_data) . &#39;)&#39; : &#39;&#39;),
                        &#39;href&#39;  =&gt; $this-&gt;url-&gt;link(&#39;product/category&#39;, &#39;path=&#39; . $category[&#39;category_id&#39;] . &#39;_&#39; . $grandchild[&#39;category_id&#39;])
                    );
                }


                $filter_data = array(
                    &#39;filter_category_id&#39;  =&gt; $child[&#39;category_id&#39;],
                    &#39;filter_sub_category&#39; =&gt; true
                );

                $children_data[] = array(
                    &#39;name&#39;  =&gt; $child[&#39;name&#39;] . ($this-&gt;config-&gt;get(&#39;config_product_count&#39;) ? &#39; (&#39; . $this-&gt;model_catalog_product-&gt;getTotalProducts($filter_data) . &#39;)&#39; : &#39;&#39;),
                    &#39;href&#39;  =&gt; $this-&gt;url-&gt;link(&#39;product/category&#39;, &#39;path=&#39; . $category[&#39;category_id&#39;] . &#39;_&#39; . $child[&#39;category_id&#39;]),
                    &#39;children&#39; =&gt; $grandchildren_data,
                );
            }

            // Level 1
            $data[&#39;categories&#39;][] = array(
                &#39;name&#39;     =&gt; $category[&#39;name&#39;],
                &#39;children&#39; =&gt; $children_data,
                &#39;column&#39;   =&gt; $category[&#39;column&#39;] ? $category[&#39;column&#39;] : 1,
                &#39;href&#39;     =&gt; $this-&gt;url-&gt;link(&#39;product/category&#39;, &#39;path=&#39; . $category[&#39;category_id&#39;])
            );
        }
    }

    return $this-&gt;load-&gt;view(&#39;common/menu&#39;, $data);
}

}

  然后还要在前端调用catalog\view\theme\default\template\common\menu.twig

  需要代码的伙伴联系ytkah