Netismine

Tips & Tricks

Add category attribute in module installation

It’s often required during custom development of a module to add an attribute to category. You could always hack your client’s database, but that’s not the right way to do it, especially ’cause Magento has support for it in it’s API. So the best way to do it is to install it during module’s DB installation. Here’s a code that will generate a new category attribute when put into install-* or update-* file in *_setup folder.

$installer = $this;
$installer->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'size_chart', array(
        'type'              => 'text',
        'backend'           => '',
        'frontend'          => '',
        'label'             => 'Size Chart',
        'input'             => 'textarea',
        'class'             => '',
        'source'            => '',
        'global'            => '0',
        'visible'           => true,
        'required'          => false,
        'user_defined'      => true,
        'default'           => '',
        'searchable'        => false,
        'filterable'        => false,
        'comparable'        => false,
        'visible_on_front'  => true,
        'used_in_product_listing' => false,
        'unique'            => false,
        'wysiwyg_enabled'   => true,
        'apply_to'          => '',
        'is_configurable'   => true
    ));
 
$installer->addAttributeToSet(Mage_Catalog_Model_Category::ENTITY, '3', '4', 'size_chart','6');
$installer->addAttributeToGroup(Mage_Catalog_Model_Category::ENTITY, '3', '4', 'size_chart','6');
select all

Now, I’m not sure if you need both addAttributeToSet and addAttributeToGroup calls, and am also pretty sure that there’s a more clever way to mark attribute set id and attribute group id than ’3′ and ’4′, so if you find it, do leave a comment.

2 comments

2 Responses to Add category attribute in module installation

  1. Courtney says:

    Adding the attribute to a set and group is not required, but adding it to a group in this way is the only way I’ve found to set it’s position. If all you care about is the group you can say:

    'group' => 'General',

    in the addAttribute function

    These should get you the Entity type id, attribute set id, and attribute group id.


    $entityTypeId = $installer->getEntityTypeId('catalog_category');
    $attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
    $attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

    Then you can add them like this:


    $installer->addAttributeToSet(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'custom_attribute_name' );

    However, attributes added this way are not accessible via a normal $category->getCustomAttribute() call. You must first fully load the category model.

    EX: $this->getCurrentCategory->getName() will return the category name. But you must fully load it to get your custom attribute. Do you know how to add a custom attribute that is loaded by default?

  2. Gavin says:

    Did you get anywhere with this and the ability to call the attribute?

    We were able to use the below bit of code but it seems bloated.

    foreach ($this->getStoreCategories() as $_main_category):
    $categoryForcefully = Mage::getModel("catalog/category")->load($_main_category->getEntityId());
    echo $categoryForcefully->getsize_chart();
    endforeach;</code

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=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">