0

How to get category attribute value by option id in magento 2.

Thanks

2
  • Which option ID? is it custom attribute?
    – Keyur Shah
    Commented Feb 1, 2018 at 14:53
  • @KeyurShah please find this link prnt.sc/i8p7tj
    – Raghu
    Commented Feb 1, 2018 at 15:04

1 Answer 1

1

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:

  • Replace category_brand with your category attribute code.
  • $categoryObj is Categroy Object

For 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.

2
  • i have only brand id , so i want brand label by brand value(id)
    – Raghu
    Commented Feb 3, 2018 at 10:50
  • yes but in attribute from where you are showing values in the form. you must have specified the source model for this Commented Feb 3, 2018 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.