How to get category attribute value by option id in magento 2.
Thanks
You can use the below code custom Category Attribute Value By Option Id:
$optionArray = [];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$attribute = $objectManager->create('Magento\Eav\Model\Config')->getAttribute('catalog_category', 'category_brand');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
foreach ($options as $option) {
$optionArray[$option['value']] = $option['label'];
}
}
$categoryBrandId = $categoryObj->getData('category_brand');
$categoryBrandLabel = $optionArray[$categoryBrandId];
In above code:
category_brand
with your category attribute code.$categoryObj
is Categroy ObjectFor example purpose, I have used ObjectManager
. you can directly create dependency for Magento\Eav\Model\Config
class in your class constructor and use the code.