7

Having an attribute set, how can I get a list of the attributes it contains (or better yet, just the custom attributes that don't belong to the Default attribute set)?

The attribute set itself can be obtained in several ways, such as:

$entityTypeId = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
$attributeSet = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityTypeId)->addFilter('attribute_set_name', 'Default');

Note that I need to use the attribute set, so getting the list of attributes from a product is not the solution I am looking for.

6 Answers 6

16
Mage::getModel('catalog/product_attribute_set_api')->items();

Gets the attribute sets themselves.

Mage::getModel('catalog/product_attribute_api')->items($setId);

Gets the attributes inside the attribute sets.

7

The right way:

$attributes = Mage::getResourceModel('catalog/product_attribute_collection')
->setAttributeSetFilter($attributeSetId)
->getItems();
var_dump($attributes);

You can change resources 'catalog/product_attribute_collection' (customer, ...) And Set ID $attributeSetId

6

I believe the answer lies in this model

Mage::getModel('catalog/product_attribute_set_api')->items($setId);

The class is Mage_Catalog_Model_Product_Attribute_Api it seems to have two methods. The items() methods seems to do what you ask i.e. "Retrieve attributes from specified attribute set"

I hope that helps :)

1
  • 10
    I think you mean Mage::getModel('catalog/product_attribute_api')->items($setId);, ...attribute_set_api would give a Mage_Catalog_Model_Product_Attribute_Set_Api, correct? Oct 1, 2012 at 8:18
3

Regarding Attribute Set, there is a good collection of code snippets in the following blog article: http://www.blog.magepsycho.com/playing-with-attribute-set-in-magento/

Hope you will find them useful. Thanks

2

You don't necessarily need to access the API class. There is a more natural approach available. If you have a product:

/** @var Mage_Catalog_Model_Product $product **/
$attributes = $product->getTypeInstance(true)->getSetAttributes($product);

If not:

$attributes = Mage::getModel('catalog/product')->getResource()
  ->loadAllAttributes()
  ->getSortedAttributes($attributeSetId);
1

I've been searching for an answer, I could'nt find it and I solve my problem with these lines;

        $attributeSetId = /* set id */;
        $attributes = array();

        $groups = Mage::getModel('eav/entity_attribute_group')
            ->getResourceCollection()
            ->setAttributeSetFilter($attributeSetId)
            ->setSortOrder()
            ->load();

        foreach ($groups as $node) {

            $nodeChildren = Mage::getResourceModel('catalog/product_attribute_collection')
                ->setAttributeGroupFilter($node->getId())
                //->addFieldToFilter('is_user_defined', true) # I was trying to get user defined attributes.
                ->addVisibleFilter()
                ->load();

            if ($nodeChildren->getSize() > 0) {
                foreach ($nodeChildren->getItems() as $child) {
                    $attr = array(
                        'id'                => $child->getAttributeId(),
                        'text'              => $child->getAttributeCode()
                    );

                    $attributes[] = $attr;
                }
            }
        }

        var_dump($attributes);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.