Request Quote

Get option label from option id and attribute code magento 2

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

Initialize product factory

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

Now retrieve option label

$attribute = $poductReource->getAttribute($attributeCode);
if ($attribute->usesSource()) {
    return  $option_Text = $attribute->getSource()->getOptionText($optionId);

where $attributeCode and $optionId are available.


Happy coding 🙂

. . .


Add Your Comment

Be the first to comment.

Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again