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 ๐
Categories:
Magento2
Tags:
option label
View Comments
Comment or Ask a Question
Quick Links