Third level category menu opencart

This tutorial explains how to display third level category menu 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.

third level menu opencart

The main files that need to change are :

  1. Retrieve third level category menu – controller
  2. Template File

Third level category menu opencart

For retrieving the third level subcategory menu from the database edit the header controller file (catalog/controller/common/header.php).Open the header.php file and make the highlighted codes.

third_level_category_menu_opencart

Copy and paste the following code.

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

                );

            }

        }
        

Display thrid level subcatgory

This step edit you header.ptpl for displaying the menu in the header (catalog/view/theme/default/template/common/header.tpl ). Add the highlighted code.

display_third_level_subcatgeory

<?php foreach ($categories as $category) {  ?>
    <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
      <?php if ($category['children']) { ?>
      <div>
        <?php for ($i = 0; $i < count($category['children']);) { ?>
        <ul class="level2">
          <?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
          <?php for (; $i < $j; $i++) { ?>
          <?php if (isset($category['children'][$i]))
          {
            ?>
          <li><a href="<?php echo $category['children'][$i]['href']; ?>" class=""><?php echo $category['children'][$i]['name']; ?></a>
              <?php if (isset($category['children'][$i]['level3'])) {  
              $level3menus = $category['children'][$i]['level3'];
              ?>
              <ul class="level3">
              <?php
                    foreach( $level3menus as $level3menu) {
              ?>
                    <li><a href="<?php echo $level3menu['href']; ?>" class=""><?php echo $level3menu['name'];?></a></li>
              <?php } ?>
              </ul>
              <?php } ?>
          </li>
          <?php } ?>
          <?php } ?>
        </ul>
        <?php } ?>
      </div>
      <?php } ?>
    </li>
    <?php } ?>

You also create a xml file in the vqmod folder and paste the following changes if you do want to edit the core files.

If you have any further queries please leave a reply.

Thanks

Share this Tutorial

2 comments on “Third level category menu opencart

  1. Shabiey on

    hi, what if i want to add third-level-category in menu using opencart 3.0.2?? i cant find code that similar to this in header.php . I am so sorry, I am new using opencart.

    Reply
    • pearl on

      My code is based on opencart 2. But i am pity sure that you can find something similar in the controller/header file.

      Reply

Leave a Reply

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

*