在菜单中以OpenCart 2.0.x的形式制作类别图像

时间:2015-05-06 15:06:25

标签: image opencart2.x

我很难让这成为可能。我要做的是,我想在菜单中将类别图像作为Opencart 2.0.x中的拇指。

我把'thumb'=> $ thumb,目录/ controller / common / header.tpl

像这样:

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

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

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

    $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) {
                $filter_data = array(
                    'filter_category_id'  => $child['category_id'],
                    'filter_sub_category' => true
                );

                $children_data[] = array(
                    '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

        $this->load->model('tool/image');
        $image = empty($category['image']) ? 'no_image.jpg' : $category['image'];
        $thumb = $this->model_tool_image->resize($image, 100, 100);

            $data['categories'][] = array(
                'name'     => $category['name'],
                'children' => $children_data,
                'thumb'    => $thumb,
                'column'   => $category['column'] ? $category['column'] : 1,
                'href'     => $this->url->link('product/category', 'path=' . $category['category_id'])
            );
        }
    }

我还将$ category ['thumb']放在catalog / view / theme / default / template / common / header.tpl

像这样:

                <?php if ($categories) { ?>
            <nav id="navigation">
                <div class="boxed bg-navigation">
                    <div class="container">
                        <ul data-breakpoint="800" class="flexnav with-js lg-screen clearfix">
                            <?php foreach ($categories as $category) { ?>
                            <?php if ($category['children']) { ?>
                            <li><a href="<?php echo $category['href']; ?>" ><?php echo $category['name']; ?></a>
                                <ul class="list-unstyled ul-col-<?php echo $category['column']; ?>">
                                    <?php foreach (array_chunk($category['children'], ceil(count($category['children']) / $category['column'])) as $children) { ?>
                                    <?php if ($category['column'] == 1) { ?> 
                                    <?php foreach ($children as $child) { ?>
                                    <li><a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a></li>
                                    <?php } ?>
                                    <?php } else { ?>
                                    <li class="li-inline">
                                        <div>
                                            <?php foreach ($children as $child) { ?>
                                            <?php $category['thumb']?>
                                            <a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a>
                                            <?php } ?>
                                        </div>
                                    </li>
                                    <?php } ?>
                                    <?php } ?>
                                </ul>
                            </li>
                            <?php } else { ?>
                            <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></li>
                            <?php } ?>
                            <?php } ?>
                        </ul>

                    </div>
                </div>
            </nav>
            <?php } ?>

我在这里做错了什么?我在Stack Overflow上发现了这个解决方案Solution

但我似乎无法正确解决这个问题,这可能是一个我无法真正看到的简单事情。我之前在OpenCart 1.5.6.4中使用了另一个框架。但这也没有帮助我很多嘿嘿。如果你想我可以粘贴其他代码。

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,并且正在使用Opencart 2.1.0.1 这是catalog / controller / common / header.php文件:

(line 113)
// Level 2
            $children_data = array();

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

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

                $this->load->model('tool/image');
                $image = empty($child['image']) ? 'no_image.png' : $child['image'];
                $thumb = $this->model_tool_image->resize($image, 100, 100);

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

这是我的目录/视图/主题/默认/模板/ common / header.tpl文件:

(line 108)
<li><a href="<?php echo $child['href']; ?>"><img src="<?php echo $child['thumb']?>" alt="<?php echo $child['name']; ?>"/><br /><?php echo $child['name']; ?></a></li>

希望这有帮助。

答案 1 :(得分:1)

这是因为$category['thumb']仅包含图像源,因此在模板文件中

替换<?php $category['thumb']?>

使用<img src="<?php echo $category['thumb']?>" alt="<?php echo $child['name']; ?>"/>

另外在控制器中你设置父类别的拇指和儿童类别的模板打印,所以它会一次又一次地打印相同的父图像。

no_image.jpg在最新版本中被替换为no_image.png