Create a custom category attribute in Magento

I’ve run into a few posts about this thing but none of them was working out of the box so I took all the info and glued them together so…

if you’ve to create a custom attribute for a Magento category but you don’t have a module (and its installer script) simply create a php file in the project’s root with this code:

require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$installer = new Mage_Sales_Model_Mysql4_Setup;
$attribute  = array(
	'type' => 'int',
	'label'=> 'Your attribute label',
	'input' => 'text',
	'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
	'visible' => true,
	'required' => false,
	'user_defined' => true,
	'default' => "",
	'group' => "General Information"
);
$installer->addAttribute('catalog_category', 'your_attribute_code', $attribute);
$installer->endSetup();

This attribute is a “text”, more info may come in comments if you need.

5 thoughts on “Create a custom category attribute in Magento

  1. Ethhan

    Thanks, your blog helped me out a lot. As an FYI for any future readers, if you want to make a multiselect attribute from an existing source model (in my case the customer groups) you’ll need to change input to ‘multiselect’ and have the following values in the attribute array:

    ‘source’ => ‘customer/customer_attribute_source_group’,
    ‘backend’ => ‘eav/entity_attribute_backend_array’,
    ‘frontend’ => ”,

  2. raj

    hi,
    How to use more than one attribute.i tried
    $this->startSetup();
    $this->addAttribute(‘catalog_category’, ‘custom_attribute’, array(
    ‘group’ => ‘General Information’,
    ‘type’ => ‘int’,
    ‘label’ => ‘View Product or Add to Cart’,
    ‘input’ => ‘select’,
    ‘global’ => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    ‘visible’ => true,
    ‘required’ => false,
    ‘user_defined’ => false,
    ‘default’ => 1,
    ‘source’ => ‘eav/entity_attribute_source_boolean’
    ));
    $this->addAttribute(‘catalog_category’, ‘custom_attribute’, array(
    ‘group’ => ‘General Information’,
    ‘type’ => ‘int’,
    ‘label’ => ‘test’,
    ‘input’ => ‘select’,
    ‘global’ => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    ‘visible’ => true,
    ‘required’ => false,
    ‘user_defined’ => false,
    ‘default’ => 1,
    ‘source’ => ‘eav/entity_attribute_source_boolean’
    ));
    $this->endSetup();
    second one dint work.

Leave a Reply

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