Add a extra category attribute under general infomation tab
-
16-10-2019 - |
Question
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.
Solution
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.
OTHER TIPS
I have managed it work in expected way like this.
$installer->addAttribute('catalog_category', 'left_nav', array(
'group' => 'General Information',
'type' => 'int',
'label' => 'Show in left navigation',
'input' => 'select',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => 0,
'source' => 'eav/entity_attribute_source_boolean'
));
Thanks
You can custom yes/no attribute to category section using the following code.
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'featured_product', array(
'group' => 'General Information',
'input' => 'select',
'type' => 'text',
'label' => 'Featured Product',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'source' => 'eav/entity_attribute_source_boolean',
));
Please refer my tutorial for step by step explanation and file structure. http://www.pearlbells.co.uk/add-custom-attribute-dropdown-category-section-magento/