Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DEV

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 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 🙂

. . .

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