Moon
Moon

Reputation: 22565

How do I get attribute set name?

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.

Upvotes: 31

Views: 62319

Answers (5)

Joe Mastey
Joe Mastey

Reputation: 27119

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

Upvotes: 74

Andrew Rutter
Andrew Rutter

Reputation: 1297

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
}

Upvotes: 0

MagePsycho
MagePsycho

Reputation: 2004

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

Upvotes: 14

trish
trish

Reputation: 261

For more sexyness you can shorten it to:

$attributeSetName = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();

Upvotes: 26

Bit32
Bit32

Reputation: 346

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

Upvotes: 1

Related Questions