In magento sometimes we are required to create category attributes programmatically in admin panel.
Lets see how we can do it:
1. Create a module with namespace and modulename.
2. Then Create a file config.xml in etc folder in our module.
2. Then Create a installer script file mysql4-install-0.0.1.php in Sql folder at the path Namespace/Modulename/Sql/namespace_modulename_setup/mysql4-install-0.0.1.php in our module.
Now, in our config.xml file at the path Namespace/Modulename/etc/config.xml write the below code in it :
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Modulename>
<version>0.0.1</version>
</Namespace_Modulename>
</modules>
<global>
<resources>
<modulenam_attribute>
<setup>
<module>Namespace_Modulename</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</modulenam_attribute>
<modulenam_attribute_write>
<connection>
<use>core_write</use>
</connection>
</modulenam_attribute_write>
<modulenam_attribute_read>
<connection>
<use>core_read</use>
</connection>
</modulenam_attribute_read>
</resources>
</global>
</config>
In the above code we have defined our module with the version 0.0.1 and
then in global we have called the installer script to run the Sql query from the installer.
Now, in the Installer script in Sql in the file mysql4-install-0.0.1.php write the below code:
<?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 in the above script we defined a custom category attribute with group as General Information , input as select, type as int etc we defined the option values of
it as
array ('values' => array(
0 => 'No',
1 => 'Yes',
)
and assigned it to global scope.
In the this way we can create different category attributes.
0 Comment(s)