Search code examples
phpopencart

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>

Solution

  • 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>';
        ?>