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 に答える