Jun 19 2009

Create Custom Category Attributes with phpMyAdmin (Tutorial)

Category: Categories, DatabaseJeff @ 3:46 pm

The following is a brief tutorial for creating a custom category attribute. This has only been tested by creating a textfield-type attribute in Magento’s version 1.3.2. However, to the best of my current knowledge, it should work in any versions 1.2+ and for any type of attribute. Note: changing the attribute type will require a couple changes to the SQL below. So on we go…

  1. Open your Magento database using phpMyAdmin.
  2. We will first find the proper attribute_id for your new attribute.  Run the following SQL statement and keep the resulting number handy: 
    SELECT MAX(attribute_id) + 1 from eav_attribute
  3. From the left pane, click the eav_attribute table link.
  4. Click Export tab link near the top of the page.
  5. Ensure the Data box is checked then click the Go button.
  6. Scroll through the list of attributes on your page and find the one that has a second comma delimited value of 3 and a third comma delimited value of meta_description.
  7. Copy that entire row, excluding the comma at the end (i.e from opening parenthesis to closing parenthesis).
  8. Click your browser’s back button.
  9. Now run the following SQL command:
    INSERT INTO eav_attribute VALUES

    followed by the text you copied about the meta_description.  Don’t click the Go button just yet - let’s alter that command a bit:

  10. Change the first comma delimited number value to your new attribute_id value (found in step 2 above).  Then change the value from meta_description to some unique identifier for your attribute.  Then change the value that reads Meta Description to a proper label for your new attribute.  Now you can click Go to run the command.
  11. The new attribute is created.  Now we need to ensure it has a proper sort order.  For this tutorial we will place it at the end of the list under the category’s General Information tab in the admin panel.  Run the following SQL command and keep the resulting number handy as it will be the new attribute’s sort order:
    SELECT MAX(sort_order) + 1 from eav_entity_attribute where attribute_set_id = 3
  12. Now run the following SQL command, but replace the last two fields with the values from step 2 and step 11: 
    INSERT INTO eav_entity_attribute ( entity_type_id, attribute_set_id, attribute_group_id, attribute_id, sort_order ) VALUES ( 3, 3, 3, [your new attribute_id], [your new sort_order] )

And that should do it.  If you bounce into your admin panel, you should see your new attribute for any given category - new or edit.  I’ll let you figure out how to use it from there ;)

14 Responses to “Create Custom Category Attributes with phpMyAdmin (Tutorial)”

  1. Matt says:

    Absolutely brilliant article! Short and sweet. Thanks :)

  2. Matt says:

    Just one heads up, at least on my Magento install, the entity_type_id for “catalog/category” is 9, not 3. And my appropriate attribute_set_id is 12 instead of 3. And finally, my “General Information” attribute_group_id is 7 and not 3.

    Also, the attribute_id column in eav_attribute is auto_increment, so I don’t think you need to calculate the MAX(attribute_id) + 1.

    Your post certainly helped me find where to go!

  3. Muza says:

    Thanks, that was very helpful.. Please keep posting Magento development tricks :)

  4. Jacob says:

    This works great; thank you for sharing! I’ve just bookmarked this blog. :)

  5. Nirnajan says:

    It is great for text box but I needed it for select box. Is there any solution. I need it.
    Thanks In advance.

  6. Mal says:

    This was very helpful, thanks! Now, I just need to determine how to display it on the category page as a block of text just above the footer. Any pointers are welcome!

  7. anand says:

    thank you soooo much bro…..
    i was searching this for last 2days…. n jz found this.. hours b4 ma deadline… thank you… :D :D

  8. anand says:

    n i added an image gallery…. we can add anything we need, jz look the result of the 1st export.. n we can easly identify,.. n change the values accordingly

  9. soumraky says:

    Many thanks! Brief and efficient. For an alternative, there is also a modular way to do this, without going through phpmyadmin: http://www.magentix.fr/modules-magento/ajouter-categorie-attributs-champs-personnalises.html

  10. Chris says:

    Hi Jeff,

    This is a very clear tutorial, however I am running version 1.4 and even though the updates went through without errors, my attribute is only showing up in “Manage Attributes” and not on the category page as intended.

    What I mean is that its listed under “General” in Attribute Sets, but it doesn’t show up on the category admin page…

    …any ideas? Anyone? :)

    Thanks,
    Chris

  11. Demar says:

    Hi Chris,
    I had the same problem. Since version 1.4 you need to add a row in “catalog_eav_attribute” and it works! Insert the attribute id from the added row in eav_attribute and fit the other colomns to your needs.

  12. Sam says:

    Great tutorial.

    To retrieve the new category attribute use:-

    $_category->getData(’your_attribute_code’);

  13. Charly Bronson says:

    I have mag ver. 1.4.0.1. This worked, but there is a little extra work involved. Just to recap, follow the instructions above and stop after you complete step 10. Like Demar said, go into “catalog_eav_attribute” and follow the same steps (1-10). In my particular case, in “eav_attribute” I was replicating “id_37″ which is the image field (I needed an extra image for my categories). I was able to locate the same row with “id_37″ in “catalog_eav_attribute”. Once you create the new row, then run steps 11 & 12. If you do steps 11 & 12 WITHOUT creating the new row in “catalog_eav_attribute”, you’ll get an error. Hope this helps.

  14. Dhara says:

    I have magento enterprise version 1.8 and i also had the same problem until i add the row in “catalog_eav_attribute” table. now it works fine… Thanks, this tutorial helped a lot.. :)

Leave a Reply