1

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? – Vivek Kumar Jan 10 '18 at 10:41
5

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
                ->setPositionOrder('asc')
                ->setAttributeFilter($attributeId)
                ->setIdFilter($optionValue)
                ->setStoreFilter()
                ->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. – shivashankar m Jan 10 '18 at 4:11
  • check my updated answer. – Chander Shekhar Jan 10 '18 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. – jafar pinjar Feb 13 at 6:13
  • 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() – Dominic Xigen Jun 4 at 13:25
7

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 – shivashankar m Jan 9 '18 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 Jan 9 '18 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 – shivashankar m Jan 9 '18 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 Jan 9 '18 at 11:37
  • in my code i have only attribute option value/id With that i want toget label name. – shivashankar m Jan 9 '18 at 11:42
2

Try 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 codding :)

  • 1
    not best practice imo – Radu Oct 2 '18 at 11:31
  • 1
    In most cases if you have to use an SQL query you're probably doing it wrong. – ol'bob dole May 3 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! – Juliano Vargas Aug 13 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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