Php Opencart 3.x如何在菜单中手动添加第四级类别项目

Php Opencart 3.x如何在菜单中手动添加第四级类别项目,php,twig,opencart,opencart-3,Php,Twig,Opencart,Opencart 3,嗨,朋友们,我正在使用Opencart 3.0.2.0,并希望显示类别第4级的菜单项(我已经能够搜索并找到解决方案,直到Stackoverflow的第3级) 您可以在此处看到.twig文件: <div class="hidden-xs"> {% if categories %} <div class="cateti"> <h3><i class="fa fa-list">&

嗨,朋友们,我正在使用Opencart 3.0.2.0,并希望显示类别第4级的菜单项(我已经能够搜索并找到解决方案,直到Stackoverflow的第3级)

您可以在此处看到.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 %}
                                  <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>
<?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);
    }
}

{%if类别%}
类别
{{text_category}}
    {categories%%中的类别为%s} {%if category.children%}
  • {category.children | batch(category.children | length/category.column | round(1,'ceil'))%}
      {children%%中的children的百分比} {%if child.grand_childs%}
      • {child.grand_childs%中grand_child的百分比]
      • {%endfor%}
    • {%else%}
    • {%endif%} {%endfor%}
    {%endfor%}
  • {%else%}
  • {%endif%} {%endfor%}
{%endif%} 函数头菜单(){ if(jQuery(window).width()<992) { jQuery('ul.nav li.dropdown a.header-menu').attr(“数据切换”,“下拉”); } 其他的 { jQuery('ul.nav li.dropdown a.header-menu').attr(“数据切换”); } } $(document).ready(函数(){headermenu();}); jQuery(窗口).resize(函数(){headermenu();}); jQuery(窗口).scroll(函数(){headermenu();});
以及此处的控制器文件:

<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>
<?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);
    }
}

您有非标准模板。但这是可行的。
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);
    }
}

谢谢!很好用!我感谢你的帮助!!!!难道不可能制作一个控制器和细枝系统来自动、一劳永逸地满足无限类别级别的需求吗?控制器-可能是,细枝-不。但我还没有看到这样的情况,当你需要超过5个级别时。给你一份五级菜单