每个类别/子类别的Opencart 2.2.0仅显示类别模块中的第三级类别

时间:2017-06-08 08:30:08

标签: php module opencart categories

这可能是重复的,但我找到的答案,并没有解决我的问题。我有这样的类别结构:Category-> Subcategory-> Sub sub category。在我的类别模块中,我设法显示了整个结构(Category-> Subcategory-> Subsub类别)。但我需要的只是类别,以显示每个类别或子类别。我的category.php模块文件如下所示:

<?php
class ControllerModuleCategory extends Controller {
    public function index() {
        $this->load->language('module/category');

        $data['heading_title'] = $this->language->get('heading_title');

        if (isset($this->request->get['path'])) {
            $parts = explode('_', (string)$this->request->get['path']);
        } else {
            $parts = array();
        }

        if (isset($parts[0])) {
            $data['category_id'] = $parts[0];
        } else {
            $data['category_id'] = 0;
        }

        if (isset($parts[1])) {
            $data['child_id'] = $parts[1];
        } else {
            $data['child_id'] = 0;
        }

        $this->load->model('catalog/category');

        $this->load->model('catalog/product');

        $data['categories'] = array();

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

        foreach ($categories as $category) {
            $children_data = array();

            if ($category['category_id'] == $data['category_id']) {
                $children = $this->model_catalog_category->getCategories($category['category_id']);

                foreach($children as $child) {
                    $children_data_2 = array();

                    $children_2 = $this->model_catalog_category->getCategories($child['category_id']);

                    foreach ($children_2 as $child_2) {
    $filter_data = array(
        'filter_category_id'  => $child_2['category_id'],
        'filter_sub_category' => true
    );

    $children_data_2[] = array(
        'category_id' => $child_2['category_id'],
        'name'        => $child_2['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'] . '_' . $child_2['category_id'])
    );
}

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

                    $children_data[] = array(
                        'category_id' => $child['category_id'],
                        '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']),
                            'children'    => $children_data_2 // insert this line

                    );
                }
            }

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

            $data['categories'][] = array(
                'category_id' => $category['category_id'],
                'name'        => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
                'children'    => $children_data,
                'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
            );
        }

        return $this->load->view('module/category', $data);
    }
}

我的category.tpl模块文件如下所示:

<h3><?php echo "Potkategorije" ?></h3>
<div class="list-group">
<ul>
    <?php foreach ($categories as $category) { ?>
    <?php if ($category['category_id'] == $category_id) { ?>
    <li class="cat-active">
        <a href="<?php echo $category['href']; ?>" class="active"><?php echo $category['name']; ?></a>
    <?php } else { ?>
    <li>
        <a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
        <?php } ?>
        <?php if ($category['children']) { ?>
        <b class="cc"></b>
        <ul class="col-subcat">
            <?php foreach ($category['children'] as $child) { ?>
            <li>
                <?php if ($child['category_id'] == $child_id) { ?>
                <a href="<?php echo $child['href']; ?>" class="active"><?php echo $child['name']; ?></a>
                <?php } else { ?>
                <a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a>
                <?php } ?>
                <?php if ($child['children']) { ?>
                <b class="cc"></b>
                <ul class="col-subcat">
                    <?php foreach ($child['children'] as $child_2) { ?>
                    <li>
                        <?php if ($child_2['category_id'] == $child_id) { ?>
                        <a href="<?php echo $child_2['href']; ?>" class="active"><?php echo $child_2['name']; ?></a>
                        <?php } else { ?>
                        <a href="<?php echo $child_2['href']; ?>"><?php echo $child_2['name']; ?></a>
                        <?php } ?>
                    </li>
                    <?php } ?>
                </ul>
                <?php } ?>
            </li>
            <?php } ?>
        </ul>
        <?php } ?>
    </li>
    <?php } ?>
</ul>
</div>

如何修改文件以使其工作?感谢你的时间,感谢你。

2 个答案:

答案 0 :(得分:1)

Thanx对一个好男人@Ahmed Ginani试图帮助我,我重新解决了我的问题。我从未定义过当前的子类别。相反,我继续一次展示整个结构。因此,我必须修改我的两个文件(php和tpl),以便只显示当前类别/子类别的子类别。我在这里显示正确的文件。

  

Category.php:

<?php
class ControllerModuleCategory extends Controller {
    public function index() {
        $this->load->language('module/category');

        $data['heading_title'] = $this->language->get('heading_title');

        if (isset($this->request->get['path'])) {
            $parts = explode('_', (string)$this->request->get['path']);
        } else {
            $parts = array();
        }

        if (isset($parts[0])) {
            $data['category_id'] = $parts[0];
        } else {
            $data['category_id'] = 0;
        }

        if (isset($parts[1])) {
            $data['child_id'] = $parts[1];
        } else {
            $data['child_id'] = 0;
        }



        $this->load->model('catalog/category');

        $this->load->model('catalog/product');

        $data['categories'] = array();

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

        foreach ($categories as $category) {
            $children_data = array();

            if ($category['category_id'] == $data['category_id']) {
                $children = $this->model_catalog_category->getCategories($category['category_id']);

                foreach($children as $child) {
                    $children_data_2 = array();

                    $children_2 = $this->model_catalog_category->getCategories($child['category_id']);

                    foreach ($children_2 as $child_2) {
    $filter_data = array(
        'filter_category_id'  => $child_2['category_id'],
        'filter_sub_category' =>  true
    );

    $children_data_2[] = array(
        'category_id' => $child_2['category_id'],
        'name'        => $child_2['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'] . '_' . $child_2['category_id']),

    );
}

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

                    $children_data[] = array(
                        'category_id' => $child['category_id'],
                        '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']),
                        'children'    => $children_data_2 // insert this line


                    );
                }
            }

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

            $data['categories'][] = array(
                'category_id' => $category['category_id'],
                'name'        => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
                'children'    => $children_data,
                'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
            );
        }

        return $this->load->view('module/category', $data);
    }
}
  

Category.tpl:

<div class="list-group">
 <ul id="menu">
    <?php foreach ($categories as $category) : 

        if (!empty($category['children'])) : 
        echo '<h3 style="margin-top:54px;">Potkategorije</h3>';
            echo '<ul>';
            foreach ($category['children'] as $category_level2) :
                if (!empty($category_level2['children']) && $category_level2['category_id'] == $child_id) :
                    echo '<ul>';
                    foreach ($category_level2['children'] as $category_level3) :
                        echo '<li style="list-style-type: none"><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>';
    ?>
</div>

我真的希望这可以帮助别人,欢呼!

答案 1 :(得分:0)

我认为你在变量比较中存在问题:

<?php foreach ($child['children'] as $child_2) { ?>
<li>
<?php if ($child_2['category_id'] == $child_id) { ?>

 <?php if ($child_2['category_id'] == $child_2_id) { ?>