opencart3调用三级菜单level 3 sub categories

  Opencart 3的menu菜单默认只调用一级和二级菜单,但很多电商网站类目复杂,三级菜单一般都是需要的,甚至更深,那么如何调用三级菜单level 3 sub categories呢?ytkah有一些思路可以参考一下。

opencart3调用三级菜单level 3 sub categories

  打开Catalog/controller/common/menu.php文件,修改成如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
class ControllerCommonMenu extends Controller {
    public function index() {
        $this->load->language('common/menu');
 
        // Menu
        $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
                    $grandchildren_data = array();
 
                    $grandchildren = $this->model_catalog_category->getCategories($child['category_id']);
 
                    foreach ($grandchildren as $grandchild) {
 
                        $grandchild_filter_data = array(
                            'filter_category_id'  => $grandchild['category_id'],
                            'filter_sub_category' => true
                        );
 
                        $grandchildren_data[] = array(
                            'name'  => $grandchild['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($grandchild_filter_data) . ')' : ''),
                            'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $grandchild['category_id'])
                        );
                    }
 
 
                    $filter_data = array(
                        'filter_category_id'  => $child['category_id'],
                        'filter_sub_category' => true
                    );
 
                    $children_data[] = array(
                        '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']),
                        'children' => $grandchildren_data,
                    );
                }
 
                // 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'])
                );
            }
        }
 
        return $this->load->view('common/menu', $data);
    }
}

  然后还要在前端调用catalog\view\theme\default\template\common\menu.twig

  需要代码的伙伴联系ytkah

posted @   ytkah  阅读(430)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2015-04-02 微信在公众号增开了新广告位 这次是在图文消息头部
网址导航 gg N / G Sitemap

部分内容来源于网络,如有版权问题请联系删除

  
点击右上角即可分享
微信分享提示