Skip to content

Adding a category attribute in Magento

November 10, 2011

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="1.0"?>
<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».

 

About these ads

From → Extensions

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: