How to add new custom category attribute in Magento

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!

You made it all the way down here so you must have enjoyed this post! You may also like:

Adding gallery tab to a CMS page Antun Martinovic
Antun Martinovic, | 5

Adding gallery tab to a CMS page

How I learned to stop worrying and love 3rd party modules for Magento Luka Rajcevic
Luka Rajcevic, | 2

How I learned to stop worrying and love 3rd party modules for Magento

Solving problems with Category tree database information Nikola Stojiljkovic
Nikola Stojiljkovic, | 19

Solving problems with Category tree database information

83 comments

  1. I want to upload multiple images for categories without using static blocks. How can we do that through coding blocks?

  2. wordfull article..!
    but I want to add wisywig editor.
    Please see following code:

    $attribute1  = array(
        'group'         => 'General',
         'input'         => 'textarea',
         'type'          => 'text',
         'label'         => 'Custom attribute',
         'backend'       => '',
         'visible'       => true,
         'required'      => false,
         'wysiwyg_enabled' => true,
         'visible_on_front' => true,
         'is_html_allowed_on_front' => true,
         'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    );
    $installer->addAttribute('catalog_category', 'news', $attribute1);

    but its not working for me. πŸ™
    please help…

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

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

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

    Please help me out.
    Thanks

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

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

    <?xml version="1.0"?>
    <config>
        <modules>
            <Pleasures_Catattributes>
                <version>1.0</version>
            </Pleasures_Catattributes>
        </modules>
        <global>
    		<resources>
    			<catattributes_setup>
    			  <setup>
    				<module>Pleasures_Catattributes</module>
    				<class>Mage_Eav_Model_Entity_Setup</class>
    			  </setup>
    			  <connection>
    				<use>core_setup</use>
    			  </connection>
    			</catattributes_setup>
    			<catattributes_setup_write>
    			  <connection>
    				<use>core_write</use>
    			  </connection>
    			</catattributes_setup_write>
    			<catattributes_setup_read>
    			  <connection>
    				<use>core_read</use>
    			  </connection>
    			</catattributes_setup_read>
    		</resources>
        </global>
    </config> 

    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)

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

    in app\etc\modules\Pleasures_Catattributes.xml

    <?xml version="1.0" encoding="utf-8"?>
    <config>
        <modules>
    		<Pleasures_Catattributes>
    			<active>true</active>
    			<codePool>local</codePool>
    		</Pleasures_Catattributes>
        </modules>
    </config>

    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->getData('highlight_cat');
            } else {
                $url = Mage::getModel('catalog/category')->load($category->getId())
                    ->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

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

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

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

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

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

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

  14. 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();
  15. 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

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

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

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

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

  20. 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?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.