0

我可以将一堆代码添加到 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 中。但是我该怎么做呢?

4

2 回答 2

1

您可以使用自定义块类。但是为名称生成创建辅助方法更容易。有关助手的更多信息:

于 2012-09-18T07:47:08.143 回答
1

这种代码应该放在产品模型中。最好的方法是覆盖产品类,但为简单起见,我将描述更简单的方法:

1) 复制

/app/code/core/Mage/Catalog/Model/Product.php

/app/code/local/Mage/Catalog/Model/Product.php

2) 向文件中添加新方法

/**
 * Get custom name here...
 *
 * @return    string
 */
public function getCombinedName()
{
    // your code below....
    $attributes = $this->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;

    }

    return $name;
}
于 2012-09-18T07:56:55.647 回答