2012-02-14 7 views
3

私はMagentoの新しい "Filter by Product"モジュールで作業しています。すべての属性とその値を取得する必要がある状況があります。私はこれをGoogleで検索し、以下のコードMagentoで製品のすべてのアクティブな属性を取得するには?

 

$product = Mage::getModel('catalog/product'); 

$attributes = Mage::getResourceModel('eav/entity_attribute_collection') 
     ->setEntityTypeFilter($product->getResource()->getTypeId()) 
     ->load(); 

     //  ->addFieldToFilter('attribute_code', 'color') 

$attribute = $attributes->getFirstItem()->setEntity($product->getResource()); 

/* @var $attribute Mage_Eav_Model_Entity_Attribute */ 

$attr = $attribute->getSource()->getAllOptions(true); 

foreach ($attr as $att) { 
    echo " Label : ".$att['label']." Value : ".$att['value'].""; 
} 
 

を見つけましたが、これは、すべての利用可能な属性のリストからラベルのみと最後の属性の値を取得します。

どのようにすべての属性を取得するには?このコードで何が間違っていますか?

おかげで、 バラン

答えて

14

これを試してみてください:

$attributes = Mage::getSingleton('eav/config') 
    ->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection(); 

// Localize attribute label (if you need it) 
$attributes->addStoreLabel(Mage::app()->getStore()->getId()); 

// Loop over all attributes 
foreach ($attributes as $attr) { 
    /* @var $attr Mage_Eav_Model_Entity_Attribute */ 
    // get the store label value 
    $label = $attr->getStoreLabel() ? $attr->getStoreLabel() : $attr->getFrontendLabel(); 
    echo "Attribute: {$label}\n"; 

    // If it is an attribute with predefined values 
    if ($attr->usesSource()) { 

     // Get all option values ans labels 
     $options = $attr->getSource()->getAllOptions(); 

     // Output all option labels and values 
     foreach ($options as $option) 
     { 
      echo " {$option['label']} (Value {$option['value']})\n"; 
     } 
    } 
    else 
    { 
     // Just for clarification of the debug code 
     echo " No select or multiselect attribute\n"; 
    } 
} 
+0

私は私のモジュールにこのコードをロードしようとした..「あなたは相関名を定義することはできません、それはすべての属性を示していますが、いくつかの時間の後に、それは誤りで終わる 『アル』より" – balanv

+0

完全なエラーバックトレースを投稿してください。 – Vinai

+0

したがって、オブジェクトの親関数の関数を複数回呼び出すだけで、わからないものを実際に伝える必要がありました。 –

0

これが最初のソリューションです: http://blog.alexparadise.com/magento-dbeav-class/

$products = Mage::getModel('catalog/product')->getCollection(); 
foreach($this->products as $product) { 
    $prod = Mage::getModel('catalog/product')->load($product->getId()); 
} 

あなたが外Magentoのからのデータを使用して使用している場合、あなたは私が作ったクラスを使用することができますそれはベータですが、それはあなたのケースで動作するはずです。 EAVテーブルの概念を取得し、独自のSQLクエリを作成します。

関連する問題