Magento – Cant create a custom category attribute in admin

attributescategorymagento-1.7

I used the link http://www.magentocommerce.com/magento-connect/custom-attributes-4340.html and i can create customer attribute and also customer address attribute , but i can not create category attribute.

When i went throw the code i found in the "CategoryController.php", in the saveAction function wrote like bellow

            Mage::getSingleton('eav/config')
            ->getAttribute($this->_type, $attribute->getAttributeCode())
            ->setData('used_in_forms', $data['customer_form'])
            ->save();

When i print the $data by print_r($data['customer_form']), i could not find any 'customer_form' in the $data. So is there any alternative way to add the custom category attribute dynamically.

Best Answer

$attributeUtility = Mage::getModel('core/setup');

$attributeUtility->addAttribute('catalog_category', 'attribute_code',
    array(
        'sort_order' => '0',
        'backend_type' => 'int',
        'input' => "boolean",
        'label' => 'Custom Attribute Label',
        'source_model' => 'eav/entity_attribute_source_boolean',
        'required' => '0',
        'user_defined' => false,
        'default_value' => '0',
        'unique' => '0',
        'note' => '',
        'global' => '1',
        'visible' => '1',
        'searchable' => '0',
        'filterable' => '0',
        'comparable' => '0',
        'visible_on_front' => '0',
        'html_allowed_on_front' => '1',
        'used_for_price_rules' => '0',
        'filterable_in_search' => '0',
        'used_in_product_listing' => '0',
        'used_for_sort_by' => '0',
        'configurable' => '0',
        'apply_to' => 'configurable',
        'visible_in_advanced_search' => '0',
        'position' => '0',
        'wysiwyg_enabled' => '0',
        'used_for_promo_rules' => true,
        'search_weight' => '1',
    )
);

You'll need to keep in mind that your 'source_model' needs to match a data type that makes sense for your attribute. The other values below do various things that need to be looked into for your implementation. The first parameter in the 'addAttribute' call is 'catalog_category' this maps out to a value from the database table eav_entity_type. For more information on EAV attributes see: Advanced ORM - by Alan Storm

Related Topic