Sometimes you need to extend functionality of Magento categories. There is several ways to do that, I will show you how it can be done.
You can do that by modifying and adding data into some of tables directly, but it can be waste of time if you don’t know what you are doing.
This post will describe how you can add new custom category attribute in your Magento store via sql_setup script.
MySQL setup script will look something like this, it depends on your needs and how you would like to configure your new attribute:
$installer = $this;
$installer->startSetup();
$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttribute('catalog_category', 'new_cat_attrb', array(
'type' => 'int',
'label' => 'New Category Attribute',
'input' => 'text',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => 0
));
$installer->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'new_cat_attrb',
'11' //last Magento's attribute position in General tab is 10
);
$attributeId = $installer->getAttributeId($entityTypeId, 'new_cat_attrb');
$installer->run("
INSERT INTO `{$installer->getTable('catalog_category_entity_int')}`
(`entity_type_id`, `attribute_id`, `entity_id`, `value`)
SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1'
FROM `{$installer->getTable('catalog_category_entity')}`;
");
//this will set data of your custom attribute for root category
Mage::getModel('catalog/category')
->load(1)
->setImportedCatId(0)
->setInitialSetupFlag(true)
->save();
//this will set data of your custom attribute for default category
Mage::getModel('catalog/category')
->load(2)
->setImportedCatId(0)
->setInitialSetupFlag(true)
->save();
$installer->endSetup();
In your config.xml file of your module you will need to add this part in order to install correctly your new category attribute:
<resources>
<new_attribute_csv_setup>
<setup>
<module>New_Attribute</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</new_attribute_setup>
<new_attribute_setup_write>
<connection>
<use>core_write</use>
</connection>
</new_attribute_setup_write>
<new_attribute_setup_read>
<connection>
<use>core_read</use>
</connection>
</new_attribute_setup_read>
</resources>
Pay attention on class tag here, class must be: “Mage_Catalog_Model_Resource_Eav_Mysql4_Setup”.
If you did this correctly, you will get something like this:
Post from inchoo.net