In today’s blog, we are showing how to get an option value based on the option id in Magento 2, without loading the product.
First of all load the AttributeRepositoryInterface via constructor wherever you require this feature.
protected $attributeRepository;
.......
public function __construct(
\Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository,
...
)
{
$this->attributeRepository = $attributeRepository;
}
Next, load the attribute and fetch the option value from the attribute object.
$attribute = $attributeRepository->get('4', 'product_brand');
$optionValue = $attribute->getSource()->getOptionText($brand->getOptionId());
That’s it. Hope it helps!
Did this post help you?
Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply