I was looking for a code in order to create category attributes in Magento and found a post from Mukesh Chapagain where he explains in a really easy way how to do so. But it didn’t work for me. I’m supposing that the problem is the way how we call the installer in the mysql php file. So I want to explain here, step by step, how I did to make it work:

Create the module

Create a file YourCompany_CategoryAttribute.xml file with the following code and place it in your app/etc/modules folder:

<config>
  <modules>
    <YourCompany_CategoryAttribute>
      <active>true</active>
      <codePool>local</codePool>
    </YourCompany_CategoryAttribute>
  </modules>
</config>

Now create the folder structure for your module. Basically, you need to create the folders: app/code/local/YourCompany/etc and app/code/local/YourCompany/sql/categoryattributesetup. In the first one you have to place a config.xml with this content:

<?xml version=&quot;1.0&quot;?>
<config>
    <modules>
        <YourCompany_CategoryAttribute>
            <version>0.1.0</version>
        </YourCompany_CategoryAttribute>
    </modules>
    <global>
            <resources>
                <categoryattribute_setup>
                  <setup>
                    <module>YourCompany_CategoryAttribute</module>
                    <class>Mage_Eav_Model_Entity_Setup</class>
                  </setup>
                  <connection>
                    <use>default_setup</use>
                  </connection>
                </categoryattribute_setup>
            </resources>
    </global>
</config>

Finally, create the mysql php file that is going to create the category attribute:

<?php

$this->startSetup();

$this->addAttribute('catalog_category', 'new_attribute', array(
    'group'         => 'General',
    'input'         => 'text',
    'type'          => 'varchar',
    'label'         => 'New attribute',
    'backend'       => '',
    'visible'       => 1,
    'required'      => 0,
    'user_defined'  => 1,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

$this->endSetup();

?>

This will create a new tab on the category edit page named «General» and inside it you’ll find the new attribute name «New attribute».


0 Comments

Leave a Reply

Avatar placeholder

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.