Magento Tips & Tricks

Magento category attributes dependency

There is no default way to create dependent category attributes in magento admin. Dependency can be easily added to system configuration inputs or custom magento forms, but not for category attributes.

We are going to create simple attribute dependency by adding new input renderer for attribute. It will work this way:
You have several attributes:
– my_attribute
– my_attribute_text
– my_attribute_select

Note that they all start from my_attribute.
First attribute has boolean type. When it is set to true – other attributes that start from my_attribute is visible.

Source can be found on Github

1. How to add category attribute ?

Adding new category attribute is pretty easy task, but it require several steps.

First you need to tell magento that your extension will use install script. Add this code in extension config app/code/community/Company/Extension/etc/config.xml

Second you need to create install script and add following code
app/code/community/Company/Extension/sql/company_extension_setup\mysql4-install-1.0.0.php

More information about creating category attributes can be found here:
http://www.atwix.com/magento/add-category-attribute/
http://inchoo.net/magento/how-to-add-new-custom-category-attribute-in-magento/

2. Add our input renderer

Now we will create new input renderer class to inject our javascript.
app/code/community/Company/Extension/Block/Adminhtml/Catalog/Category/Dependency.php

3. Create dependency javascript class and add it to admin layout.

Create new file called js/company/category_attribute_dependency.js
It will create new class that will track for attribute state and show / hide other attributes based on main attribute value.

4. Add javascript to admin layout

– declare our layout xml updates in extension config

– create app/design/adminhtml/default/default/layout/company/extension.xml

5. Update install script to add our input renderer

6. Cache issues

You might now see your new attributes on category edit page. In this case you can manually run reindex ( System > Index Management ). Or you can modify install script to reindex data programmatically

More info on magento reindex process can be found here
http://www.kennydeckers.com/magento-programatically-reindexing-indexes/
http://www.codetweet.com/magento/reindexing-magento-programmaticaly/

Congratulations

You’re done. Source can be found on Github

Troubleshooting

If you want to reinstall the extension or remove category attributes – you can use following SQL

License

Our freebies are released under the terms of the MIT license.

The MIT License is simple and easy to understand and it places almost no restrictions on what you can do with our freebie item.

You are allowed to: use any NWDthemes freebie plugin in your project (even commercial projects) as long as the copyright header is left intact.

You MUST NOT:
Provide our freebies on any other site as is.
Sell our freebies.

4 thoughts on “Magento category attributes dependency

  1. Nice tutorial! A couple points:

    For setup class, it’s generally recommended to use your own, rather than rely on another module. In your setup script you then use the class which makes sense, e.g.:

    $installer = Mage::getResourceModel('catalog/setup','catalog_setup');

    This is an academic point though, as the Catalog setup class is unlikely to go away. If you want to couple your module to the Catalog setup class it would make more sense to specify Mage_Catalog_Model_Resource_Setup rather than Mage_Catalog_Model_Resource_Eav_Mysql4_Setup though.

    The other point is regarding the MIT license. AFAIK (might be mistaken) your restrictions (“You MUST NOT…”) are meaningless if your code is MIT licensed. In any event, you need to add the actual MIT license to your repository.

    Thanks for putting this out there, I’m sure that many will find it helpful.

    • Thanks for the comment.

      Update code to use Mage_Catalog_Model_Resource_Setup

      Regarding license – you might be right. I will check it

    • This tutorial is for categories. I would advise you to start debugging from mysql install script

      $this->addAttribute(‘catalog_category’

Leave a Reply

Your email address will not be published. Required fields are marked *