在opencart 2中获得第三级类别

get third level category in opencart 2

我正在编辑 2 的模板。0.x 打开购物车,发现顶部的菜单 - 仅显示第二个子类别.. 我一直在查找控制器文件、模板文件,根据需要对其进行编辑 - 只显示一个级别(或所有级别..只要我可以获得更深层次的 sub sub cats..)子类别在左边的导航上 --- 几个星期..找不到路。

header.tpl

<ul>
                            <?php foreach ($categories as $category_1) { ?>
                            <li class="sub-menu"><a href="<?php echo $category_1['href']; ?>"><div><?php echo $category_1['name']; ?></div></a>
                                <?php if ($category_1['children']) { ?>
                                <div class="mega-menu-content style-2 col-4 clearfix">
                                <?php foreach($category_1['children'] as $category_2) { ?>
                                    <ul id="m2">
                                        <li class="mega-menu-title"><a href="<?php echo $category_2['href']; ?>"><div><?php echo $category_2['name']; ?></div></a>
                                        <?php if ($category_2['children']) { ?>
                                            <ul id="m3">
                                                <?php foreach($category_2['children'] as $category_3) { ?>
                                                <li><a href="<?php echo $category_3['href']; ?>"><div><?php echo $category_3['name']; ?></div></a></li>
                                                <?php } ?>
                                            </ul>
                                        <?php } ?>  
                                        </li>
                                    </ul>
                                    <?php } ?>
                                </div>
                                <?php } ?>
                            </li><!-- .mega-menu end -->
                            <?php } ?>
                        </ul>

控制器 - header.php //下面写的是启用第3级子类别

      $categories_1 = $this->model_catalog_category->getCategories(0);

      foreach ($categories_1 as $category_1) {
         $level_2_data = array();

         $categories_2 = $this->model_catalog_category->getCategories($category_1['category_id']);

         foreach ($categories_2 as $category_2) {
            $level_3_data = array();

            $categories_3 = $this->model_catalog_category->getCategories($category_2['category_id']);

            foreach ($categories_3 as $category_3) {
               $level_3_data[] = array(
                  'name' => $category_3['name'],
                                       'column'   => $category_3['column'] ? $category_3['column'] : 1,
                  'href' => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'] . '_' . $category_3['category_id'])
               );
            }

            $level_2_data[] = array(
               'name'     => $category_2['name'],
               'children' => $level_3_data,
               'href'     => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'])   
            );               
         }

         $this->data['categories'][] = array(
            'name'     => $category_1['name'],
            'children' => $level_2_data,
            'column'   => $category_1['column'] ? $category_1['column'] : 1,
            'href'     => $this->url->link('product/category', 'path=' . $category_1['category_id'])
         );
      }

// End of the written addition

有人可以帮忙解决这个问题吗?

控制器文件

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

在视图中,即 tpl 文件

<ul class="nav navbar-nav">
    <?php foreach ($categories as $category) { ?>
    <?php if ($category['children']) { ?>
    <li class="dropdown"><a href="<?php echo $category['href']; ?>" class="dropdown-toggle" data-toggle="dropdown"><?php echo $category['name']; ?></a>
      <div class="dropdown-menu">
        <div class="dropdown-inner">
          <?php foreach (array_chunk($category['children'], ceil(count($category['children']) / $category['column'])) as $children) { ?>
          <ul class="list-unstyled">
            <?php foreach ($children as $child) { ?>
            <li><a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a></li>
            <?php if($child['children']) { ?>
            <?php foreach($child['children'] as $child2) { ?>
            <ul>
            <li><a href="<?php echo $child2['href']; ?>"><?php  echo $child2['name']; ?></a></li>
            </ul>
            <?php } } ?>
            <?php } ?>
          </ul>
          <?php } ?>
        </div>
        <a href="<?php echo $category['href']; ?>" class="see-all"><?php echo $text_all; ?> <?php echo $category['name']; ?></a> </div>
    </li>
    <?php } else { ?>
    <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></li>
    <?php } ?>
    <?php } ?>
  </ul>

您需要调整一些 css 代码以使其看起来不错。 它在 OpenCart 版本 2.0.1.1 上进行了测试。