-1

Stack Overflow 上有另一篇文章,其中包含以下代码,用于根据产品 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,因此它可以从管理面板进行管理。

例如,声明“如果产品位置 = 附件,则获取产品/附件.tpl”

我是否必须先在产品控制器中加载该字段,然后才能使用 if 语句请求它?

语法会是什么样子?

4

1 回答 1

0

You should be able to use any of the fields in product data in the admin panel such as Location that you already referenced.

Everything from the product table for your requested row should be present in the $product_info array.

Try something like this:

$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;
}

Hope that helps.

于 2013-08-14T10:52:01.503 回答