OpenCart 2.2.0.0中类别和产品页面的特定模板

时间:2016-11-23 03:35:42

标签: opencart opencart2.x

我正在使用OpenCart 2.2.0.0版,并尝试为每个类别和产品页面设置不同的模板。在线搜索我发现以下代码:

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/category_' . $category_id . '.tpl')) {
    $this->template = $this->config->get('config_template') . '/template/product/category_' . $category_id . '.tpl';
} elseif (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/category.tpl')) {
    $this->template = $this->config->get('config_template') . '/template/product/category.tpl';
} else {
    $this->template = 'default/template/product/category.tpl';
}

此代码适用于较旧版本的OpenCart但在新版本中我没有在catalog / controller / product / category.php文件中找到类似的代码结构

如何在OpenCart 2.2.0.0中实现类似的结果?

2 个答案:

答案 0 :(得分:1)

由于Opencart将其方法从2.2更改为代码不再起作用,您可以像这样修改它:

首先,我们必须知道哪个主题是活动的,将其名称存储在变量

$config_theme = $this->config->get('config_theme') == 'theme_default' ? 'default' : $this->config->get('config_theme');

然后我们必须检查是否有专门针对当前类别的文件,例如,如果我们在类别20,我们检查category_20.tpl存在。

if (file_exists(DIR_TEMPLATE . $config_theme . '/template/product/category_' . $category_id . '.tpl')) {

如果找到该文件:

$view = 'product/category_' . $category_id;

如果没有此类文件,请使用原始文件:category.tpl

} else {
    $view = 'product/category';
}

根据上述声明加载选定的视图文件。

$this->response->setOutput($this->load->view($view, $data));

<强>结论

$this->response->setOutput($this->load->view('product/category', $data));中找到catalog/controller/product/category.php并将其替换为上述代码,此处为完整代码:

$config_theme = $this->config->get('config_theme') == 'theme_default' ? 'default' : $this->config->get('config_theme');
if (file_exists(DIR_TEMPLATE . $config_theme . '/template/product/category_' . $category_id . '.tpl')) {
    $view = 'product/category_' . $category_id;
} else {
    $view = 'product/category';
}
$this->response->setOutput($this->load->view($view, $data));

答案 1 :(得分:0)

我将 OpenCart 2.2 设计成只有商品目录,index.php 有这一行:

$application_config = 'catalog';

我在这里找到了负责选择视图文件的行(MVC 技术):

catalog/controller/event/theme.php

我的主题目录是“default2” 在这里我更改路径的模板文件:/index.php?route=product/category&path=69_65 到文件目录/视图/主题/default2/template/product/category_69_65.tpl

我的代码片段的整个代码是这样的。

<?php
class ControllerEventTheme extends Controller {
    public function index(&$view, &$data) {
        if (!$this->config->get($this->config->get('config_theme') . '_status')) {
            exit('Error: A theme has not been assigned to this store!');
        }
        
        // This is only here for compatibility with old themes.
        if (substr($view, -4) == '.tpl') {
            $view = substr($view, 0, -4);
        }
        
        if ($this->config->get('config_theme') == 'theme_default') {
            $directory = $this->config->get('theme_default_directory');
        } else {
            $directory = $this->config->get('config_theme');
        }

        if (is_file(DIR_TEMPLATE . $directory . '/template/' . $view . '.tpl')) {
            $view = $directory . '/template/' . $view;
            //my snippet starts
            if ($view=='default2/template/product/category') {
                $url=$data['breadcrumbs'][count($data['breadcrumbs'])-1]['href'];
                if (substr($url, -5)=='69_65') {
                    if (is_file(DIR_TEMPLATE . $directory . '/template/product/category_69_65.tpl')) {
                        $view = $directory . '/template/product/category_69_65';
                    }
                }
            }
        //end of my snippet
        } else {
            $view = 'default/template/' . $view;
        }           
    }
}

这个文件是对 uri product/catalog & path=69_65 的反应:

catalog/view/theme/default2/template/product/category_69_65.tpl