Magento CE 1.7.0.2 – Add Extra Category Attribute Under General Information Tab

categoryce-1.7.0.2

I am trying adding a extra category attrbiute to general infomation tab i have tried adding that using the following code ,

require_once("app/Mage.php");
Mage::app('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

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


$installer->addAttribute('catalog_category', 'nav_left',  array(
    'type'     => 'tinyint',
    'label'    => 'Show in left navgigation',
    'input'    => 'boolean',
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'default'           => 0
));

$installer->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'nav_left',
    '11'

//last Magento's attribute position in General tab is 10
);

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

$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')}`;
");

This is just working fine but this is adding an additional information tab named General just to the right of general infomation tab i have tried adding it to first tab using attributeGroupId set to 4 but after testing it is just crashing the site.

Any idea how can i add that attribute to first tab.

Best Answer

Try it like this:

$installer->addAttribute('catalog_category', 'nav_left', array(
    'group'         => 'General Information',
    'type'     => 'int',
    'label'    => 'Show in left navgigation',
    'input'    => 'boolean',
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'default'           => 0
)); 

EDIT
$installer must be instance of Mage_Catalog_Model_Resource_Setup.

Off topic a bit: I recommend adding this script in an update file of one of your modules instead of making an instance of Mage::app() and running it 'on the fly'. If you put it in an upgrade script it's portable to other instances.

Related Topic