Playing with Attribute Set in Magento

November 18, 2011  |  6 Comments  |  by Raj (MagePsycho)  |  Latest, Magento


An attribute set is a collection of attributes which can be created/edited from Catalog -> Manage Attribute Sets in backend.

Some useful snippets regarding Attribute Set.

1> Getting Attribute Set Details

$attributeSetModel = Mage::getModel('eav/entity_attribute_set');
$attributeSetId	   = 9; // or $_product->getAttributeSetId(); if you are using product model

2> Getting Attribute Set Id by Name

$entityTypeId = Mage::getModel('eav/entity')
$attributeSetName	= 'Default';
$attributeSetId		= Mage::getModel('eav/entity_attribute_set')
					->addFieldToFilter('attribute_set_name', $attributeSetName)
echo $attributeSetId;

Note that the following code doesn’t work

$attributeSetId = Mage::getModel('eav/entity_attribute_set')
->load($attributeSetName, 'attribute_set_name')

Because if you refer to eav_attribute_set table then you can see there are lots of rows with same attribute set name(for example: Default)

3> Getting all Attribute Sets by Entity Type(catalog_product)

$entityTypeId = Mage::getModel('eav/entity')
$attributeSetCollection = Mage::getModel('eav/entity_attribute_set')
foreach($attributeSetCollection as $_attributeSet){

Alternatively, you can also use:

$attributeSets = Mage::getModel('catalog/product_attribute_set_api')->items();
foreach($attributeSets as $_attributeSet){

4> Getting all attributes in an Attribute Set

$attributes = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId);
foreach($attributes as $_attribute){

5> Filtering Products by Attribute Set

$productCollection = Mage::getModel('catalog/product')
					->addFieldToFilter('attribute_set_id', $attributeSetId);
foreach($productCollection as $_product){

Hope you will find these snippets useful/helpful.
Please do share if you have any useful code snippets regarding Attribute Set.

Thanks for reading!

Posted in Latest, Magento and tagged , , , , , . Bookmark the permalink.

About Raj (MagePsycho)

Raj, the guy behind MagePsycho is a Zend PHP5 Certified Engineer, Magento Certified Developer, Magento Moderator / Freelancer with specialization in web applications (CMS, E-Commerce, ERP etc.). Catch him on: Twitter: @magepsycho Skype: magentopycho
  • Nara123

    Great Snippets for Magento Developers.

    Can you also share your findings about Tax module. I was searching for finding tax rate by customers tax class but I did not find any good posts after googling for some hours.

    Thanks Again

  • Thanks for this.
    Just noticed a typo ..’Hope you will find these snippets useful/helful.’ Is ‘helful’ a Freudian slip, seeing how coding for Magento can seem like hell sometimes?

    • Thanks for reporting.
      Typo has been fixed 🙂

  • For performance optimization, it’s better to use following code for getting product entity id:


  • Palmer Del Campo

    Hi, thanks for this useful post. I was wondering if you could tell me how to count how many different attribute sets are in the cart during one-page-checkout. For example, if I have 6 products in my cart, and they all have different attribute sets, the number I’m looking for would be 6. Thanks!

  • Hind Walieddine

    Hi thank you a lot for the post
    I am using $attributeSets = Mage::getModel(‘catalog/product_attribute_set_api’)->items();
    to get all the attribute set,
    But what I really want is to get all custom attribute set related to one of the attribute set.
    Is there anyone can help me how to do it?
    Thank you