我可以将一堆代码添加到 app/design/frontend/default/mytheme/catalog/product/list.phtml,但我想知道是否有一种方法可以在另一个文件中创建“名称”值并简洁地检索它在上述文件中。
我不想为每个项目硬编码名称,而是将每个产品属性的名称拼凑在一起,并根据产品类型使用不同的逻辑。
半伪代码:
$attributes = $product->getAttributes();
// universal attributes:
$manuf = $attributes['manufacturer']->getFrontend()->getValue($product);
$color = $attributes['color']->getFrontend()->getValue($product);
$type = $attributes['product_type']->getFrontend()->getValue($product);
// base name, added to below
$name = $manuf . ' ' . $type . ' in ' . $color;
// then logic for specific types
switch($type) {
case 'baseball hat':
$team = $attributes['team']->getFrontend()->getValue($product);
$name .= ' for ' . $team;
break;
case 'stilts':
$length = $attributes['length']->getFrontend()->getValue($product);
$name .= ' - ' . $length . ' feet long';
break;
}
由于这个逻辑可能会变得很长,我觉得它不应该全部塞进 list.phtml 中。但是我该怎么做呢?