2010-07-18 165 views
2

我已经在我的Magento商店中设置了一个属性集,其中有几个二进制属性。Magento:从没有产品的属性集中获取属性

对于下拉列表,我需要列出这一个属性集中的所有属性,包括它们的内部名称和它们的标签。由于这种下拉应该出现在不一定有产品选择的地方,所以我不能走通常的“获取产品属性”的路线。

我该如何着手获取我的设置中所有属性的列表?

回答

5

OK,我意识到,我错过了你想要的整套属性,而不仅仅是一个单独的一个。试试这个:

$productEntityType = Mage::getModel('eav/entity_type')->loadByCode(Mage_Catalog_Model_Product::ENTITY); 

$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection'); 
$attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection') 
    ->setEntityTypeFilter($productEntityType->getId()) //4 = product entities 
    ->addSetInfo() 
    ->getData(); 

然后您就需要通过返回的东西,如数组遍历:

foreach($attributesInfo as $attribute): 
    $attribute = Mage::getModel('eav/entity_attribute')->load($attribute['attribute_id']); 
    echo 'label = '.$attribute->getFrontendLabel().'<br/>'; 
    echo 'code = '.$attribute->getAttributeCode().'<br/><br/>'; 
endforeach; 

对不起失踪的原点,希望这有助于!

干杯, JD

+0

好多了,当我使用你的代码时,我得到了一个属性列表,但不是我自定义的。我通过将“ - > setEntityTypeFilter”从4改为10来获得这些属性,尽管我不确定为什么是10.我刚刚到达那里随机尝试。:) 无论如何,谢谢。 – Sorcy 2010-07-27 12:10:20

+0

没问题。您应该能够通过查看数据库中的eav_entity_type表来查找您的实体的ID。看起来像10 =销售/ quote_payment,至少在法师v1.4.1 高兴它为你工作:) JD – 2010-07-27 23:31:07

+0

感谢编辑@FabianBlechschmidt – 2014-09-29 07:49:24

1

试试这个片段,它应该会给你想要的,除了多选属性。

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product','attribute_name'); 
    foreach($attribute->getSource()->getAllOptions(true,true) as $option){ 
     $attributeArray[$option['value']] = $option['label']; 
    } 
    return $attributeArray; 

希望这有助于 JD

+0

不,只是返回一个空数组。 :( – Sorcy 2010-07-25 19:22:01

5

为了获得在设置属性的所有属性,你可以使用如:

$entityTypeId = Mage::getModel('eav/entity') 
                ->setType('catalog_product') 
                ->getTypeId(); 
$attributeSetName   = 'Default'; //Edit with your required Attribute Set Name 
$attributeSetId     = Mage::getModel('eav/entity_attribute_set') 
                    ->getCollection() 
                    ->setEntityTypeFilter($entityTypeId) 
                    ->addFieldToFilter('attribute_set_name', $attributeSetName) 
                    ->getFirstItem() 
                    ->getAttributeSetId(); 
$attributes = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId); 
foreach($attributes as $_attribute){ 
    print_r($_attribute); 
} 

干杯!

相关问题