31

I am trying to get attribute set name in Magento product view template. I can get attribute value by $_product->getAttributeText('attribute'), but how do I get attribute set name?

I would like to display an attribute only if it is belong to a certain attribute set.

Knase
  • 1,224
  • 14
  • 23
Moon
  • 22,195
  • 68
  • 188
  • 269

5 Answers5

74

Whenever you have a product object, you can access its attribute set like this:

$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName  = $attributeSetModel->getAttributeSetName();

This will give you the name of the attribute set, which you can then compare using strcmp:

if(0 == strcmp($attributeSetName, 'My Attribute Set')) {
    print $product->getAttributeText('attribute');
}
starball
  • 20,030
  • 7
  • 43
  • 238
Joe Mastey
  • 26,809
  • 13
  • 80
  • 104
  • An anonymous user offered an edit suggestion, correcting `$attributeSet` to `#attributeSetName`. It looked reasonable, and so I approved it. However, I do not know this language, so do check to see if it is correct. – abcd Jun 30 '11 at 22:39
  • The last line should read: `$attributeSetName = $attributeSetModel->getAttributeSetName();` without the ')' at the end – Yeroon Aug 05 '11 at 08:18
  • Strange choice of function strcmp? Why not use the direct comparison with ===? – Matthew Haworth Jul 10 '14 at 20:50
  • @MatthewHaworth Honestly, the answer was from almost half a decade ago (geez...), so I really don't know why. If I had to guess, I may have preferred to use strcmp because it still allows implicit casting of other datatypes into strings, but is stricter than `==`. In this specific case, they'd both be just fine. – Joe Mastey Jul 12 '14 at 03:02
26

For more sexyness you can shorten it to:

$attributeSetName = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();
trish
  • 261
  • 3
  • 2
14

Try the following code:

$entityTypeId = Mage::getModel('eav/entity')
                ->setType('catalog_product')
                ->getTypeId();
$attributeSetName   = 'Default';
$attributeSetId     = Mage::getModel('eav/entity_attribute_set')
                    ->getCollection()
                    ->setEntityTypeFilter($entityTypeId)
                    ->addFieldToFilter('attribute_set_name', $attributeSetName)
                    ->getFirstItem()
                    ->getAttributeSetId();
echo $attributeSetId;

Find more info about Attribute Set in the following article.

Thanks

Roman Snitko
  • 3,655
  • 24
  • 29
MagePsycho
  • 1,944
  • 2
  • 29
  • 60
1

Joe's answer requires a couple of alterations in order for it to work.

Firstly it should be $_product not $product, and secondly there is an erroneous ')' in the last line.

The following code should be correct:

$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($_product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
Bit32
  • 326
  • 2
  • 5
0

Comparing to a text value can have problems if users decide to later change that text - which is easy to do in Magento for attribute sets. One other option is to use the underlying id instead which is never going to change.

You can get this by looking up the value of the attribute_set_id column in the database using

select * from eav_attribute_set;

This number is also in the edit link in admin which is in bold below

http://.../index.php/admin/catalog_product_set/edit/id/10/key/6fe89fe2221cf2f80b82ac2ae457909ce04c92c51716b3e474ecad672a2ae2f3/

Your code would then simply use that property of the product. Base on the id of 10 in the link above this would just be

if (10 == $_product->getAttributeSetId()) {
  //Do work
}
Andrew Rutter
  • 1,257
  • 2
  • 18
  • 34