Magento2: How to fetch value of custom attribute added to Category entity?

Created on 10 May 2017  路  3Comments  路  Source: magento/magento2

Preconditions


  1. Magento2 CE version 2.1
  2. PHP 7.1
  3. Minimal installation so that it runs, no sample data, no custom theme.
  4. Using default theme (luma)

Steps to reproduce


1. Create your own custom module that does the following:
- adds custom attribute to category entity
This non-required attribute is type int with source model value of Magento\Eav\Model\Entity\Attribute\Source\Boolean, default value is 1.
- add the custom attribute to the Category form, by overriding category_form.xml
- create Plugin that intercepts Magento\Catalog\Helper\Category::getCategoryUrl

Expected result

  1. Ideally $category->getCustomAttribute('blah_is_clickable') returns something instead of null
    but I don't know what the "right" way is.
    Using getCustomAttribute is just a guess on my part.
    Magento2 stores my custom attribute value in the catalog_category_entity_int table.
    I can see that it gets updated correctly when I modify the given category in the admin ui.

Actual result

  1. $category->getCustomAttribute('blah_is_clickable') always returns null

Since Magento2 stores my category custom attribute in catalog_category_entity_int table,
what is the correct way to fetch the value given an instance of
Magento\Catalog\Model\Category?

Most helpful comment

@routbiplab I ended up finding my own solution, forgot to close this ticket.
I created an event listener for the catalog_category_collection_load_before event.
In my observer class I added my custom attribute to the collection.
$collection->addAttributeToSelect('is_clickable');

The missing piece of the puzzle was that I didn't add my custom attribute to the Category entity's default attribute set. Drove me crazy trying to figure out why the collection was missing Category entities.

Also the official docs discourage the use of ObjectManager, and I believe the AbstractModel::load method is deprecated (in favour of repository classes).

All 3 comments

@VladimirZaets ping

I am assuming that you need the custom attribute (e.g 'description') value in your template file. Then you simply need to use this :
$_category->getData('description'); where
$_category = $objectManager->create('MagentoCatalogModelCategory')->load($currentCategoryId);

@routbiplab I ended up finding my own solution, forgot to close this ticket.
I created an event listener for the catalog_category_collection_load_before event.
In my observer class I added my custom attribute to the collection.
$collection->addAttributeToSelect('is_clickable');

The missing piece of the puzzle was that I didn't add my custom attribute to the Category entity's default attribute set. Drove me crazy trying to figure out why the collection was missing Category entities.

Also the official docs discourage the use of ObjectManager, and I believe the AbstractModel::load method is deprecated (in favour of repository classes).

Was this page helpful?
0 / 5 - 0 ratings