Magento 1.8 – How to Change Position of Custom Tab in Catalog Product Edit Page

formsmagento-1.8product-edittabs

I have a custom module which I am using to do some advanced operation on product. Using my module, I have overwrite the tabs file of adminhtml_catalog_product_edit_tabs in order to appear my custom tab on catalog_prdouct_edit page in admin. Now the tab appears above all default tabs.

enter image description here

(See Combo Products tab)

I need to appear this tab below cross-sells tab(which is a default tab).

local/Mysite/combooffers/etc/config.xml

<config>
<modules>
    <Mysite_Combooffers>
        <version>0.1.0</version>
    </Mysite_Combooffers>
</modules>
<frontend>
    <routers>
        <Combooffers>
            <use>standard</use>
            <args>
                <module>Mysite_Combooffers</module>
                <frontName>combooffers</frontName>
            </args>
        </Combooffers>
    </routers>
    <layout>
        <updates>
            <combooffers>
                <file>combooffers.xml</file>
            </combooffers>
        </updates>
    </layout>
</frontend>
<admin>
    <routers>
        <combooffers>
            <use>admin</use>
            <args>
                <module>Mysite_Combooffers</module>
                <frontName>combooffers</frontName>
            </args>
        </combooffers>
    </routers>
</admin>
<global>
    <blocks>
        <combooffers>
            <class>Mysite_Combooffers_Block</class>
        </combooffers>
        <adminhtml>            
          <rewrite>  
                 <catalog_product_edit_tabs>Mysite_Combooffers_Block_Adminhtml_Catalog_Product_Edit_Tabs</catalog_product_edit_tabs> 
            </rewrite>  
        </adminhtml>
    </blocks>
    <helpers>
        <combooffers>
            <class>Mysite_Combooffers_Helper</class>
        </combooffers>
    </helpers>
</global>
</config>

local/Mysite/Combooffers/Block/Adminhtml/Catalog/Product/Edit/Tabs.php

<?php

class Mysite_Combooffers_Block_Adminhtml_Catalog_Product_Edit_Tabs extends Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs
{
protected function _prepareLayout()
{
    $this->addTab('combooffer', array(
            'label'     => Mage::helper('catalog')->__('Combo Products'),
            'content'   => $this->getLayout()->createBlock('combooffers/adminhtml_catalog_product_edit_tab_Combooffer')->toHtml(),
        ));
    return parent::_prepareLayout();
}
} 

Mysite/Combooffers/Block/Adminhtml/Catalog/Product/Edit/Tab/Combooffer.php

<?php

class Mysite_Combooffers_Block_Adminhtml_Catalog_Product_Edit_Tab_Combooffer extends Mage_Adminhtml_Block_Widget_Form
{
 protected function _prepareForm()
 {    
 $productId      = $this->getRequest()->getParam('id');
 $storeId = $this->getRequest()->getParam('store');
 if(!$storeId) $storeId=0;

  $form = new Varien_Data_Form();
  $fieldset = $form->addFieldset('combooffer_form', array('legend'=>Mage::helper('combooffers')->__('Combo Products')));

// You can write your code here

   $this->setForm($form);
  return parent::_prepareForm();
  }

  public function getTabTitle()
  {
    return Mage::helper('combooffers')->__('Combo Products');
   }

  public function canShowTab()
  {
    return true;
  }

  public function getTabUrl() 
  {
    return $this->getUrl('*/*/combooffers', array('_current' => true));
  }

  public function isHidden()
  {
    return false;
  }

}

How can achieve this. Please give a hint for this problem.

Best Answer

When you create your own tab you can use the index 'after' as shown below.

$this->addTab('productalert', array(
     'label'   => Mage::helper('catalog')->__('Product Alerts'),
     'content' => $this->_translateHtml($this->getLayout()
                         ->createBlock('adminhtml/catalog_product_edit_tab_alerts', 'admin.alerts.products')->toHtml()),
     'after'   => '[tab_id]'
));

In [tab_id] you insert a real tab_id created by Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs block like 'inventory', 'websites' or 'categories'. When you do this, the responsible block for the output of the tabs makes a reorder automatically in _beforeToHtml() method (see Mage_Adminhtml_Block_Widget_Tabs:: _beforeToHtml()).

protected function _beforeToHtml()
{
    if ($activeTab = $this->getRequest()->getParam('active_tab')) {
        $this->setActiveTab($activeTab);
    } elseif ($activeTabId = Mage::getSingleton('admin/session')->getActiveTabId()) {
        $this->_setActiveTab($activeTabId);
    }

    $_new = array();
    foreach( $this->_tabs  as $key => $tab ) {
        foreach( $this->_tabs  as $k => $t ) {
            if( $t->getAfter() == $key ) {
                $_new[$key] = $tab;
                $_new[$k] = $t;
            } else {
                if( !$tab->getAfter() || !in_array($tab->getAfter(), array_keys($this->_tabs)) ) {
                    $_new[$key] = $tab;
                }
            }
        }
    }

    $this->_tabs = $_new;
    unset($_new);

    $this->assign('tabs', $this->_tabs);
    return parent::_beforeToHtml();
}

Good luck.

Tiago Sampaio

Related Topic