Magento E-commerce Platform Q/ A

How to Add a New Tab to a Category in Magento

To add a new magento category tab (manage categories page/ admin area), rewrite or simply copy the file from: core/Mage/Adminhtml/Block/Catalog/Category/Tabs.php to: local/Mage/Adminhtml/Block/Catalog/Category/Tabs.php.

Next in _prepareLayout method after this code:

$this->addTab(’products’, array( 
‘label’ => Mage::helper(’catalog’)->__(’Category Products’), 
‘content’ => $this->getLayout()->createBlock(’adminhtml/catalog_category_tab_product’, ‘category.product.grid’)->toHtml(), 
));

 

Add this one:

$this->addTab(’new_tab’, array( 
‘label’ => Mage::helper(’catalog’)->__(’New Category Tab’), 
‘content’ => $this->getLayout()->createBlock(’yourmodule/adminhtml_category_form’)->toHtml(), 
));

 

The parameter 'content' refers to a block, this block needs to be added into the module you are working with, here's the example of the block content:

<?php class Namespace_Module_Block_Adminhtml_Category_Form extends Mage_Adminhtml_Block_Widget_Form {     protected function _prepareForm()     { 		$form = new Varien_Data_Form(); 		$this->setForm($form); 	         $fieldset = $form->addFieldset('custom_category_tab_form', array('legend'=>Mage::helper('catalog')->__('Custom Tab')));                 $fieldset->addField('anytext', 'text', array(             'label'     => Mage::helper('catalog')->__('Any Text'),             'name'      => 'anytext',         ));      return parent::_prepareForm();     } }

 

After that in order to have the possibility to save added tab fields into the table of the module, you need to add an event observer. Add it into the section 'global' of the config.xml file:

<events>
			<catalog_category_prepare_save >
				<observers>
					<yourmodule>
						<type>singleton</type>
						<class> yourmodule /observer</class>
						<method>categorySave</method>
					</ yourmodule >
				</observers>
			</catalog_category_prepare_save >
		</events>

 

And add the model of the observer into 'models' folder of the module:

<?php
 class Namespace_Module _Model_Observer
{
    public function categorySave($observer){
	$params = $observer->getRequest()->getParams();
	// now you could save your custom category params to your db table
    }
}