Subcategory menu opencart

This tutorial explains how to display subcategory menu in opencart. For displaying the third level menus in the opencart categories we need to make some changes to the core file.

This section explains what are the changes we need to make for displaying the third level subcategories as shown below.

Please click for step by step explanation

catalog/controller/common/header.php

For displaying third level categories edit the header controller file and header template file

foreach ($categories as $category) {
            if ($category['top']) {
                $children_data = array();
                $children = $this->model_catalog_category->getCategories($category['category_id']);

                foreach ($children as $child) {
                    $sec_children_data = array();
                    $sec_children = $this->model_catalog_category->getCategories($child['category_id']);

                    foreach ($sec_children as $sec_child) {
                        $sec_children_data[] = array(
                            'name'  => $sec_child['name'] . ($this->config->get('config_product_count') ? '' : ''),
                            'href'  => $this->url->link('product/category', 'path=' . $child['category_id'] . '_' . $sec_child['category_id'])
                        );

                    }
                    $data = array(
                        'filter_category_id'  => $child['category_id'],
                        'filter_sub_category' => true
                    );
                    $product_total = $this->model_catalog_product->getTotalProducts($data);

                    $children_data[] = array(
                        'name'  => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
                        'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
                        'level3' => $sec_children_data
                    );
                }

                // Level 1

                $this->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'])

                );

            }

        }
        

Share this Tutorial