Magento provides a way to add product attribute from admin but sometime we need to add category attribute in our category settings. So here I am going to tell you the way we need to create category attribute programmatically.E.g. I am creating one age restriction attribute for some categories. Before start we need to be clear about the files we needed to create attribute. Below are the folders and files we will create -
1) app/code/local/Evon/CategoryAttr/etc/config.xml
2) app/code/local/Evon/CategoryAttr/sql/custom_attribute/mysql4-install-0.1.0.php
3) app/etc/modules/Custom_CategoryAttr.xml
1) First we will create module file Custom_CateogryAttr.xml in "app/etc/modules/" folder with the following content -
<?xml version="1.0"?>
<config>
<modules>
<Custom_CateogryAttr>
<active>true</active>
<codePool>local</codePool>
</Custom_CateogryAttr>
</modules>
</config>
2) Then we will create config.xml in "app/code/local/Custom/CategoryAttr/etc/" folder with the following content -
<?xml version="1.0"?>
<config>
<modules>
<Custom_CategoryAttr>
<version>0.1.0</version>
</Custom_CategoryAttr>
</modules>
<global>
<resources>
<category_attribute>
<setup>
<module>Custom_CategoryAttr</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</category_attribute>
<category_attribute_write>
<connection>
<use>core_write</use>
</connection>
</category_attribute_write>
<category_attribute_read>
<connection>
<use>core_read</use>
</connection>
</category_attribute_read>
</resources>
</global>
</config>
3) Then we need to create sql file which will enter attribute in database with name mysql4-install-0.1.0.php in "app/code/local/Custom/CategoryAttr/sql/category_attribute/" folder with the following content -
<?php
$this->startSetup();
$this->addAttribute( Mage_Catalog_Model_Category::ENTITY, 'custom_attribute', array(
'group' => 'General Information',
'input' => 'select',
'type' => 'int',
'label' => 'Age Restriction',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => false,
'option' => array ('values' => array(
0 => 'No',
1 => 'Yes',
)
),
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
Now after executing the file you will see the Age Restriction attribute under General Information tab in category detail page.
0 Comment(s)