开发者

Magento: getting attributes from an attribute set without a product

I have set an attribute set in my Magento shop which has several binary attributes.

For a p开发者_开发技巧ulldown I need a list of ALL the attributes inside this one attribute set, including their internal name and their label. Since this pulldown should appear in places that not necessarily have a product selected I can't go the usual route of "getting the attributes of a product".

How do I go about of getting a list of all the attributes inside my set?


OK, I realised that I missed that you want the whole set of attributes, not just an individual one. Try this:

$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();

You'll then need to iterate through the array that is returned with something like:

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;

Sorry for missing the original point, hope this helps!

Cheers, JD


In order to get all the attributes in an attribute set, you can use as:

$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);
}

Cheers!!


try this snippet, it should give you want you need, except for multi-select attributes.

$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;

Hope this helps, JD

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜