D
D
DeniSidorenko2020-04-25 08:00:24
opencart
DeniSidorenko, 2020-04-25 08:00:24

How to display all child categories in header.tpl?

there is a structure like this

Catalog -> And categories that are child, nested, etc.

When displaying $categories, I only show children for the 1st level (ie for the directory) And children for the current children are not. How can I fix and display children for child directories ( 3rd level )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2020-04-25
@DeniSidorenko

The module is part of Opencart.pro (free version;) ) - you can download - and see in more detail, but in general - something like this:
Controller

$data['categories'] = array();

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

    foreach ($categories as $category) {

      $children_data = array();

      $children = $this->model_catalog_category->getCategories($category['category_id']);

      foreach ($children as $child) {
      
      $children2_data = array();
      $children2 = $this->model_catalog_category->getCategories($child['category_id']);
      
        foreach ($children2 as $child2) {
        
          $children3_data = array();
          $children3 = $this->model_catalog_category->getCategories($child2['category_id']);
          
            foreach ($children3 as $child3) {
              
              $filter_data3 = array(
                'filter_category_id'  => $child3['category_id'],
              );				

              $children3_data[] = array(
                'category_id' => $child3['category_id'],
                'name'        => $child3['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data3) . ')' : ''),
                'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']. '_' . $child2['category_id']. '_' . $child3['category_id'])	
              );		
            
            
            }
        
          $filter_data2 = array(
            'filter_category_id'  => $child2['category_id'],
          );				

          $children2_data[] = array(
            'category_id' => $child2['category_id'],
            'name'        => $child2['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data2) . ')' : ''),
            'children3'    => $children3_data,
            'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']. '_' . $child2['category_id'])	
          );		
        
        
        }
        
        $filter_data1 = array(
            'filter_category_id'  => $child['category_id'],
          );	

        $children_data[] = array(
          'category_id' => $child['category_id'],
          'name'        => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data1) . ')' : ''),
          'children2'    => $children2_data,
          'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])	
        );		
      }
      
      $filter_data = array(
        'filter_category_id'  => $category['category_id'],
      );

      $data['categories'][] = array(
        'category_id' => $category['category_id'],
        'name'        => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
        'children'    => $children_data,
        'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
      );	
    }

Template Code
<div class="list-group">
      <?php foreach ($categories as $category) { ?>
        <?php if ($category['category_id'] == $category_id) { ?>
        <a href="<?php echo $category['href']; ?>" class="list-group-item active"><?php echo $category['name']; ?></a>
        <?php } else { ?>
        <a href="<?php echo $category['href']; ?>" class="list-group-item"><?php echo $category['name']; ?></a>
        <?php } ?>
        <?php if (($category['children']) && ($category['category_id'] == $category_id)) { ?>
          <?php foreach ($category['children'] as $child) { ?>
            <?php if ($child['category_id'] == $child_id) { ?>
            <a href="<?php echo $child['href']; ?>" class="list-group-item active">&nbsp;- <?php echo $child['name']; ?></a>
            <?php } else { ?>
            <a href="<?php echo $child['href']; ?>" class="list-group-item">&nbsp;- <?php echo $child['name']; ?></a>
            <?php } ?>
              <?php if (($child['children2']) &&  ($category['category_id'] == $category_id)) { ?>
              <?php foreach ($child['children2'] as $child2) { ?>
              <?php if ($child2['category_id'] == $child_id2) { ?>
              <a href="<?php echo $child2['href']; ?>" class="list-group-item active">&nbsp;&nbsp;&nbsp;- <?php echo $child2['name']; ?></a>
              <?php } else { ?>
              <a href="<?php echo $child2['href']; ?>" class="list-group-item">&nbsp;&nbsp;&nbsp;- <?php echo $child2['name']; ?></a>
              <?php } ?>
                <?php if (($child2['children3']) &&  ($category['category_id'] == $category_id)) { ?>
                    <?php foreach ($child2['children3'] as $child3) { ?>
                    <?php if ($child3['category_id'] == $child_id3) { ?>
                    <a href="<?php echo $child3['href']; ?>" class="list-group-item active">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <?php echo $child3['name']; ?></a>
                    <?php } else { ?>
                    <a href="<?php echo $child3['href']; ?>" class="list-group-item">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <?php echo $child3['name']; ?></a>
                    <?php } ?>
                    <?php } ?>
                 <?php } ?>
              <?php } ?>
           <?php } ?>
          <?php } ?>
        <?php } ?>
      <?php } ?>
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question