Opencart子类别

时间:2016-06-28 17:42:26

标签: php html menu opencart submenu

我试图建立一个opencart商店,但我遇到了一个问题,这个问题开始变得很烦人。

所以我想做的是将左侧菜单设置为子类别页面。那很顺利。

但是,当我想让菜单显示子子类别时,它不起作用。

我当然要尝试:

foreach ($categories as $category)
{
}

但是,当我尝试$ category [' children']时,它无法正常工作。

我确定这是因为这些是2级类别,但有没有办法可以做到这一点?

提前谢谢

1 个答案:

答案 0 :(得分:0)

控制器文件

$categories = $this->model_catalog_category->getCategories(0);
    foreach ($categories as $category) {
        if ($category['top']) {
            // Level 2
            $children_data = array();
            $children = $this->model_catalog_category->getCategories($category['category_id']);
            foreach ($children as $child) {
              // Level 3
                $children_data2 = array();
                $children2 = $this->model_catalog_category->getCategories($child['category_id']);
                foreach ($children2 as $child2) {
                    $children_data2[] = array(
                    'name'  => $child2['name'],
                    'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'].'_'.$child2['category_id'])
                    );
                }

                $filter_data = array(
                    'filter_category_id'  => $child['category_id'],
                    'filter_sub_category' => true
                );

                $children_data[] = array(
                    'children'=>$children_data2,
                    'name'  => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
                    'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
                );
            }
            // Level 1
            $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'])
            );
        }
    }

在视图中,即tpl文件

<ul class="nav navbar-nav">
    <?php foreach ($categories as $category) { ?>
    <?php if ($category['children']) { ?>
    <li class="dropdown"><a href="<?php echo $category['href']; ?>" class="dropdown-toggle" data-toggle="dropdown"><?php echo $category['name']; ?></a>
      <div class="dropdown-menu">
        <div class="dropdown-inner">
          <?php foreach (array_chunk($category['children'], ceil(count($category['children']) / $category['column'])) as $children) { ?>
          <ul class="list-unstyled">
            // second level child             
            <?php foreach ($children as $child) { ?>
            <li><a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a></li>
            // third level child 
            <?php if($child['children']) { ?>
            <?php foreach($child['children'] as $child2) { ?>
            <ul>
            <li><a href="<?php echo $child2['href']; ?>"><?php  echo $child2['name']; ?></a></li>
            </ul>
            <?php } } ?>
            <?php } ?>
          </ul>
          <?php } ?>
        </div>
        <a href="<?php echo $category['href']; ?>" class="see-all"><?php echo $text_all; ?> <?php echo $category['name']; ?></a> </div>
    </li>
    <?php } else { ?>
    <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></li>
    <?php } ?>
    <?php } ?>
  </ul>

你需要调整一些css代码才能看起来很好。它在OpenCart版本2.0.1.1上进行了测试。