HummingbirdUK main logo

HummingbirdUK

Coding solutions to business problems

About us

We use code to create solutions to business challenges, bottle-necks and headaches.

If you think your business has a problem that can be solved through code, we are happy to chat things through without any obligation.

Get in touch

Create a category attribute in Magento

Home / Blog / Create a category attribute in Magento

Written by Giles Bennett

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] <?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(); ?> [/php] 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.
Author : Giles Bennett

About the author

Giles Bennett built his first website in 1996, and is old enough to miss Netscape Navigator. Initially a lawyer, he jumped ship to IT in 2008, and after 5 years as a freelancer, he founded HummingbirdUK in 2013. He can be reached by email at giles@hummingbirduk.com.