0

I try to get the attribute-set name for a product in a phtml file. In magento 1 the code for this is:

$id = 2;
$product = Mage::getModel(‘catalog/product’)->getCollection->load($id);
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
echo $attributeSetName;

i have the attribute_set_id, but i try to get the name.

Which one is equivalent for magento 2? Any ideas?

0

1 Answer 1

3

For product page use below code :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$attributeSet = $objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface');
$attributeSetRepository = $attributeSet->get($product->getAttributeSetId());
$attribute_set_name = $attributeSetRepository->getAttributeSetName();
echo $attribute_set_name;

For list Page in foreach loop :

$attributeSet = $objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface');
$attributeSetRepository = $attributeSet->get($product->getAttributeSetId());
$attribute_set_name = $attributeSetRepository->getAttributeSetName();
echo $attribute_set_name;

I override list.phtml file in custom module and add "For list page in foreach loop" code.But i got error undefined variable product. How can i resolve this error? Can you help me?

4
  • i try to get attribute name for products which are in the shopping cart. so i guess i need to use the frist code. my products came in a array. where should i need to put my product array? Commented Nov 9, 2016 at 13:03
  • what you want Attribute name or attribute set name?
    – user46386
    Commented Nov 9, 2016 at 13:06
  • attribute set name Commented Nov 9, 2016 at 13:19
  • In cart page you can get product object and then try above code it will give you attribute set name
    – user46386
    Commented Nov 9, 2016 at 13:23

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