get third level category in opencart 2

泄露秘密 提交于 2019-12-08 12:58:12

问题


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

<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>

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

can anyone help this out please?


回答1:


controller file

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

In view i.e tpl file

<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>

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



来源:https://stackoverflow.com/questions/35803329/get-third-level-category-in-opencart-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!