Create Custom Category Attribute – The Unofficial Step-By-Step Guide
The Problem
Magento does not allow the creation of category attributes. For whatever reason, there is no way to do this in the Admin. Sometimes it would be nice to have the ability to display additional information on a per category basis. Or even use it for calculations or what have you.
The Solution
This simple solution will help you create an attribute for a category and display it on your category view page. Pay close attention, as your Magento setup is most likely different than what I show here. Also be sure to back up your database prior to making ANY changes.
Step 1: Creating A New Attribute
First off, we need to create a new attribute. Simply do so in the Admin under Catalog > Attributes > Manage Attributes. I'll call my attribute category_test but you can call it anything you like. Keep in mind that it must be unique and can't contain spaces or non alphanumeric characters. Set the Scope to what you need. If you have a multi-store setup and want it to be different for each store, leave it at Store View. Otherwise set it to Website or Global depending on your needs. Set Apply To to All Product Types. Specify all the Attribute Properties but set all Frontend Properties except Allow HTML Tags on Frontend to "No".
Specify at least the Admin label under the Manage Label / Options tab. This is what will be displayed as the title in the backend as well as each store view on the frontend.
Please note that you will not be able to make any changes to this attribute through the backend after you continue with step 2. Double check to make sure your settings are correct.
Save the attribute and write down the attribute_id. You can find the attribute's id by hovering over the row in the grid and reading it from the link which is displayed in the tool tip. My attribute_id is 954. (Yours will be different, of course)
Step 2: Looking Up the Correct Attribute Entity Type ID, Attribute Set ID and Attribute Group ID In the Database
First we need to open the Magento database in a database management tool, like phpMyAdmin, and open the table eav_entity_type. Search for the row containing catalog_category in the field entity_type_code and write down the entity_type_id.
SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_category';
In my case it is 9. Yours will most likely be different, depending on what version of Magento you started your store with.
Now open the table eav_entity_attribute and find a row that has the entity_type_id set to the entity_type_id that we just looked up.
SELECT attribute_set_id, attribute_group_id FROM eav_entity_attribute WHERE entity_type_id = [YOUR entity_type_id here];
Write down the attribute_set_id and the attribute_group_id values. Look at several rows and make sure they contain the same attribute_group_id values, as you could have multiple category attribute groups in your setup.
My setup has three different attribute_group_id's: 7, 166 and 167. These group id's are actually the tabs the attributes appear under when you edit a category. If you have several ID's, pick the lowest attribute_group_id, as this is most likely the "General Information" tab. In my case this is 7.
Step 3: Changing the entity_type_id of the Attribute
Next open the table eav_attribute. Now that we know what entity_type_id our new attributes needs to have, we must change it to the entity_type_id we looked up above.
UPDATE eav_attribute SET entity_type_id = [YOUR entity_type_id] WHERE attribute_id = [YOUR attribute_id];
For me, the SQL statement looks like this:
UPDATE eav_attribute SET entity_type_id = 9 WHERE attribute_id = 954;
Step 4: Creating an Entry In the eav_entity_attribute Table
Now we need to create a new entry in the eav_entity_attribute table. To do so, use your database management tool's editor or use the following SQL statement:
INSERT INTO eav_entity_attribute (entity_type_id, attribute_set_id, attribute_group_id, attribute_id, sort_order) VALUES ([YOUR entity_type_id], [YOUR attribute_set_id], [YOUR attribute_group_id], [YOUR attribute_id], 0);
Use the entity_type_id, attribute_set_id, attribute_group_id and attribute_id values you looked up earlier instead of the placeholders.
For my setup, the SQL statement looks like this:
INSERT INTO eav_entity_attribute (entity_type_id, attribute_set_id, attribute_group_id, attribute_id, sort_order) VALUES (9, 12, 7, 954, 0);
Step 5: Changing the Sort Order
Because we added this attribute to the database by hand, we need to set the proper sort order so it appears in the right place on the category edit page. Go to Catalog > Manage Categories and edit any category in the Admin and you will find the newly created attribute at the very first position. You can change the sort order for your attribute by changing the sort_order field in the eav_entity_attribute table. Keep in mind that the sort_order can have missing and duplicate values.
Displaying the Attribute On the Category View Page (Optional)
Now that we have our custom category attribute, we can display it on the category view page. First we check if the theme you are using already has the file /app/design/frontend/default/YOUR THEME NAME/template/catalog/category/view.phtml. If you can't find the file you must copy it to your theme's /template/catalog/category directory from /app/design/frontend/base/default/template/catalog/category/view.phtml.
Now open the file in your editor of choice and add these lines anywhere you want to display the attribute's value:
<?php $_my_category_attribute = $_category->getData('category_test'); if($_my_category_attribute): echo $_my_category_attribute; endif; ?>
Of course you need to change 'category_test' to whatever you set your attribute code to.
As allways, refresh your cache or disable it while you make changes to ensure you are actually looking at the real thing and not some phantom.
That's it. If you have any trouble, post a comment and I'll try to help you figure it all out.



September 6th, 2011 - 15:35
Very well explained!
I have long sought such a comprehensible description.
Thank you very much!