Add a Custom Category Attribute

A really common task in Magento is to add custom attributes for EAV entities that is not possible to do it in Magento admin. Like for the catalog_category entity. Let's see how you can add a custom category attribute in Magento 2.

Like in most of our previous posts, we create a new module for that post called Blog17. The main thing that we need to add a new custom category attribute is an install script. So under Blog17 folder, we create a new folder called Setup and inside we create an install script called InstallData with the following code:

Before we continue to add our attribute to the category form in Magento admin, let's have a look on the code above. So the most important parameters for the addAttribute function that we use, is the entity type (in our example the catalog_category), the attribute code (seo_description) and an array of attribute option. 

To add our custom category attribute to the category form in Magento admin, we will need to extend the category_form.xml which can be found under

/vendor/magento/module-catalog/view/adminhtml/ui_component/category_form.xml

 To achoeve that we have to create an xml file called category_form.xml under 

/app/code/MageVision/Blog17/view/adminhtml/ui_component/

and add the following code

With the code above, we add our new custom attribute, the SEO Description which has the same attribute options with the Magento's default description attribute, in the tab Content and under the description attribute as is shown in the screen below.

The full example as extension can be found here.

Feel free to share this post and ask your questions in the comments below.

Till next time!