Retrieve the category attribute details information by code in Magento 2, data will be attribute code, title, id, backend_type, source_model and many more.
You can Retrieve specific attribute details by the interface class, Magento\Catalog\Api\CategoryAttributeRepositoryInterface.
Use public function get($attributeCode) method to retrieve the information with below simple code of snippets,
<?php namespace Jesadiya\Category\Model; use Exception; use Magento\Catalog\Api\Data\CategoryAttributeInterface; use Magento\Catalog\Api\CategoryAttributeRepositoryInterface; class CategoryAttributeInfo { /** * @var CategoryAttributeRepositoryInterface */ private $categoryAttributeInfo; public function __construct( CategoryAttributeRepositoryInterface $categoryAttributeInfo ) { $this->categoryAttributeInfo = $categoryAttributeInfo; } /** * Category attribute information * * @param string $categoryAttributeCode * @return CategoryAttributeInterface */ public function getCategoryAttribute($categoryAttributeCode) { $categoryAttributeData = []; try { $categoryAttributeData = $this->categoryAttributeInfo->get($categoryAttributeCode); } catch (Exception $exception) { throw new Exception($exception->getMessage()); } return $categoryAttributeData; } }
Using above code snippets, You can retrieve only Category attibute details, if you try with other attribute code like product or customer, it will throws error.
Call from the template or PHP class to retrieve attribute details,
$categoryAttributeCode = "is_anchor"; $categoryAttribute = $this->getCategoryAttribute($categoryAttributeCode); var_dump($categoryAttribute->debug());
Output:
Magento\Catalog\Api\Data\CategoryAttributeInterface