Magento is very flexible, so there are numerous ways how you can achieve the desired result. The only problem is to determine the best way to do it, i.e. find the most reliable and effective way.
Two approaches can be suggested here. The choice depends on the technical details of the required functionality, which are not stated in the initial question:
- The custom tab will contain only basic fields, used for entering data
for a product
- The custom tab will contain advanced fields, and/or javascript, and/or other custom HTML
markup
Let's see the solution for both cases.
#1. The tab will contain only basic fields, used for entering data for a product
In such a case it is enough to use Magento's attributes mechanism. It allows to create attributes (fields) for a product, apply them to certain product types only, and separate the fields into groups (tabs).
This is how a script may look like.
<module_dir>/sql/install-1.0.0.0.php
<?php
/* @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = $this;
// Add attribute
$allowedProductTypes = array(
Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE,
Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE,
);
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'attribute_for_tab', array(
'label' => 'Attribute For Tab',
'apply_to' => implode(',', $allowedProductTypes),
'type' => 'varchar',
'input' => 'text',
'default' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'user_defined' => true,
'visible' => true,
'required' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'is_configurable' => false
));
// Add group
$installer->addAttributeGroup(Mage_Catalog_Model_Product::ENTITY, 'Default', 'My Attribute Tab');
// Add attribute to set and group
$installer->addAttributeToSet(Mage_Catalog_Model_Product::ENTITY, 'Default', 'My Attribute Tab', 'attribute_for_tab');
Feel free to download simple module example for this approach: "attribute_tab.zip"
#2. The custom tab will contain advanced fields, and/or javascript, and/or other custom HTML markup
In such a case the tab should be created as a usual Magento block. And injected into the Tabs renderer via layout.
Custom tab block <module_dir>/Block/Adminhtml/Catalog/Product/Edit/Tab/Custom.php
<?php
class Zerkella_CustomTab_Block_Adminhtml_Catalog_Product_Edit_Tab_Custom
extends Mage_Adminhtml_Block_Widget implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
/**
* Class constructor
*
*/
public function __construct()
{
parent::__construct();
$this->setTemplate('zerkella_customtab/catalog/product/edit/tab/custom.phtml');
}
/**
* Get tab label
*
* @return string
*/
public function getTabLabel()
{
return Mage::helper('zerkella_customtab')->__('My Custom Tab');
}
/**
* Get tab title
*
* @return string
*/
public function getTabTitle()
{
return Mage::helper('zerkella_customtab')->__('My Custom Tab');
}
/**
* Check if tab can be displayed
*
* @return boolean
*/
public function canShowTab()
{
$allowedProductTypes = array(
Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE,
Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE,
);
$productType = $this->_getProduct()->getTypeId();
return in_array($productType, $allowedProductTypes);
}
/**
* Retrieve product
*
* @return Mage_Catalog_Model_Product
*/
protected function _getProduct()
{
return Mage::registry('current_product');
}
/**
* Check if tab is hidden
*
* @return boolean
*/
public function isHidden()
{
return false;
}
}
The layout file app/design/adminhtml/default/default/layout/zerkella_customtab.xml:
<?xml version="1.0"?>
<layout>
<adminhtml_catalog_product_new>
<reference name="product_tabs">
<action method="addTab">
<name>my_custom_tab</name>
<block>zerkella_customtab/adminhtml_catalog_product_edit_tab_custom</block>
</action>
</reference>
</adminhtml_catalog_product_new>
<adminhtml_catalog_product_edit>
<reference name="product_tabs">
<action method="addTab">
<name>my_custom_tab</name>
<block>zerkella_customtab/adminhtml_catalog_product_edit_tab_custom</block>
</action>
</reference>
</adminhtml_catalog_product_edit>
</layout>
Note: if the product types, having custom tab, are fixed, then you can put them statically in the layout file, instead of checking them dynamically in block's canShowTab()
method.
Here is the layout file for such a sample case, when product types with custom tab are fixed and include Downloadable only, app/design/adminhtml/default/default/layout/zerkella_customtab.xml:
<?xml version="1.0"?>
<layout>
<adminhtml_catalog_product_downloadable>
<reference name="product_tabs">
<action method="addTab">
<name>my_custom_tab</name>
<block>zerkella_customtab/adminhtml_catalog_product_edit_tab_custom</block>
</action>
</reference>
</adminhtml_catalog_product_downloadable>
</layout>
The rest is simple - everything you put into zerkella_customtab/catalog/product/edit/tab/custom.phtml
will be rendered in the tab.
You can download simple module example for this approach: "custom_tab.zip"
Also I would not recommend to use class rewrites to implement the task. The approaches, described above, cover all the developer's needs. There is no sense to utilize rewrites. While rewrites is a powerful feature, which allows to do anything in Magento, it has two restrictions:
- A class can be rewritten by one module only
- If there are rewritten classes in your system, then more work will be needed in order to
upgrade Magento to a newer version
The proposed approaches follow a natural way of customizing product tabs in Magento, so it is better to choose one of them.
Good luck with your store :)