Create a category attribute in Magento

From time to time you might need to create an attribute which isn’t linked to a particular product, but instead is linked to a category. A recent example (for me) was creating a block of overlay text to float above the category image.

Create a Category Attribute in Magento


But it’s not as simple to create category attributes in Magento as it is to create product attributes – but with a simple script it’s not particularly difficult.

<?php
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' => 'text',
    'label'=> 'Your attribute label text',
    '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', 'ATTRIBUTECODE', $attribute);
$installer->endSetup();
?>

It should go without saying that the ATTRIBUTECODE in the second to last line should be whatever you want your attribute’s code to be

Save this file as a PHP file, upload it to the web root of your site, hit it in a browser and then look in the admin panel and you should see your new category attribute ready to use at the bottom of the General Information tab.