OpenCart-查看基于任意产品字段的替代产品模板 [英] OpenCart - View alternate product template based on arbitrary product field
问题描述
Stack Overflow上还有另一篇文章,其中包含以下代码,用于根据产品ID为多个产品模板提供服务
There is another post on Stack Overflow that includes the following code for serving multiple product templates based on product ID
//42 is the id of the product
if ($this->request->get['product_id'] == 42) {
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/customproduct.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/customproduct.tpl';
} else {
$this->template = 'default/template/product/customproduct.tpl';
}
} else {
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/product.tpl';
} else {
$this->template = 'default/template/product/customproduct.tpl';
}
}
我想检查一个我不会使用的替代产品字段值,而不是ID,因此可以通过管理面板进行管理.
I would like to check for an alternate product field value that I won't be using instead of ID so it is something that can be managed from the admin panel.
例如,一条语句显示为如果产品位置=附件,则获得product/accessory.tpl"
For example, a statement that reads "If product location = accessory then get product/accessory.tpl"
在使用if语句请求该字段之前,是否必须将该字段加载到产品控制器中?
Would I have to load that field in the product controller before I can request it with the if statement?
语法是什么样的?
推荐答案
您应该能够在管理面板中使用产品数据中的任何字段,例如您已经引用的位置.
You should be able to use any of the fields in product data in the admin panel such as Location that you already referenced.
product
表中所请求行的所有内容都应出现在$product_info
数组中.
Everything from the product
table for your requested row should be present in the $product_info
array.
尝试这样的事情:
$template = ($product_info['location'] == 'accessory') ? 'accessory.tpl' : 'product.tpl';
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/' . $template)) {
$this->template = $this->config->get('config_template') . '/template/product/' . $template;
} else {
$this->template = 'default/template/product/' . $template;
}
如果您预计在不同的位置会有许多不同的模板,那么使用开关控件会更有效.
If you anticipate there will be many different templates for different locations it would be more efficient to use a switch control.
switch ($product_info['location']):
case 'accessory':
$template = 'accessory.tpl';
break;
case 'tool':
$template = 'tool.tpl';
break;
default:
$template = 'product.tpl';
break;
endswitch;
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/' . $template)) {
$this->template = $this->config->get('config_template') . '/template/product/' . $template;
} else {
$this->template = 'default/template/product/' . $template;
}
希望有帮助.
这篇关于OpenCart-查看基于任意产品字段的替代产品模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!