在opencart 2中获得第三级类别 [英] get third level category in opencart 2

查看:88
本文介绍了在opencart 2中获得第三级类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为2.0.x的购物车打开了Im编辑模板,发现顶部的菜单一-仅显示第二个子类别. 而且我一直在查找控制器文件,模板文件以根据需要对其进行编辑-仅显示一个级别(或所有级别..只要我能获得更深层次的子类别的猫..)在左侧的导航上---持续了数周.并且找不到路.

Im editing template for 2.0.x open cart, and found out that the Menu one the top side - shows only upto second sub category.. and i'v been looking up controller files, template files, to edit it as I want - show just one more level (or all levels..as long as I can get more deeper level of sub sub cats..) of sub categories on the navigation on the left --- for couple weeks..and could not find the way.

header.tpl

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 //以下内容用于启用三级子类别

controller - header.php //below is written to enable 3rd level sub-categories

      $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版上进行了测试.

You need to adjust some css code to look it good. It is tested on OpenCart version 2.0.1.1.

这篇关于在opencart 2中获得第三级类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆