Back to Top

Get option label from option id and attribute code magento 2

Updated 26 March 2024

In this blog, we will see how to obtain option label from attribute code and option id in magento 2.

Initialize product factory

public function __construct(
    \Magento\Catalog\Model\ResourceModel\ProductFactory $productFactory
) {
    parent::__construct();
    $this->productFactory = $productFactory;
}

Now retrieve the option label

$poductReource=$this->productFactory->create();
$attribute = $poductReource->getAttribute($attributeCode);
//checking if attribute exists and it uses source model for options
if ($attribute && $attribute->usesSource()) {
    return  $option_Text = $attribute->getSource()->getOptionText($optionId);
}

where $attributeCode and $optionId are available.

Happy coding 🙂

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

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


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home