Magento2 Category Attribute – Custom Category Attribute Created but Not Saved

attributescategory-attribute

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?

Best Answer

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();
Related Topic