Add sub sub categories in opencart

Sometimes we need to add sub sub categories in opencart header with the product images. One of my client wanted a similar layout, I did it with the following code.

Add the following code to header controller.

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
$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) {
 
/*Incase level 3 not available*/
 
$level_5_data = array();
 
$categories_5 = $this->model_catalog_product->getProducts($data = array('filter_category_id' => $category_2['category_id']));
 
foreach ($categories_5 as $category_5 ) {
 
$level_5_data [] = array(
'name' => $category_5['name'],
'image' => $server . 'image/' . $category_5['image'],
'href' => $this->url->link('product/product', 'product_id=' . $category_5['product_id']),
);
 
}
/*Incase level 3 not available*/
$level_3_data = array();
 
$categories_3 = $this->model_catalog_category->getCategories($category_2['category_id']);
 
foreach ($categories_3 as $category_3) {
// Custum code for for products by Diwakar Mishra (Appealsoft)
$level_4_data = array();
 
$categories_4 = $this->model_catalog_product->getProducts($data = array('filter_category_id' => $category_3['category_id']));
 
foreach ($categories_4 as $category_4 ) {
 
$level_4_data [] = array(
'name' => $category_4['name'],
'image' => $server . 'image/' . $category_4['image'],
'href' => $this->url->link('product/product', 'product_id=' . $category_4['product_id']),
);
}
// Custum code for for products by Diwakar Mishra (Appealsoft)
$level_3_data[] = array(
'name' => $category_3['name'],
'children' => $level_4_data,
'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,
'children_else' => $level_5_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'])
);
}

you will need to change the header.tpl file and create a loop of the above arrays to get the final result.

Leave a Reply

Your email address will not be published. Required fields are marked *