how to get custom category attributes by id : Magento 2

4.9k Views Asked by At

I have category ID and need to get all custom attribute for example thumbnail image.

my code does not return all attributes

$category = $this->categoryRepository->get($childId, $this->_storeManager->getStore()->getId());
$category->getData();
2

There are 2 best solutions below

0
On

Try below code:

$categoryId = 5;
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$object_manager = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
echo "<pre>";
print_r($object_manager->getData());
die('shasha test');

I hope it will help...!!!

2
On

You can use Category's CollectionFactory class and select all attributes by using a star (*) symbol in addAttributeToSelect method. You can use this code example below in your class.

protected $_categoryFactory;

public function __construct(
    // ...
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $collecionFactory,
    ) {
        // ...
        $this->_categoryFactory = $collecionFactory;
}

public function yourFunctionName()
{
    $catId = 3; // your category id        
    $collection = $this->_categoryFactory
                    ->create()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('entity_id',['eq'=>$catId])
                    ->setPageSize(1);

    $catObj = $collection->getFirstItem();
    $thumbnail = $catObj->getThumbnail(); // it should return value if attribute name is thumbnail
    $catData = $catObj->getData(); // dump this line to check all data

    // ...
}