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.
You can retrieve the attribute set data by id in Magento 2 to know about attribute details. You can use Magento\Catalog\Api\AttributeSetRepositoryInterface to retrieve the attribute set info with get($attributeId) method. Output will be entity_type_id, attribute_set_name and sort order of the attribute set.
Attribute sets can be defined as a list of attributes where all the characteristics of a product are demonstrated. For a new product the attribute set works as a template. Every product must belong to a specific attribute set which comes helpful in cases of: Dividing the products into groups.
Attribute set is a list of certain individual Magento product attributes, which fully describe all product's characteristics. Attribute set is used during every new product creation. This step lets one add all import information about the product in one step. By default, Magento includes 14 attribute sets.
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'); }
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With