Magentoから属性オプションのリストを取得します
-
10-10-2019 - |
質問
私はMagentoのような属性オプションをつかんできました:
<?php
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
?>
組み込みの「色」属性のオプションを取得しようとするまで、それは正常に動作してきました - 次のエラーが得られました。
PHP Fatal error: Call to a member function setAttribute() on a non-object in app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php on line 374
のように見えます getSource()
呼び出しが失敗し、このエラーを引き起こします。なぜこれが起こるのか、どのようにして色のオプションを出すことができるか知っていますか?
ありがとう!
解決
Magento属性の初期化プロセスを使用する代わりに、自分で属性を初期化するように見えます。
Mage::getSingleton('eav/config')
->getAttribute($entityType, $attributeCode)
1.4.x Magentoには、カタログと顧客の個別の属性モデルがあるため catalog_product
これで、EAV属性モデルから移動します(Mage_Eav_Model_Entity_Attribute
)カタログ1に(Mage_Catalog_Model_Resource_Eav_Attribute
).
その結果、一部のカタログ属性はEAV属性モデルでは機能しません。特に使用するもの Mage_Eav_Model_Entity_Attribute_Source_Table
ただし、明示的に定義しないでください(色、メーカーなど)。
次のコードスニペットは、インストールで完全に機能するはずです。
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'color');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
ところで Mage_Eav_Model_Config
モデルには多くの有用な方法があり、開発に使用できるので、このモデルを覗き込むことをheしないでください。
他のヒント
Resource_Modelが空の場合、上記のコードは機能しません。次のスニペットが仕事をします:
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'YOUR_ATTRIBUTE_CODE');
/** @var $attribute Mage_Eav_Model_Entity_Attribute */
$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($attribute->getId())
->setStoreFilter(0, false);
$attribute = Mage::getModel('eav/config')->getAttribute('customer','cateinterest');
$options = $attribute->getSource()->getAllOptions();
<?php
//Possible color value
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color'); //"color" is the attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
$id = $instance['value']; //id of the option
$value = $instance['label']; //Label of the option
不完全な答えは申し訳ありませんが、特にデータベースをご覧ください。 backend_model
桁。この点でこのフィールドをいくつかのシステムフィールドに一致させるように設定するまで、これと同じ問題を抱えていることを覚えているようです。