This article shows how you can get attribute name or label, id, entity_type, etc. by attribute code in Magento 2.
If you would like get attribute name and value in Magento 1 then you may refer to this article: Magento: How to get attribute name and value?
I will be showing both Dependency Injection (DI) way and Object Manager way.
Using Dependency Injection (DI): Get attribute id, name and value from attribute code
Here is the code that you need to write in your Block class.
/**
* Eav Entity Attribute Collection
*
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
*/
protected $_entityAttributeCollection;
/**
* @var \Magento\Eav\Model\Entity\Attribute
*/
protected $_entityAttribute;
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
*/
protected $_attributeOptionCollection;
/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $eavEntityAttributeCollection
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $entityAttributeCollection,
\Magento\Eav\Model\Entity\Attribute $entityAttribute,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $attributeOptionCollection,
array $data = []
) {
$this->_entityAttributeCollection = $entityAttributeCollection;
$this->_entityAttribute = $entityAttribute;
$this->_attributeOptionCollection = $attributeOptionCollection;
parent::__construct($context, $data);
}
/**
* Load attribute data by code
*
* @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 of an 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 attribute option data of a single option of the attribute
*
* @param int $attributeId
* @param int $optionId
* @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
*/
public function getAttributeOptionSingle($attributeId, $optionId)
{
return $this->_attributeOptionCollection
->setPositionOrder('asc')
->setAttributeFilter($attributeId)
->setIdFilter($optionId)
->setStoreFilter()
->load()
->getFirstItem();
}
/**
* Get attributes by code
* Multiple entity types can have same attribute code
* Entity types 'catalog_product' & 'catalog_category' both have 'name' attribute code
* So, this function can return object of size greater than 1
*
* @return Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
*/
public function getAttributesByCode($code) {
$this->_entityAttributeCollection->getSelect()->join(
['eav_entity_type'=>$this->_entityAttributeCollection->getTable('eav_entity_type')],
'main_table.entity_type_id = eav_entity_type.entity_type_id',
['entity_type_code'=>'eav_entity_type.entity_type_code']);
$attributes = $this->_entityAttributeCollection->setCodeFilter($code);
return $attributes;
}
/**
* Get single product attribute data
*
* @return Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
*/
public function getProductAttributeByCode($code) {
$this->_entityAttributeCollection->getSelect()->join(
['eav_entity_type'=>$this->_entityAttributeCollection->getTable('eav_entity_type')],
'main_table.entity_type_id = eav_entity_type.entity_type_id',
['entity_type_code'=>'eav_entity_type.entity_type_code']);
$attribute = $this->_entityAttributeCollection
->setCodeFilter($code)
->addFieldToFilter('entity_type_code', 'catalog_product')
->getFirstItem();
return $attribute;
}
Below are the code to be written in template file.
Here, we will be fetching attribute data for attribute “color” of “product”.
$attributeCode = 'color';
$entityType = 'catalog_product';
$attributeInfo = $block->getAttributeInfo($entityType, $attributeCode);
//var_dump($attributeInfo->getData());
$attributeId = $attributeInfo->getAttributeId();
$optionId = 49; // YOUR_OPTION_ID
$attributeOptionAll = $block->getAttributeOptionAll($attributeId);
//var_dump($attributeOptionAll->getData());
$attributeOptionSingle = $block->getAttributeOptionSingle($attributeId, $optionId);
//var_dump($attributeOptionSingle->getData());
It’s better to use the above code to fetch attribute info. But, here is the other way to fetch attribute info by attribute code.
Getting all attributes with the attribute code ‘name’.
$code = 'name';
var_dump($block->getAttributesByCode($code));
Get attribute info with attribute code ‘name’ of the product entity type only.
$code = 'name';
var_dump($block->getProductAttributeByCode($code));
Using Object Manager: Get attribute info and it’s options
/**
* Get attribute info by attribute code and entity type
*/
$attributeCode = 'color';
$entityType = 'catalog_product';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/*$attributeInfo = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class)
->setCodeFilter($attributeCode)
->getFirstItem();*/
$attributeInfo = $objectManager->get(\Magento\Eav\Model\Entity\Attribute::class)
->loadByCode($entityType, $attributeCode);
//var_dump($attributeInfo->getData()); exit;
/**
* 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()); exit;
/**
* Fetch particular option's name and value of the attribute
*/
$optionId = YOUR_OPTION_ID;
$attributeOptionSingle = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
->setPositionOrder('asc')
->setAttributeFilter($attributeId)
->setIdFilter($optionId)
->setStoreFilter()
->load()
->getFirstItem();
//var_dump($attributeOptionSingle->getData()); exit;
Hope this helps. Thanks.