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());
  • Any system or exception log? – Rohan Hapani Aug 29 '19 at 9:29
  • no, it returns some attributes for example id , created_at, url but not all – Dito Khelaia Aug 29 '19 at 9:53

You need to get your category first like this

use Magento\Catalog\Api\CategoryRepositoryInterface;
class ...
    protected $_storeManager;
    protected $categoryRepository;
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository,
    ) {
        $this->_storeManager = $storeManager;
        $this->categoryRepository = $categoryRepository;

     public  function getCategory()
            $category = $this->categoryRepository->get($categoryId, $this->_storeManager->getStore()->getId());


and after this load your attributes from model like this

$attributes = $category->getAttributes();
  • yes works, but how can I get only one attribute from this variable? – Dito Khelaia Aug 29 '19 at 10:03
  • Try this $category->_getAttribute($attributeCode); – Gatteka Aug 29 '19 at 10:16
  • not working ... invalid method – Dito Khelaia Aug 29 '19 at 10:20
  • $category>getIsCollectionPageListing(); where IsCollectionPageListing will be your attribute name – Gatteka Aug 29 '19 at 10:21

You can't print getData() because it's not part of that categoryRepository class. If you want to get all attributes then, you need to follow this below code:

$category = $this->categoryRepository->get($childId, $this->_storeManager->getStore()->getId());
$attributes = $category->getAttributes(); //It's object. So, you need to get value by foreach
foreach($attributes as $value)
    echo $value->getName();

If you want to get single attribute value. Then, you can get by this below code :

For ex : echo $category->getData('name');

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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