user1289939
user1289939

Reputation:

get third level category in opencart

I want to foreach third level in OpenCart category module, here is code which only generates 2 level category, please help and modify so that it will genarate third level:

<ul id="menu">
    <?php foreach ($categories as $category) { ?>
    <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
      <?php if ($category['children']) { ?>
        <?php for ($i = 0; $i < count($category['children']);) { ?>
        <ul>
          <?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']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
          <?php } ?>
          <?php } ?>
        </ul>
        <?php } ?>
      <?php } ?>
    </li>
    <?php } ?>
  </ul>

Upvotes: 1

Views: 10467

Answers (5)

Liz Eipe C
Liz Eipe C

Reputation: 255

For editing the third level menu please make the following changes in the header.php controller file.

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

                }

And also make the following changes to the header.tpl file.

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

Upvotes: 0

Faysal Mahmud
Faysal Mahmud

Reputation: 1

First download vqmod from here then extract it.Now vqmod folder keep in your site root directory. Then go to browser write your site url then "/vqmod/install" and press Enter then you get a message that you can install vqmod in your site successfully. Now you download a extension form here and extract it. and keep extract file in your site indicate foulder exm: menu3rdlevel-opencart-2_2\vqmod\xml/Menu3rdLevel.xml file in your site eg : vqmod\xml/Menu3rdLevel.xml them other file with "menu3rdlevel" folder.From your extension folder "javascript" to your site folder "javescript", extension folder "image" to site folder "image" extension folder "stylsheet" to site folder "stylsheet". then refresh your site in browser its ok now.

NB: Transfer xml file only file and other file with folder.

Upvotes: 0

Nicolas Connault
Nicolas Connault

Reputation: 464

You could try this, it's not elegant but should work:

    <ul id="menu">
    <?php foreach ($categories as $category) : 
        echo '<li><a href="'.$category['href'].'">'.$category['name'].'</a>';
        if (!empty($category['children'])) : 
            echo '<ul>';
            foreach ($category['children'] as $category_level2) :
                echo '<li><a href="'.$category_level2['href'].'">'.$category_level2['name'].'</a>';
                if (!empty($category_level2['children'])) :
                    echo '<ul>';
                    foreach ($category_level2['children'] as $category_level3) :
                        echo '<li><a href="'.$category_level3['href'].'">'.$category_level3['name'].'</a></li>';
                    endforeach;
                    echo '</ul>';
                endif;
                echo '</li>';
            endforeach;
            echo '</ul>';
        endif;
        echo '</li>';
    endforeach;
    echo '</ul>';
    ?>

Upvotes: 2

Prasath Albert
Prasath Albert

Reputation: 1457

For this first you need to edit the Header Controller:

Go to Catalog->controller->common->header.php

Edit the section that create the $categories variable. update by following script:

$categories = $this->model_catalog_category->getCategories(0);

        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') ? ' (' . $product_total . ')' : ''),
                        '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']), 'children' => $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'])
                );
            }
        }

Then Update the view file for displaying the third level categories.

Upvotes: 7

Wojciech Rutkowski
Wojciech Rutkowski

Reputation: 11389

Supposing you have administration side already done then you should check whether there are some categories under 2nd level. If yes, then make for construction (or foreach) and display them as 3rd level.

You will generate triply <ul></ul> construction. It should be stylized under CSS.

You can even make infinity subcategories using recursive function.

Let me know if you don't get it.

Upvotes: 0

Related Questions