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

问题描述:

我使用的是OpenCart 2.2.0.0版本,并尝试为每个类别和产品页面设置不同的模板。网上搜索,我发现下面的代码: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的,但在新版本中,我没有找到在目录/控制器/产品类似的代码结构/ category.php文件

如何我可以在OpenCart 2.2.0.0中获得类似的结果吗?

+0

你使用默认的主题? – DigitCart

+0

不,我正在写一个新的主题,但控制器文件在默认主题 – Mithu

由于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)); 

结论:

找到catalog/controller/product/category.php$this->response->setOutput($this->load->view('product/category', $data));,并与上面的代码替换它,这里是全码:

$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)); 
+0

嗨,感谢它的工作。我可以动态获取主题名称 – Mithu

+0

不客气,它必须识别您的自定义主题名称,我已使用默认和自定义主题对其进行了测试。但如果你有任何问题,你可以使用'$ config_theme ='My_Theme';'而不是'$ config_theme = $ this-> config-> get('config_theme')=='theme_default'? 'default':$ this-> config-> get('config_theme');' – DigitCart

+0

ok gr8是可能的目标子类别也可以用波纹管代码添加类别明智的独立样式表使用它在波纹管之前$这 - >文档 - >的setTitle。 'if(file_exists(DIR_TEMPLATE。$ this-> config-> get('config_template')。'/ stylesheet/category_'。$ category_id。'.css')){$ this-> document-> addStyle('catalog/view/theme /'。$ this-> config-> get('config_template')。'/ stylesheet/category_'。$ category_id。'.css'); } $ this-> document-> setTitle($ category_info ['name']); '我在这里怎么用这个? – Mithu