Get Customer Attribute Information from Attribute Code in Magento 2?

In my previous article “How to Get Attribute Id, Name, Value from Attribute Code in Magento 2”, I have shown to get catalog attribute information from attribute code. Here, I am going to show you, how to get the customer attribute information from attribute code in Magento 2.

Using Dependency Injection (DI)

Here is the example code to get the customer attribute information from attribute code using dependency injection.

In this, we might 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

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 gender.

$attributeCode = 'gender';
$entityType = 'customer';
  
$attributeInfo = $block->getAttributeInfo($entityType, $attributeCode);
var_dump($attributeInfo->getData());
  
$attributeId = $attributeInfo->getAttributeId();
$optionId = 2;
  
$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

Here is the example code to get the customer attribute information from attribute code using object manager.

/**
 * Get attribute info by attribute code and entity type
 */
$attributeCode = 'gender';
$entityType = 'customer';
  
$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 = 2; 
$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());

Note: There is no big difference between catalog and customer to get the attribute information. To get the catalog attribute information, set $entityType as catalog_product in the above code and to get the customer attribute information, set $entityType as customer.

Hope this helps.

Leave a Reply

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