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.

19 comments

  • Neha

    How to use more than one attribute

    • Giles Bennett (author)

      Neha,

      Run the script more than once, changing it as you go.

      Kind regards,

      Giles

  • Simon

    Hi,

    If I have ran that script a few times now with different attribute codes how do I remove the unwanted ones from the database?

    • Giles (author)

      Simon,

      Using the attribute code you want to remove you can probably just do this (note, not tested) :

      < ?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;
      $installer->removeAttribute(‘catalog_category’, ‘ATTRIBUTECODE’);
      $installer->endSetup();
      
  • Alex

    Hi Giles,
    Please tell me
    How to create category image attribute ?

    • Giles (author)

      Alex,

      It’s not that tricky – try looking for the “type” option where the attribute is created.

      Kind regards,

      Giles

  • gaurav

    Hi i am able to create a category attribute but i am not able to add editor on textarea.
    please check my code

    setCurrentStore(Mage::getModel(‘core/store’)->load(Mage_Core_Model_App::ADMIN_STORE_ID));
    //$installer = new Mage_Sales_Model_Mysql4_Setup;
    $installer = new Mage_Eav_Model_Entity_Setup(‘core_setup’);
    $attribute = array(
    ‘type’ => ‘text’,
    ‘label’=> ‘Your attribute label text’,
    ‘input’ => ‘textarea’,
    ‘visible’ => true,
    ‘required’ => false,
    ‘global’ => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    ‘wysiwyg_enabled’ => true,
    ‘visible_on_front’ => true,
    ‘is_html_allowed_on_front’ => true,
    ‘user_defined’ => true,
    ‘default’ => “”,
    ‘group’ => “General Information”

    );

    $installer->addAttribute(‘catalog_category’, ‘ATTRIBUTECODE’, $attribute);
    $installer->endSetup();
    ?>

    how we resolve it ???
    its create a textarea attribute but not add editor over their

    • Giles (author)

      If it’s created the attribute (and you should probably have replaced “ATTRIBUTECODE” in the penultimate line with what you actually wanted the attribute to be), then if the WYSIWYG editor isn’t displaying then it’s most likely that it’s disabled in the configuration.

      Kind regards,

      Giles

  • Imran Sohail

    Works great. i tried lot of tutorials to add category attribute, but this is the only one that worked.

  • Loren McDonald

    This is really great. I am in the process of moving a site onto Magento and had decided on extra on-page coding to parse psudo-attributes entered in the description field. Doing it this way will save me hours of work.

    But how would I go about modifying this to add an attribute as a radio or drop down selection with predefined values instead of just a text field? This would help eliminate spelling errors or typos.

    Lastly, this might be going a bit too far for this (maybe in a new post you could answer the question) but is it possible to add a list of the CMS blocks to choose from as a category attribute?

    • Giles (author)

      Loren,

      You can specify a different attribute type (we just used text because a textbox is the most frequently used) when creating the attribute, so could use textarea, select, or multi-select (radio isn’t a possibility, I don’t think). Not sure about the predefined values – in searching to see if there’s any guidance, though, I did come across this (free) module which may help!

      http://www.magentocommerce.com/magento-connect/manage-category-customer-and-customer-address-attributes.html

      I suspect it’s unlikely that you’d be able to add a list of CMS blocks, because the attribute’s options would have to be specified when the attribute is created, and the list of CMS blocks is open to change. I would suggest having a text attribute and storing the CMS block’s attribute value in there instead.

      Hope that’s useful,

      Giles

      • Loren McDonald

        Next question: how do I get the value of the new attribute in a template?

        • Giles (author)

          Loren,

          If you’ve got the current category loaded into, say, $category, and your new attribute is “attribute” then you should be able to do that using :

          echo $category->getData('attribute');
          

          Kind regards,

          Giles

          • Loren McDonald

            Perfect!
            All I was finding was code that did not work for me. This however does.

          • Giles (author)

            No problem – glad to be of assistance.

            Kind regards,

            Giles

  • Benj

    Thanks!

    The last time I did this I had to create a module and it was really hard to work with, This is much easier.

    Nice one

    • Giles (author)

      Benj,

      You’re welcome.

      Kind regards,

      Giles

  • Danijel

    Thanks,
    after searching over the internet I saved a lot of time. 🙂

    • Giles (author)

      You’re welcome – glad I could help!

      Giles

Leave a Reply

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

Want to talk to us about your project?