cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 get custom category attribute

SOLVED

Magento 2 get custom category attribute

I have created a new category attribute using a setup script and now it is showing on the backend and assigned values (it is just a boolean attribute). the attribute name on EAV entity table is

is_home_category

 .And now I'm trying to get its values on frontend

 

 

$categories = $this->getStoreCategories(true, true, true);

foreach ($categories as $category):
   echo $category->getIsHomeCategory()
endforeach;

But it only returns NULL , how can I get my custom category attribute on the frontend 

 

_______________________________________________
An Expert in Anything Was Once a Beginner
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2 get custom category attribute

Instantiate a category collection manually and call its addAttributeToSelect method.

View solution in original post

3 REPLIES 3

Re: Magento 2 get custom category attribute

Instantiate a category collection manually and call its addAttributeToSelect method.

Re: Magento 2 get custom category attribute

thank you very much I have inject 

\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection

and able to load category collection with 

public function getCategoryCollection()
    {
        $collection = $this->_categoryCollection->create()
            ->addAttributeToFilter('is_home_category', '1');

        return $collection;
    }

and Now I can call 

getIsHomeCategory()

 

method but I can not get category name by getName() do you know how to get category name ? 

_______________________________________________
An Expert in Anything Was Once a Beginner

Re: Magento 2 get custom category attribute

my bad Smiley Tongue it should be 

 

public function getCategoryCollection()
    {
        $collection = $this->_categoryCollection->create()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('is_home_category', '1');

        return $collection;
    }

and thanks again 

_______________________________________________
An Expert in Anything Was Once a Beginner