2011-08-18 5 views
7

属性のフロントエンド値を取得する簡単な方法はありますか?

$attributes = array(
    'Category'   => 'type', 
    'Manufacturer'  => 'brand', 
    'Title'    => 'meta_title', 
    'Description'  => 'description', 
    'Product Link'  => 'url_path', 
    'Price'    => 'price', 
    'Product-image link' => 'image', 
    'SKU'    => 'sku', 
    'Stock'    => 'qty', 
    'Condition'   => 'condition', 
    'Shipping cost'  => 'delivery_cost'); 

製品コレクションを反復した後、次のような属性のフロントエンド値を取得します。

$attributeId = Mage::getResourceModel('eav/entity_attribute') 
    ->getIdByCode('catalog_product', $attribute_code); 
$attribute = Mage::getModel('catalog/resource_eav_attribute') 
    ->load($attributeId); 
$value = $attribute->getFrontend()->getValue($product); 

$product->getDate($attribute)はドロップダウンやマルチ選択では機能しません。フロントエンドの値ではなくIDだけを返します。

上記のコードはうまく動作しますが、値を得るのが難しいようですが、もっと重要なことに、それはかなり遅くなります。製品属性のフロントエンド値を取得するためのより迅速な、より賢明な方法がありますか?


を編集する私は今、以下のようにしています(imageqtyのような特別なケースを扱った後)。

$inputType = $product->getResource() 
        ->getAttribute($attribute_code) 
        ->getFrontend() 
        ->getInputType(); 

switch ($inputType) { 
case 'multiselect': 
case 'select': 
case 'dropdown': 
    $value = $product->getAttributeText($attribute_code); 
    if (is_array($value)) { 
     $value = implode(', ', $value); 
    } 
    break; 
default: 
    $value = $product->getData($attribute_code); 
    break; 
} 

$attributesRow[] = $value; 

誰かがこれを改善できれば(より簡単で効率的に)、回答を投稿してください。

+2

は、この記事を見てみましょうhttp://blog.chapagain.com.np/magento-how-to-get-attribute-name-and-値/ –

+1

ありがとう、役に立つ記事。 – Jamie

答えて

11

ドロップダウンやマルチセレクト、および製品のみの場合(これは一般的なEAVのテクニックではありません)、getAttributeText()を使用できます。

$value = $product->getAttributeText($attribute_code); 
+0

それを指摘してくれてありがとう。私はその方法が他の属性ではなくいくつかの属性にどのように作用したのか、と思っていました。 – Jamie

0

それはあなたの属性(例えば、meta_titleのため)が、最も簡単な方法のように通常である(それはあなたがそれに到達しようとしているコンテキストからアクセス可能である?)設定した方法によって異なります。

$product->getMetaTitle() 
+0

ありがとうございますが、一部の属性には魔法のゲッターメソッドを使用してアクセスできない場合があります。 – Jamie

3

バージョン1.7では、$product->getAttributeText($attribute_code)は製品ページで私にとっては機能しません。最初は、その属性がcatalog_product_flatインデックスにないためだと思いました。しかし属性がそこにあったことが判明しました。いずれにせよ、次のコードは私のために働く。私はシンプルなコードを試してから、EAVコードに戻ります。このような

だから私は使用していますコード:

$value = $product->getAttributeText($attribute_code); // first try the flat table? 
if(empty($value)) { // use the EAV tables only if the flat table doesn't work 
    $value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product); 
} 
+1

私は、製品のコレクションを取得していて、 'getValue($ product)'を実行して '' No ''を返していました。 '$ product'を' Mage :: getModel( 'catalog/product') - > load($ product-> getEntityId()) 'に変更し、そのメソッドがデータベースに送られる製品を得ました。それ以外の場合、コードは内部配列の値からデータを取得しようとします。通常の状況下では、私は上記のコードがうまく動作すると期待しています。 – pavlindrom

関連する問題