How to Get Attribute Id, Name, Value from Attribute Code in Magento 2?

In Magento, attributes are used to manage the piece of information associated with the customer, product and product’s category. This attribute will appear as fields in forms, product reviews, layered navigation, email messages, etc. As a developer, while developing a Magento 2 website or extension, it’s a usual work to get the attribute information like attribute id, name or label, value, etc by using attribute code. So here is a blog which helps you to get the attribute information from the attribute code in Magento 2.

In the below example, I will be using both Dependency Injection (DI) and Object Manager to get the attribute information using attribute code in Magento 2.

Using Dependency Injection (DI)

First we will see how to get the attribute id, name and value from attribute code using dependency injection. To get the attribute related information, we need to inject the object of below classes in the constructor of our module’s block file and access it from the view (.phtml) file.

  • \Magento\Eav\Model\Entity\Attribute
  • \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
  • \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection

Here is the example code to get the attribute id, name and value from attribute code using dependency injection.

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;
 
class YourCustomBlock extends \Magento\Framework\View\Element\Template {
  
    /**
     * @var \Magento\Eav\Model\Entity\Attribute
     */
    protected $_entityAttribute;
 
    /**
     * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
     */
    protected $_entityAttributeCollection;
  
    /**
     * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
     */
    protected $_entityAttributeOptionCollection;
  
    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Eav\Model\Entity\Attribute $entityAttribute
     * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $eavEntityAttributeCollection 
     * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $entityAttributeOptionCollection
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Eav\Model\Entity\Attribute $entityAttribute,
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $entityAttributeCollection, 
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $entityAttributeOptionCollection,
        array $data = []
    ) {
        $this->_entityAttribute = $entityAttribute;
        $this->_entityAttributeCollection = $entityAttributeCollection; 
        $this->_entityAttributeOptionCollection = $entityAttributeOptionCollection;
  
        parent::__construct($context, $data);
    }
 
    /**
     * Get attribute info by attribute code and entity type
     *
     * @param mixed $entityType can be integer, string, or instance of class Mage\Eav\Model\Entity\Type
     * @param string $attributeCode
     * @return \Magento\Eav\Model\Entity\Attribute
     */
    public function getAttributeInfo($entityType, $attributeCode)
    {
        return $this->_entityAttribute->loadByCode($entityType, $attributeCode);
    }
  
    /**
     * Get all options name and value of the attribute
     *
     * @param int $attributeId
     * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
     */
    public function getAttributeOptionAll($attributeId)
    {
        return $this->_attributeOptionCollection
                    ->setPositionOrder('asc')
                    ->setAttributeFilter($attributeId)
                    ->setStoreFilter()
                    ->load();
    }
  
    /**
     * Get particular option's name and value of the attribute
     *
     * @param int $attributeId
     * @param int $optionId
     * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
     */
    public function getAttributeOptionById($attributeId, $optionId)
    {
        return $this->_attributeOptionCollection
                    ->setPositionOrder('asc')
                    ->setAttributeFilter($attributeId)
                    ->setIdFilter($optionId)
                    ->setStoreFilter()
                    ->load();
    }
 
    /**
     * Get the first option's name and value of the attribute
     *
     * @param int $attributeId
     * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
     */
    public function getAttributeFirstOption($attributeId)
    {
        return $this->_attributeOptionCollection
                    ->setPositionOrder('asc')
                    ->setAttributeFilter($attributeId)
                    ->load()
                    ->getFirstItem();
    } 
}

Now, we can use the functions in our view (.phtml) file as follows. Here, we will be fetching attribute data for attribute “style_bags”.

$attributeCode = 'style_bags';
$entityType = 'catalog_product';
  
$attributeInfo = $block->getAttributeInfo($entityType, $attributeCode);
var_dump($attributeInfo->getData());
  
$attributeId = $attributeInfo->getAttributeId();
$optionId = 26;
  
$attributeOptionAll = $block->getAttributeOptionAll($attributeId);
var_dump($attributeOptionAll->getData());
  
$attributeOptionSingle = $block->getAttributeOptionById($attributeId, $optionId);
var_dump($attributeOptionSingle->getData());
 
$attributeOptionSingle = $block->getAttributeFirstOption($attributeId);
var_dump($attributeOptionSingle->getData());

Using Object Manager

Next we will see how code to get the attribute id, name and value from attribute code using object manager.

/**
 * Get attribute info by attribute code and entity type
 */
$attributeCode = 'style_bags';
$entityType = 'catalog_product';
  
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  
$attributeInfo = $objectManager->get(\Magento\Eav\Model\Entity\Attribute::class)
                               ->loadByCode($entityType, $attributeCode);
  
var_dump($attributeInfo->getData());
  
/**
 * Get all options name and value of the attribute
 */
$attributeId = $attributeInfo->getAttributeId();
$attributeOptionAll = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
                                    ->setPositionOrder('asc')
                                    ->setAttributeFilter($attributeId)
                                    ->setStoreFilter()
                                    ->load();
  
var_dump($attributeOptionAll->getData());
  
/**
 * Get particular option's name and value of the attribute
 */
$optionId = 26; 
$attributeOptionSingle = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
                                       ->setPositionOrder('asc')
                                       ->setAttributeFilter($attributeId)
                                       ->setIdFilter($optionId)
                                       ->setStoreFilter()
                                       ->load();
  
var_dump($attributeOptionSingle->getData());
 
/**
 * Get the first option's name and value of the attribute
 */
$attributeOptionSingle = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
                                       ->setPositionOrder('asc')
                                       ->setAttributeFilter($attributeId)
                                       ->setStoreFilter()
                                       ->load()
                                       ->getFirstItem();
  
var_dump($attributeOptionSingle->getData());

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *