5

I've created a custom extension in order to add a custom attribute on product Categories. Here's the code I've used to create it:

$installer = $this;
$installer->startSetup();
$attribute  = array(
    'type' => 'int',
    'label'=> 'Featured Author',
    'input' => 'checkbox',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible' => true,
    'required' => false,
    'user_defined' => false,
    'default' => 0,
    'source' => "eav/entity_attribute_source_boolean",
    'group' => "General Information"
);
$installer->addAttribute('catalog_category', 'featured_author', $attribute);
$installer->endSetup();

The attribute is created and it's visible on backend. I also checked eav_attribute database table and the attribute is created in there too.

But changing the attribute's value (check/uncheck) and then saving category has no effect at all. The attribute value is not updated/saved. The checkbox is always unchecked.

What should I do to fix this?

2
  • 4
    But is there a value in the db for your attribute in catalog_category_entity_int? As a general advice. Try to avoid checkboxes and radio buttons in the backend forms. They don't play well with Magento. Use instead a yes/no attribute and have a dropdown. The problem with checkboxes is that the value is not sent when the checkbox is unchecked. With selects you will have the value 0.
    – Marius
    Commented Oct 10, 2014 at 14:30
  • 1
    Marius i am agree with you.i am suggesting... ktsixit to use yes no attribute..
    – Amit Bera
    Commented Oct 10, 2014 at 17:33

2 Answers 2

6

Use the field yes/no instead of checkbox

    $installer =new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup;
    $installer = $this;
    $installer->startSetup();

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

    $installer->addAttribute('catalog_category', 'featured_author',  array(
        'type'     => 'int',
        'label'    => 'Featured Author',
        'input'    => 'select',
        'source'   => 'eav/entity_attribute_source_boolean',
        'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'required' => false,
        'default'  => 0
'group' => "General Information"
    ));

    $installer->addAttributeToGroup(
        $entityTypeId,
        $attributeSetId,
        $attributeGroupId,
        'featured_author',
        '10'
    );

    $attributeId = $installer->getAttributeId($entityTypeId, 'featured_author');

    $installer->run("
    INSERT INTO `{$installer->getTable('catalog_category_entity_int')}`
    (`entity_type_id`, `attribute_id`, `entity_id`, `value`)
        SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1'
            FROM `{$installer->getTable('catalog_category_entity')}`;
    ");

    $installer->endSetup();
3
  • Thank you for this answer. I have already inserted the code you suggested but the selectbox appears emply, without any option available. How can I populate the selectbox with options line "Yes" and "No"?
    – zekia
    Commented Oct 15, 2014 at 9:42
  • I changed some attribute values directly in database, and it worked. But do you know why when changing the code has no impact on attribute? I re-indexed website and cleared cache. Is there something else I should have done to apply changes?
    – zekia
    Commented Oct 15, 2014 at 9:48
  • i need check .see that later
    – Amit Bera
    Commented Oct 15, 2014 at 9:51
2

I have checked a very simple solution to add new attribute in category form in Magento admin.

I found 3 tables are responsible for that.

Step 1:- Create an attribute from your admin panel.

Step 2:- Go to database and open eav_attribute table, find the attribute with attribute_code. Now make entity_type_id 4 to 3

Step 3:- Go to eav_entity_attribute table insert a row entity_type_id =3 , attribute_set_id = 3, attribute_group_id=4, attribute_id = your attribute id.

Step 4:- Now go to the catalog_eav_attribute table put is_global = 1, is_visible=1.

Now refresh your category edit page it will come

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.