How to add new custom category attribute in Magento

Featured Image

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!

75
Top

Care to rate this post?

Author

Vedran Subotic

Ex. Inchooer

Vedran worked in Inchoo from 2009 to 2012 as a backend developer.

Other posts from this author

Discussion 75 Comments

1 2
Add Comment
  1. John

    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?

  2. John

    Sorry, the xml tags didnt seem to render.

    Are the file paths right though?

  3. Haney

    @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.

  4. Konstantin Ivanov

    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.

  5. shubh

    How can i get attribute value of attribute

  6. @shubh

    Could you pls expalin somewhat clear.

  7. sunil

    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

  8. prasad

    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.

  9. Hy,

    To create a new attribute for a product you could use the following snippet inside your install script

    $this->startSetup();
    $this->addAttribute('catalog_product', 'id_of_attribute', array(
        'group'                    => 'General',
        'type'                     => 'int',
        'input'                    => 'select',
        'label'                    => 'Attribute_Label',
        'global'                   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // or other scope
        'visible'                  => 1,
        'required'                 => 0,
        'visible_on_front'         => 0,
        'is_html_allowed_on_front' => 0,
        'is_configurable'          => 0,
        'source'                   => 'eav/entity_attribute_source_boolean', //select the appropriate source
        'searchable'               => 0,
        'filterable'               => 0,
        'comparable'               => 0,
        'unique'                   => false,
        'user_defined'             => true,
        'is_user_defined'          => true,
        'used_in_product_listing'  => true
    ));
    $this->endSetup();
    

    I hope it helps you

  10. Iwo

    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”):

    $installer = $this;
    $installer->startSetup();
    $installer->addAttribute('catalog_category', 'priority', array(
        'type' => 'varchar',
        'label' => 'Priority',
        'input' => 'text',
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'visible' => true,
        'required' => false,
        'user_defined' => false,
        'default' => ''
    ));
    $installer->endSetup();
    
  11. Yaroslav

    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

  12. Anshuman

    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

  13. Lee

    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.

  14. Tom

    certainly useful. thank you. I am also looking for a solution to assign an attribute of downloadable PDF to each product.

  15. 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.

  16. Dimitris

    is there a way to work with this extra field through REST API?
    Coz i give a try with no success.

  17. 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

  18. Dimitris

    what do you Mean Punnet?
    what action to change?
    its not very clear to me at least

  19. Jeremy Bass

    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

  20. Jeremy Bass

    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

    &lt;?xml version=&quot;1.0&quot;?&gt;
    &lt;config&gt;
        &lt;modules&gt;
            &lt;Pleasures_Catattributes&gt;
                &lt;version&gt;1.0&lt;/version&gt;
            &lt;/Pleasures_Catattributes&gt;
        &lt;/modules&gt;
        &lt;global&gt;
    		&lt;resources&gt;
    			&lt;catattributes_setup&gt;
    			  &lt;setup&gt;
    				&lt;module&gt;Pleasures_Catattributes&lt;/module&gt;
    				&lt;class&gt;Mage_Eav_Model_Entity_Setup&lt;/class&gt;
    			  &lt;/setup&gt;
    			  &lt;connection&gt;
    				&lt;use&gt;core_setup&lt;/use&gt;
    			  &lt;/connection&gt;
    			&lt;/catattributes_setup&gt;
    			&lt;catattributes_setup_write&gt;
    			  &lt;connection&gt;
    				&lt;use&gt;core_write&lt;/use&gt;
    			  &lt;/connection&gt;
    			&lt;/catattributes_setup_write&gt;
    			&lt;catattributes_setup_read&gt;
    			  &lt;connection&gt;
    				&lt;use&gt;core_read&lt;/use&gt;
    			  &lt;/connection&gt;
    			&lt;/catattributes_setup_read&gt;
    		&lt;/resources&gt;
        &lt;/global&gt;
    &lt;/config&gt;
    

    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)

    &lt;?php
    $installer = $this;
    $installer-&gt;startSetup();
    $installer-&gt;addAttribute('catalog_category', 'highlight_cat',  array(
        'type'     =&gt; 'int',
        'label'    =&gt; 'Highlight',
        'input'    =&gt; 'select',
        'source'   =&gt; 'eav/entity_attribute_source_boolean',
        'global'   =&gt; Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'required' =&gt; false,
        'default'  =&gt; 0
    ));
    $installer-&gt;endSetup();
    

    in app\etc\modules\Pleasures_Catattributes.xml

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;config&gt;
        &lt;modules&gt;
    		&lt;Pleasures_Catattributes&gt;
    			&lt;active&gt;true&lt;/active&gt;
    			&lt;codePool&gt;local&lt;/codePool&gt;
    		&lt;/Pleasures_Catattributes&gt;
        &lt;/modules&gt;
    &lt;/config&gt;
    

    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

    if ($category instanceof Mage_Catalog_Model_Category) {
                $url = $category-&gt;getData('highlight_cat');
            } else {
                $url = Mage::getModel('catalog/category')-&gt;load($category-&gt;getId())
                    -&gt;getData('highlight_cat');
            }
    

    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

  21. Lampix

    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();

  22. Vishal

    I need to create hidden attribute and I am setting ‘visibility’ = false, but attribute is created as visible.

    Please help me out.
    Thanks

  23. 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

  24. Ruvini

    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.

  25. Vishal

    HI
    i would like to add custom category attribute in ‘Sort by’.
    Can i do that .

1 2

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top