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 🙂
Be the first to comment.