Wednesday, March 9, 2016

Add Custom Tabs to Magento Product/Customer in Admin Panel


Magento provides the best functionality for managing products from admin panel and also all basic features provide as edit the product. all fields in Magento products group by tab menu. same for customers.
For developer sometimes need to add extra features as a tab in product or customer edit functionality. that time you can create new extension for overwrite Magento functionality or directly edit core files.So here describe basic changes for how to add extra tab menu in product and customer edit screen.
If you want to add product tab in product details than navigate to `app\code\core\Mage\Adminhtml\Block\Catalog\Product\Edit\Tabs.php` file and add below code in `_prepareLayout()` function.

$this->addTab('Custom',array(
'label' =>Mage::helper('catalog')->__('Custom'),
'class' =>   'ajax',
'url'   =>   $this->getUrl('*/*/custom',array('_current'=>true)),
));

same for customer add code as below in `app\code\core\Mage\Adminhtml\Block\Customer\Edit\Tabs.php` file and add own file in 'Tab' folder on same location or create custom action in controller file.
Create new extension for that according to Magento extension structure.first create new `Newsinfo_Customtabs.xml` file in `app\etc\modules\` with below code.

<config>
    <modules>
        <newsinfo_customtabs>
            <active>true</active>
            <codepool>local</codepool>
        </newsinfo_customtabs>
    </modules>
</config>

Create `config.xml` file in `app\code\local\Newsinfo\Customtabs\etc\`with below code.

<?xml version="1.0"?>
<config>
  <modules>
    <Newsinfo_CustomTabs>
      <version>0.1.0</version>
    </Newsinfo_CustomTabs>
  </modules>
  <global>
    <blocks>
      <customtabs>
        <class>Newsinfo_Customtabs_Block</class>
      </customtabs>
    </blocks>
    <models>
      <customtabs>
        <class>Newsinfo_Customtabs_Model</class>
      </customtabs>
    </models>
  </global>
  <adminhtml>
    <layout>
      <updates>
        <customtabs>
          <file>customtabs.xml</file>
        </customtabs>
      </updates>
    </layout>
    <events>
      <catalog_product_save_after> //for customer customer_save_after
        <observers>
          <newsinfo_save_product_data> //for customer newsinfo_save_customer_data 
            <type>singleton</type>
            <class>customtabs/observer</class>
            <method>saveProductTabData</method>
          </newsinfo_save_product_data>
        </observers>
      </catalog_product_save_after>
    </events>
  </adminhtml>
</config>

Here below display observer event after save product/customer data.create new `Tab.php` file in `app\code\local\Newsinfo\Customtabs\Block\Adminhtml\Catalog\Product\`(for customer file location `app\code\local\Newsinfo\Customtabs\Block\Adminhtml\Customer\`) as below code.

<?php
class Newsinfo_Customtabs_Block_Adminhtml_Catalog_Product_Tab 
extends Mage_Adminhtml_Block_Template
implements Mage_Adminhtml_Block_Widget_Tab_Interface {
    public function _construct()
    {
 parent::_construct();
 $this->setTemplate('customtabs/tab.phtml');
    }
    public function getTabLabel()
    {
     return $this->__('My Custom Tab');
    }
    public function getTabTitle()
    {
     return $this->__('Click here to view your custom tab content');
    }
    public function canShowTab()
    {
  return true;
    }
    public function isHidden()
    {
     return false;
    }
}?>

Add new `customtabs.xml` file in `app\design\adminhtml\default\default\layout\`.

<?xml version="1.0"?>
<layout>
  <adminhtml_catalog_product_edit>//for customer adminhtml_customer_edit
    <reference name="product_tabs">
      <action method="addTab">
        <name>my_custom_tab</name>
        <block>customtabs/adminhtml_catalog_product_tab</block>
        //for customer adminhtml_customer_tab</action>
    </reference>
  </adminhtml_catalog_product_edit>
</layout>

Create new phtml file in admin panel that you want to display in custom tab.here just display text-box.create `tab.phtml` file in `app\design\adminhtml\default\default\template\customtabs\` as below.

<div>
  <label>Custom Field</label>
  <input name="custom_field" />
</div>

Follow below code and create new extension that displays custom tab in product/category edit section if not display than clear all cache and check again.now create `Observer.php` for save data in `app\code\local\Newsinfo\Customtabs\Model\`as below.

<?php
class Newsinfo_Customtabs_Model_Observer
{
 static protected $_singletonFlag = false;
 public function saveProductTabData(Varien_Event_Observer $observer)
 {
  if (!self::$_singletonFlag) {
   self::$_singletonFlag = true;
   $product = $observer->getEvent()->getProduct(); //For Product
   $customet = $observer->getEvent()->getCustomer(); //For Customer
   try {
    $customFieldValue =  Mage::app()->getRequest()->getPost('custom_field');
   }
   catch (Exception $e) {
    Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
   }
  }
 }
 public function getProduct() //For Product
 {
  return Mage::registry('product');
 }
 public function getCustomer() //For Customer
 {
  return Mage::registry('current_customer');
 }
}
?>

No comments :

Post a Comment