magento 2 I would like to get an attribute option label by attribute option id.

For Example:-

attribute option value : 53,

attribute option label/text/name : green

from the above scenario i want to get attribute option label/text/name.

Note: without loading product i need to get this. please suggest me.

  • Where do you want to get this attribute? Commented Jan 10, 2018 at 10:41

4 Answers 4


You need to add following in your __construct method

protected $optionFactory;

protected $_attributeOptionCollection;

public function __construct(
  \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionFactory,
  \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $attributeOptionCollection
    $this->optionFactory = $optionFactory;
    $this->_attributeOptionCollection = $attributeOptionCollection;

Now you can get option data using option value;

$optionValue = 53; // your attribute value
$optionFactory = $this->optionFactory->create();
$optionFactory->load($optionValue); // load by option value
$attributeId = $optionFactory->getAttributeId(); // atribute id of given option value
$optionData = $this->_attributeOptionCollection
                ->load(); // load option data by attribute id and given option value
echo "<pre>"; print_r($optionData->getData()); exit;
  • 1
    thanks for replay, but i my code i have only attribute option value. i am not able to get attribute code also. so i need attribute details by attribute option value. Commented Jan 10, 2018 at 4:11
  • check my updated answer. Commented Jan 10, 2018 at 7:15
  • @ChanderShekhar, can we just check if optionId exist or not ? suppose i have id 53, it has to return true, else should return false. Commented Feb 13, 2019 at 6:13
  • 1
    The pointed me in the right direction. Although use of \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection caused issue for me when multiple option ids are queried. Should use factory method \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory, and use create() but not load() Commented Jun 4, 2019 at 13:25

Try the following code:

$_optionId = '53';
$_attributeId = $_product->getResource()->getAttribute('color');
if ($_attributeId->usesSource()) {
      $_optionText = $_attributeId->getSource()->getOptionText($_optionId);
  • please suggest me without loading/using product. also i have updated my questioin. Thanks for replay Commented Jan 9, 2018 at 11:24
  • ok, for that you need to create a custom module, try to follow the tutorial in this link blog.chapagain.com.np/…
    – Vlad Patru
    Commented Jan 9, 2018 at 11:28
  • 1
    in given block also they are using attribute code. but i don't have attribute code also. i want to get by attribute option value Commented Jan 9, 2018 at 11:30
  • to be honest I don't really understand why would you do it without attribute code, every attribute option is assigned to certain attribute
    – Vlad Patru
    Commented Jan 9, 2018 at 11:37
  • in my code i have only attribute option value/id With that i want toget label name. Commented Jan 9, 2018 at 11:42

Try the below code:

$option_id = 5431;
$attribute_code = 'coffee_type';
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();

    $eavConfig = $objectManager->get('\Magento\Eav\Model\Config');

    $attribute = $eavConfig->getAttribute('catalog_product', $attribute_code);
    $optionlabel =  $attribute->getSource()->getOptionText($option_id);

Try the following code to get option label by option Id directly from the database.

$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('eav_attribute_option_value');

$option_id = 350;
$sql = "select * FROM " . $tableName . " where option_id=".$option_id;
$result = $connection->fetchAll($sql);

echo $option_label = $result[0]['value'];

Happy coding :)

  • 2
    not best practice imo
    – Radu
    Commented Oct 2, 2018 at 11:31
  • 2
    In most cases if you have to use an SQL query you're probably doing it wrong. Commented May 3, 2019 at 15:11
  • @shivashankar m Did you find a proper way to get the data for attribute option by id without specifying attribute code? I am also in need to that! Commented Aug 13, 2019 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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