OpenCart 3.0.2.0 3级子类别

时间:2017-12-01 18:04:09

标签: php opencart opencart-3

我正在尝试在类别菜单上创建一个lvl 3子菜单,但到目前为止还没有成功。

我将以下代码添加到foreach循环lvl2

中的menu.php文件中

目录/ controller / common / menu.php文件

// 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'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($grandchild_filter_data) . ')' : ''),
                  'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $grandchild['category_id'])
                );
         }

在menu.twig文件上 我在li>之后在for循环中添加了以下代码。

  {% if child.children %}
     <ul>
       {% for children in child.children %}
        <li><a href="{{ children.href }}">{{ children.name }}</a></li>
       {% endfor %}
    </ul>
  {% endif %}

有人建议吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

<强>控制器: 目录\控制器\共同\ menu.php

Parent

查看: 目录\视图\主题\默认\模板\共同\ menu.twig

<?php
class ControllerCommonMenu extends Controller {
    public function index() {
        $this->load->language('common/menu');

        // 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
                    $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'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($grandchild_filter_data) . ')' : ''),
                            'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $grandchild['category_id'])
                        );
                    }


                    $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']),
                        'children' => $grandchildren_data,
                    );
                }

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

最后,您可能需要清除缓存,twig缓存,ocmod缓存等...