Get Product Attribute Information From Attribute Code in Magento 2


All the products in Magento 2 have some attributes like description, SKU, brand etc. While developing an e-commerce website many times we need to display the product attribute information on product listing page or product detail page.

In this code snippet, we will see how to fetch product attribute name, value, type and other required information from attribute code.

Note: For the demonstrated purpose we have used Objectmanager.Codextblog never recommend the direct use of ObjectManager.One should always use a constructor method to instant an object.

Get Attribute Information

//attribute code
$attributeCode = 'color';
//entity type
$entityType = 'catalog_product';

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$attribute = $objectManager->get(\Magento\Eav\Model\Entity\Attribute::class)
             ->loadByCode($entityType, $attributeCode);

//attribute id
echo $attribute->getAttributeId();

//Name/Title of attribute
echo  $attribute->getFrontendLabel();

//attribute default value
echo  $attribute->getDefaultValue();

//get backend type
echo  $attribute->getBackendType();

//get input type
echo  $attribute->getFrontendInput();

Get All Options of Select Attribute

//attribute code
$attributeCode = 'material';
//entity type
$entityType = 'catalog_product';

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$attribute = $objectManager->get(\Magento\Eav\Model\Entity\Attribute::class)
    ->loadByCode($entityType, $attributeCode);

$attributeId = $attribute->getAttributeId();
$attributeOption = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)

foreach ($attributeOption as $option){

    echo $option->getValue()."<br>";

Hope this code snippet helps you. If you liked this post, then please like us on Facebook and follow us on Twitter.

Want to ask a question or leave a comment?

Leave a Comment


All the comments are goes into moderation before approval. Irrelevant comment with links directly goes to spam. Your email address will not be published.

Was this post helpful? Please support Us!

Follow us on twitter or Like us on facebook.


To Avoid Spam Downloads, We Want Your Email

We will send you download link right
away. Please submit form below.

Increase Your
Magento 2

Get Weekly Tutorial
to your Inbox
Subscribe Me
Subscribe Here