3

Hi friends I am using Opencart 3.0.2.0 and would like to show the menu items in the 4th level of category (I have been able to search for and find solutions to show till the 3rd level on Stackoverflow).

You can see the .twig file here:

<div class="hidden-xs">
{% if categories %}
 <div class="cateti"> 
      <h3><i class="fa fa-list"></i>category</h3>
  </div>
<div class="cate-menu ">
  <nav id="menu" class="navbar">
    <div class="navbar-header"><span id="category" class="visible-xs">{{ text_category }}</span>
      <button type="button" class="btn btn-navbar navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"><i class="fa fa-bars"></i></button>
    </div>
    <div class="collapse navbar-collapse navbar-ex1-collapse">
      <ul class="nav">
        {% for category in categories %}
        {% if category.children %}
        <li class="dropdown"><a href="{{ category.href }}" class="dropdown-toggle header-menu" data-toggle="dropdown">{{ category.name }}<i class="fa fa-angle-down enangle"></i></a>
          <div class="dropdown-menu">
            <div class="dropdown-inner">
            {% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}
              <ul class="list-unstyled">
                {% for child in children %}
                      {% if child.grand_childs %}
                      <li class="dropdownj02"> <a href="{{ child.href }}" class="dropdown-toggle header-menu" data-toggle="dropdown"> {{ child.name }}<i class="fa fa-angle-down enangle"></i> </a>
                        <div class="dropdown-menuj02">
                          <div class="dropdown-inner">
                            <ul class="list-unstyled grand-child">
                              {% for grand_child in child.grand_childs %}
                                  <li> <a href="{{ grand_child.href }}"> {{grand_child.name}} </a> </li>
                              {% endfor %}
                            </ul>
                          </div>
                        </div>    
                      </li>
                      {% else %}
                      <li> <a href="{{ child.href }}"> {{ child.name }} </a></li>
                      {% endif %}
                    </li>
                {% endfor %}
              </ul>
              {% endfor %}</div>
            </div>
        </li>
        {% else %}
        <li><a href="{{ category.href }}">{{ category.name }}</a></li>
        {% endif %}
        {% endfor %}
      </ul>
    </div>
  </nav>
</div>
{% endif %}
</div>

<script type="text/javascript">
 function headermenu() {
     if (jQuery(window).width() < 992)
     {
         jQuery('ul.nav li.dropdown a.header-menu').attr("data-toggle","dropdown");        
     }
     else
     {
         jQuery('ul.nav li.dropdown a.header-menu').attr("data-toggle",""); 
     }
}
$(document).ready(function(){headermenu();});
jQuery(window).resize(function() {headermenu();});
jQuery(window).scroll(function() {headermenu();});
</script>

and the controller file here:

<?php
class ControllerExtensionModuleCategory extends Controller {
    public function index() {
        $this->load->language('extension/module/category');

        if (isset($this->request->get['path'])) {
            $parts = explode('_', (string)$this->request->get['path']);
        } else {
            $parts = array();
        }

        if (isset($parts[0])) {
            $data['category_id'] = $parts[0];
        } else {
            $data['category_id'] = 0;
        }

        if (isset($parts[1])) {
            $data['child_id'] = $parts[1];
        } else {
            $data['child_id'] = 0;
        }

        $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 
                    $children_data_3 = array();

                    $children_3 = $this->model_catalog_category->getCategories($child['category_id']);

                    foreach ($children_3 as $child_3) {

                            $filter_data_3 = array(
                                    'filter_category_id'  => $child_3['category_id'],
                                    'filter_sub_category' => true
                            );

                            $children_data_3[] = array(
                                    'name'  => $child_3['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data_3) . ')' : ''),
                                    'href'  => $this->url->link('product/category', 'path=' . $child['category_id'] . '_' . $child_3['category_id'])
                            );
                    }
                    //end of level 3                                          
                               
                    $filter_data = array(
                        'filter_category_id'  => $child['category_id'],
                        'filter_sub_category' => true
                    );

                    $children_data[] = array(
                        'thumb_menus' => HTTP_SERVER . 'image/' .$child['image'],
                        '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']),
                        'grand_childs' => $children_data_3//for level 3
                    );
                }

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

        return $this->load->view('extension/module/category', $data);
    }
}

You can see in the files that they have been modified to show till the 3rd level, now I want to show the 4th level as well.

Please help!

focus.style
  • 6,612
  • 4
  • 26
  • 38

1 Answers1

1

You have non standard template. But this will work. catalog/controller/extension/module/category.php

<?php
class ControllerExtensionModuleCategory extends Controller {
    public function index() {
        $this->load->language('extension/module/category');

        if (isset($this->request->get['path'])) {
            $parts = explode('_', (string)$this->request->get['path']);
        } else {
            $parts = array();
        }

        if (isset($parts[0])) {
            $data['category_id'] = $parts[0];
        } else {
            $data['category_id'] = 0;
        }

        if (isset($parts[1])) {
            $data['child_id'] = $parts[1];
        } else {
            $data['child_id'] = 0;
        }
        
        if (isset($parts[2])) {
            $data['grand_child_id'] = $parts[2];
        } else {
            $data['grand_child_id'] = 0;
        }
        
        if (isset($parts[3])) {
            $data['grand_child_2_id'] = $parts[3];
        } else {
            $data['grand_child_2_id'] = 0;
        }

        $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 
                    $children_data_3 = array();

                    $children_3 = $this->model_catalog_category->getCategories($child['category_id']);

                    foreach ($children_3 as $child_3) {
                        
                         // Level 4 
                         $children_data_4 = array();
    
                         $children_4 = $this->model_catalog_category->getCategories($child_3['category_id']);
    
                         foreach ($children_4 as $child_4) {
    
                                $filter_data_4 = array(
                                        'filter_category_id'  => $child_4['category_id'],
                                        'filter_sub_category' => true
                                );
    
                                $children_data_4[] = array(
                                        'name'  => $child_4['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data_4) . ')' : ''),
                                        'href'  => $this->url->link('product/category', 'path=' . $child_3['category_id'] . '_' . $child_4['category_id'])
                                );
                         }
                         //end of level 4                            
              
                
                            $filter_data_3 = array(
                                    'filter_category_id'  => $child_3['category_id'],
                                    'filter_sub_category' => true
                            );

                            $children_data_3[] = array(
                                    'name'  => $child_3['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data_3) . ')' : ''),
                                    'href'  => $this->url->link('product/category', 'path=' . $child['category_id'] . '_' . $child_3['category_id']),
                                    'grand_childs_2' => $children_data_4 //for level 4                                  
                            );                      
                    }
                    //end of level 3                                          
                               
                    $filter_data = array(
                        'filter_category_id'  => $child['category_id'],
                        'filter_sub_category' => true
                    );

                    $children_data[] = array(
                        'thumb_menus' => HTTP_SERVER . 'image/' .$child['image'],
                        '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']),
                        'grand_childs' => $children_data_3//for level 3
                    );  
                }

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

        return $this->load->view('extension/module/category', $data);
    }
}

catalog/view/theme/default/template/extension/module/category.twig

<div class="hidden-xs">
{% if categories %}
<div class="cateti"> 
  <h3><i class="fa fa-list"></i>category</h3>
</div>
<div class="cate-menu ">
  <nav id="menu" class="navbar">
    <div class="navbar-header"><span id="category" class="visible-xs">{{ text_category }}</span>
      <button type="button" class="btn btn-navbar navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"><i class="fa fa-bars"></i></button>
    </div>
    <div class="collapse navbar-collapse navbar-ex1-collapse">
      <ul class="nav">
        {% for category in categories %}
        {% if category.children %}
        <li class="dropdown"><a href="{{ category.href }}" class="dropdown-toggle header-menu" data-toggle="dropdown">{{ category.name }}<i class="fa fa-angle-down enangle"></i></a>
          <div class="dropdown-menu">
            <div class="dropdown-inner">
            {% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}
              <ul class="list-unstyled">
                {% for child in children %}
                {% if child.grand_childs %}
                <li class="dropdownj02"> <a href="{{ child.href }}" class="dropdown-toggle header-menu" data-toggle="dropdown"> {{ child.name }}<i class="fa fa-angle-down enangle"></i> </a>
                  <div class="dropdown-menuj02">
                    <div class="dropdown-inner">  
                      <ul class="list-unstyled grand-child">
                        {% for grand_child in child.grand_childs %}
                        {% if grand_child.grand_childs_2 %}
                        <li class="dropdownj03"><a href="{{ grand_child.href }}" class="dropdown-toggle header-menu" data-toggle="dropdown">{{ grand_child.name }}<i class="fa fa-angle-down enangle"></i> </a>
                          <div class="dropdown-menuj03">
                            <div class="dropdown-inner">
                              <ul class="list-unstyled grand-child_2">
                                {% for grand_child_2 in grand_child.grand_childs_2 %}
                                <li><a href="{{ grand_child_2.href }}"> {{grand_child_2.name}} </a> </li>
                                {% endfor %}
                              </ul>
                            </div>
                          </div>    
                        </li>
                        {% else %}
                        <li><a href="{{ grand_child.href }}">{{grand_child.name}}</a></li>
                        {% endif %}
                        {% endfor %}
                      </ul>
                    </div>
                  </div>    
                </li>
                {% else %}
                <li><a href="{{ child.href }}"> {{ child.name }} </a></li>
                {% endif %}
                {% endfor %}
              </ul>
              {% endfor %}</div>
            </div>
        </li>
        {% else %}
        <li><a href="{{ category.href }}">{{ category.name }}</a></li>
        {% endif %}
        {% endfor %}
      </ul>
    </div>
  </nav>
</div>
{% endif %}
</div>

<script type="text/javascript">
 function headermenu() {
     if (jQuery(window).width() < 992)
     {
         jQuery('ul.nav li.dropdown a.header-menu').attr("data-toggle","dropdown");        
     }
     else
     {
         jQuery('ul.nav li.dropdown a.header-menu').attr("data-toggle",""); 
     }
}
$(document).ready(function(){headermenu();});
jQuery(window).resize(function() {headermenu();});
jQuery(window).scroll(function() {headermenu();});
</script>
focus.style
  • 6,612
  • 4
  • 26
  • 38
  • Thanks! works perfectly! I appreciate your help!!!! – Jawad Shafiq Jun 22 '20 at 17:09
  • Isn't it possible to make a controller and twig system that will cater to unlimited category levels automatically, once and for all ? – Jawad Shafiq Jun 23 '20 at 01:36
  • Controller - may be, Twig - no. But i haven't seen a situation yet, when you may need more then 5 levels. Here, a nice 5-level menu for you https://www.dropbox.com/s/zi1z8h48r80rttr/Yomenu_2.0.zip?dl=0 – focus.style Jun 23 '20 at 07:38