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?
I tested with Magento 1.6.1.0 . I think it work with version 1.6.2 .
You just do as I note, it’ll work well
This looks like a nice and decent technique.
Does it also work with magento community 1.6.2?
I am trying to imlement it but there is nothing being saved into the database nor is there anything in the category config page in admin.
Ok Haney, for that you need to review the class fie “Mage_Adminhtml_Block_Catalog_Category_Tab_Design” and the _prepareLayout() function in the same. From this file you can get the idea to how to add the tab in the Catalog –> Category management.
Thanks Senthil. But don’t want to assign products to multiple categories instead would like to assign categories to other categories.
I’ll talk about a real time scenario. Say I have 3 categories: Men, Women and Hoods. In Men category, there are shirts, hats and hoods and the same for Women category. But when you click on Hoods, it should show all the hoods from Men and Women categories.
Hi Haney,
You can assign products for multiple categories, also the same will give the above expected output. So no need to do add the tab. If you want further details. Please follow me and ask your comments on http://sankai-senthil.blogspot.com.
I’m looking for to add a new tabpage under category in magento admin, which loads the category grid. Idea is to associate multiple categories to one category and on the front end, when you click on that particular category, it should show products from all the associated categories. Is there any better solution then create a new tabpage with categories grid?
If not then can anybody help me to guide how to create a new tabpage under category in admin…
@senthil thank you for reply.
Adminβ> Catalog->Categories has just one record – root derictory (id = 2). But DB table catalog_category_entity has two records:
entity_id | 1 | 2 |
entity_type_id | 3 | 3 |
attribute_set_id | 0 | 3 |
parent_id | 0 | 1 |
…
path | 1 | 1/2 |
…
And another question – what setter setImportedCatId should do?
Hi Anthony,
You can find the “Category Id” in Admin–> Catalog->Categories [For Community].
Admin–> Catalog->Categories->Manage Categories [For Enterprise].
Thx for the nice article. Colud you describe one point pls. You wrote Mage::getModel(‘catalog/category’)->load(1)… and Mage::getModel(‘catalog/category’)->load(2). 1 & 2 – it is some IDs. But which table it has?
How to add some test product with some required fields Magento store via sql_setup script.
How to add new category in your Magento store via sql_setup script.
Your post has helped me figure out something in my custom module for which I was wrecking my brains since days. I am trying to add an extra field to review_detail table for which my module is overriding Review Model files. But the data is not getting saved to the table because there is no Setup.php in Review/Model files in core. Any pointers on this one?
Can you please tell that how can i add an option in front-end , so that a user can add a category.
I am trying to get this to work in Magento 1.6.1 with no joy. I have tried just about every variation I am seeing on this page and the only thing that I can see even being kind of an issue is that all of the tables in 1.6 have “mage_” as a prefix. Any thoughts on this would be greatly appreciated.
-Thanks
Hi Karthick ,
Go to Manage Productsβ>Click select All present in the grid and in the right top corner of the listing grid you can see βActionsβ from there select βUpdate Attributresβ it will take to a page which have three tabs in the left panel from there click inventory there you can see stock option. Please set Qty greater than Zero and update. Thats All. Also you can manage the stock for each product by Edit and click on inventory you have the same option. The Error cause for not listing the product is may be the βOut of stockβ. And also some other cause may due to not map the products with the Active category . Please check that too.
All the best.
Hi i’m using magento 1.6.0… Modern theme… i was able to add the product category in the dashboard but not getting it in home page… what i do…? im new to Magento will you help me?
I found the problem:
I forgot settings the ‘backend’ => ‘eav/entity_attribute_backend_array’
and I changed ‘user_defined’ => true
I have added a multiselect attribute for categories as Bozo suggested. The attribute display normally in categories, but it is not saved when I save the category.
Here is the attribute I added:
$installer->addAttribute(‘catalog_category’, ‘display_item’, array(
‘label’ => ‘Item to display’,
‘type’ => ‘varchar’,
‘input’ => ‘multiselect’,
‘default’ => ”,
‘class’ => ”,
‘backend’ => ”,
‘frontend’ => ”,
‘source’ => ‘xcategory/Valuelist’,
‘global’ => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
‘visible’ => true,
‘required’ => false,
‘user_defined’ => false,
‘searchable’ => false,
‘filterable’ => false,
‘comparable’ => false,
‘visible_on_front’ => false,
‘visible_in_advanced_search’ => false,
‘unique’ => false
));
Please help me to save the attribute when I save the categories. Thanks a lot.
Hi
How i can show only subcategories with attribute?
Hi, I have one really good query, can you guys tell me how to add one new field in front end property.
Someone just ask me if I can add one new field in his front end property which will remain as text field.?
previous one is misleading . Please review this
I have found the problem and i have used this
I have found the problem and i have used this
Companynamwe_Yourmodulename
Mage_Catalog_Model_Resource_Eav_Mysql4_Setup
core_setup
It is not working in Enterprise edition. Please help me
i am struggling nearly 2 days.
Thanks. This Post saved me a lot of headaches.
Can you tell how much time you’ve spent to write this snippet? Especially with importing products it’s sometimes a staggering amount of time I spent to get it running :-/
i found it… i forgot to adapt this little piece of code:
it should be
to play with varchar attributes…
thanks again, guys!
hi,
thanks a LOT for the hints! with your help I managed to set up a multiselect attribute for additional static blocks to be attached to categories!
BUT:
it appears the data is not saved correctly. while catalog_category_entity_varchar table receives the correct values, all I get in catalog_category_flat_store_1 table is the value ‘1’, no matter what I enter.
here’s the code I used for the multiselect attribute:
$installer->addAttribute('catalog_category', 'cat_blocks', array(
'label' => 'Blocks',
'type' => 'varchar',
'input' => 'multiselect',
'default' => '',
'class' => '',
'backend' => 'eav/entity_attribute_backend_array',
'frontend' => '',
'source' => 'hme_xtracms/entity_attribute_source_blocks',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false
));
the source model brings up the correct values. any idea what I might be missing?
thanks a lot for your help!
I am newbie to Magento development
I have created a new module for this on app/code/local/company/catalog. In this I have added 2 directory for ./etc/config.xml and ./sql/catalog_setup/mysql4-install-1.0.0.php. I have also added a file to app/etc/modules/company_catalog.xml but it still does not seem to work. Can someone please help.
Wasted almost 2 days on doing this instead of directly changing the database.
@myself:
I needed to add the New_Attribute.xml in etc/modules. After refreshing the cache the new attribute was added π
Thanks for this script. I’ve created my own to add an attribute to a category. Now my question is, how do i execute this or does Magento do this by itself?
At the moment I don’t see the new attribute π
how to update the custom attribute value programatically to some default value on creation or updation of the category.
Ahhh, of course! Thanks David… sweeeeet indeed! π
Actually, Tony, just figured out that if you re-index your store (specifically the Category Flat Data table), you’ll end up with your EAV fields in the category info. You may have to disable/re-enable the Flat Catalog Categories flag first (that’s when mine started working).
Sweet!
Tony, it’s probably because you have Flat Catalog Category (in System -> Config -> Catalog) set to Yes. Just ran into that myself.
Anyone have any suggestions on how to best add fields to Flat Catalog Categories? Or do we need to alter the structure of the flat table in the db?
Hi,
I have your code implemented on a 1.5.0.1 store, the attribute appears in the admin and saves fine. However when I come to access the attribute it does not work. I have the following code…
I can see by using the following code that my model does not contain the attribute..
Any ideas why that might happen?
Thank!!
There is an error in the xml. the closing tag is wrong.
@Vedran,
This is very simple tip Thanks. Can u please tell me how can I add an attribute in “Display setting” tab. I have to change the GROUPID or ATTRIBUTESET_ID?
how i can add a new parent category with setup script in php/magento?
Thank you for the tutorial. I’m a beginner. Can you explain in which files i have to put the code? Thanks
@Lucas Gabriel everything that you can see on the last picture π
Man, I did not understand what is the MySQL setup script, can you help me?
Great list. Thanks for this useful article.
@Newbie
not necessarily..you can create “update” sql_setup script of existing module and just increse the nuber of module version.
@Byron
It’s developed and tested on 1.5.0.1
I don’t believe that there is too much difference between those 2 version so it should work. Try again.
Thanks Vedran for the write up. For some reason, I am unable to get this to work properly on v1.5. I followed your instructions to the letter (including fixed a typo / should be ). Not sure what I missed.
Hi i am very much new to magento.so please bear with me..
Should I create a new module for this?
Nice post. To be honest I’ve never needed to use much more than Category title, description and image, however this is a pretty clever script.
Love the simplicity of it too.
Thanks Vedran. Probably makes sense to use some sort of prefix to reference the site to make it unique.
@Natalie
one of this posts will help you:
https://inchoo.net/ecommerce/magento/how-to-extend-magento-order-grid/
or
https://inchoo.net/ecommerce/magento/how-to-add-custom-renderer-for-a-custom-column-in-magento-grid/
@Neil
If the extension coded well and you know how to update (upgrade) Magento then it won’t be any problem.
Problem with attributes is that you cannot have 2 with same name/identifier.
Very nice work Vedran. Do these sort of changes cause any problems when it is time to update Magento?
Many thanks for the useful article!
Could you tell me – where to read how to add a column to Category admin product tab (for example – ‘is_in_stock)?