How to add new custom category attribute in Magento

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:
Enjoy coding!
83 comments
How to filter category with custom attribute.
So, do i need to reinstall again or not?
is this code works fine with magento 1.9.2.2
Please Check Below Comment that have if you can added select box.
http://myphpinformation.blogspot.in/2014/09/create-new-custom-category-attribute-in-magento.html
How to set sort order?
‘sort_order’ => 10
but it not worked
This might also be necessary:
php indexer.php –reindex catalog_category_flat
I want to upload multiple images for categories without using static blocks. How can we do that through coding blocks?
wordfull article..!
but I want to add wisywig editor.
Please see following code:
but its not working for me. π
please help…
HI
i would like to add custom category attribute in ‘Sort by’.
Can i do that .
Thanks Lampix.
Your code works like a charm and I wish I found this a couple of days ago.
This is the quickets way to add a new category attribute.
ADD below code in app\code\local\Meteorify\Customcatattrb\sql\customcatattrb_setup\mysql4-install-0.0.1.php
startSetup();
$attribute = array(
‘type’ => ‘text’,
‘label’=> ‘Bottom Description’,
‘input’ => ‘textarea’,
‘global’ => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
‘visible’ => true,
‘required’ => false,
‘user_defined’ => true,
‘default’ => “”,
‘group’ => “General Information”
);
$installer->addAttribute(‘catalog_category’, ‘bottom_description’, $attribute);
$installer->endSetup();
?>
———————————————————-
app\code\local\Meteorify\Customcatattrb\etc\config.xml
0.0.1
Meteorify_Customcatattrb
Mage_Eav_Model_Entity_Setup
default_setup
——————————————————
app\etc\modules\Meteorify_Customcatattrb.xml
true
local
I need to create hidden attribute and I am setting ‘visibility’ = false, but attribute is created as visible.
Please help me out.
Thanks
If you just want to add an attribute to a category tab you don’t have to use a module (testet in 1.7.0.2). Paste the following code (adapt it to your needs… look at Mage_Catalog_Model_Resource_Setup at line 87+ for reference) at the beginning of any .phtml file and call it once. (I used catalog/category/view.phtml so the code gets executed when I view a category in the frontend, but the choice is really up to you… Delete the code afterwards)
$setup = Mage::getModel(‘catalog/resource_setup’, ‘core/resource’);
$setup->startSetup();
$setup->addAttribute(“catalog_category”, “default_sort_dir”, array(
‘type’ => ‘varchar’,
‘label’ => ‘Default Product Listing Sort Direction’,
‘input’ => ‘select’,
‘source’ => ‘catalog/category_attribute_source_sortdir’,
‘required’ => false,
‘sort_order’ => 51,
‘global’ => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
‘group’ => ‘Display Settings’
));
$setup->endSetup();
Got it to work but itβs not like the suggested code here.. This is what I had to do but I’d like to know why this is off from the post. Cheers
Making a highlight category yes/no bool
CompanyName = Pleasures
ModuleName = Catattributes
in app\code\local\Pleasures\Catattributes\etc\config.xml
in app\code\local\Pleasures\Catattributes\sql\catattributes_setup\mysql4-upgrade-0.1-1.0.php
(note i had to use upgrade so that i could get it installed on the existing site)
in app\etc\modules\Pleasures_Catattributes.xml
Than .. it worked but never could i get any combo with the posted to work. and if you want to access it you can do this
I’m only saying what it took to get a stable set of file to upload to a 1.7.2 version of magneto CE not that I warranty anything anyone does of the feedback I’m providing
Any chance that you could post the example files? Trying to follow this is requiring a lot of fill in the blanks. Personally I’m a learn from seeing the whole thing kind of guy. Really interested in learning what to do. Thanks for the how to here. Cheers -Jeremy
what do you Mean Punnet?
what action to change?
its not very clear to me at least
i have create a module and i create a attribute in manage category that is maltipalselect but i don’t know that where can i change save acction
is there a way to work with this extra field through REST API?
Coz i give a try with no success.
Many thnx. This info helped big time. Not all info is correct, id’s have changed in Magento many times over but all in all good info.
certainly useful. thank you. I am also looking for a solution to assign an attribute of downloadable PDF to each product.
Really useful, thanks. Is is possible to use this to allow adding of a file upload.
I need to assign a downloadable PDF to each category. Any ideas greatly appreciated.
For all who have problem in adding attribute to General Information Tab
go eav_entity_attribute table find your attribute id and change attribute_group_id from 3 to 4
Hi, I’m new in magento, as many more and I was looking through the code to see if can fits my needs but for what I see seems to me that it does not. I asked already on Magento forums and on StackOverflow, I post here the link to the question to see if anyone can help: http://stackoverflow.com/questions/11665563/linking-categories-through-different-attributes
I copied all the example and it didn’t work π Nothing new shown up in ‘General Information’ tab. I only managed to create text attribute in “General” tab (NOT “General Information”):
Hy,
To create a new attribute for a product you could use the following snippet inside your install script
I hope it helps you
Hi, i am new to Magento . I am try to add custom attribute to product. I need help to create a custom attribute in product using sql setup.
I need a help regarding custom category attribute
I want to add select dropdown as a custom category attribute
can you tell me the how to do that ?
how to assign values to that dropdown?
Thanks
@shubh
Could you pls expalin somewhat clear.
How can i get attribute value of attribute
And how can I do so in this category, the products have no prices, but to make a client request. I have install question extensions in products, if you can help me for other things – the abolition of purchase button and paste text and only for this category. Thank you in advance.
@Senthil: To create a new tabpage, I copied the core/Mage/Adminhtml/Block/Catalog folder to local/Mage/Adminhtml/Block/Catalog. And then under Catalog folder, in Category/Tabs.php, duplicated the ‘Category Products’ as:
$this->addTab(‘products’, array(
‘label’ => Mage::helper(‘catalog’)->__(‘Category Products’),
‘content’ => $this->getLayout()->createBlock(‘adminhtml/catalog_category_tab_product’, ‘category.product.grid’)->toHtml(),
));
to:
$this->addTab(‘categories’, array(
‘label’ => Mage::helper(‘catalog’)->__(‘Associated Categories.’),
‘content’ => $this->getLayout()->createBlock(‘adminhtml/catalog_category_tab_category’, ‘category.category.grid’)->toHtml(),
));
At the end, I literally duplicate the local/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php to local/Mage/Adminhtml/Block/Catalog/Category/Tab/Category.php. In Category.php, instead of:
$collection = Mage::getModel(‘catalog/product’)->getCollection()
I am loading category collection as:
$collection = Mage::getModel(‘catalog/category’)->getCollection()
Now it is showing me ‘Associated Categories tabpage under categories and it loads categories but they don’t load as I’m expecting. When I click on add new category, it shows all the categories under ‘Associated Categories’ but when I click on any of the categories, they disappear. I want to be able to link any category to any other category. Also on clicking ‘Reset Filter’ button, it loads the products.
Sorry, the xml tags didnt seem to render.
Are the file paths right though?
Just to make sure:
app\etc\modules\AL_CategoryAttributes.xml:
true
local
app\code\local\AL\CategoryAttributes\config.xml:
0.1.0
CategoryAttributes
Mage_Catalog_Model_Resource_Eav_Mysql4_Setup
core_setup
core_write
core_read
The mysql setup file:
app\code\local\AL\CategoryAttributes\sql\categoryattributes_setup\mysql4-install-0.1.0.php
Does is look correct?