phpmagentobackendmagento-1.6

Magento 1.6 add tab to customer edit on back end


I've been trying to add a tab to the Customer Information page in Magento CE 1.6.

I've tried the following examples:

The last one is the only one that even seems to come close. However, it has two problems. One is it edits core files and two is that when I click on the tab it spins and dyes. Chrome DevTools show the server reporting 404.

Can anyone provide me with some decent documentation or code that is compatible with Magento 1.6?


Edit Adding the text from the last link as that seems to be the only one that remotely worked.

  1. Override the file /app/code/core/Mage/Adminhtml/Block/Customer/Edit/Tabs.php,Inside _beforeToHtml() method, add the following code:

    $this->addTab('Custom',array(
    'label' =>Mage::helper('customer')->__('Custom'),
    'class' =>   'ajax',
    'url'   =>   $this->getUrl('*/*/custom',array('_current'=>true)),
    ));
    
  2. Override the file /app/code/core/Mage/Adminhtml/controllers/CustomerController.php, Add the following code:

    public function customAction()
    { $this->_initCustomer();
    $this->getResponse()->setBody(
    Mage::app()->getLayout()->createBlock('core/template')->setTemplate('custom/customer/tab/custom.phtml')->setCustomerId(Mage::registry('current_customer')->getId())
        ->setUseAjax(true)->toHtml()   
    ); 
    
    }
    
  3. Create the file /app/code/core/Namespace/ModuleName/Block/Adminhtml/Customer/Edit/Tab/ and create Custom.php, Add the following source code to the file:

    <?php
    class Custom_Custom_Block_Adminhtml_Customer_Edit_Tab_Custom extends Mage_Adminhtml_Block_Widget_Form
    {
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('custom/customer/tab/custom.phtml');
    }
    }
    ?>
    
  4. Now, you need to create a template file. Go to /app/design/adminhtml/default/default/template/modulename/customer/tab/ and create custom.phtml,


Solution

  • Managed to track down some out of the box code that works without any modification as a starting point:

    http://www.engine23.com/magento-how-to-create-custom-customer-tab-and-submit-a-form.html

    app/etc/modules/Russellalbin_Customertab.xml

    <?xml version="1.0"?>
    <config>
    <modules>
        <Russellalbin_Customertab>
            <active>true</active>
            <codePool>local</codePool>
        </Russellalbin_Customertab>
    </modules>
    </config>
    

    app/etc/local/Russellalbin/Customertab/etc/config.xml

    <config>
        <modules>
            <Russellalbin_Customertab>
                <version>0.0.1</version>
            </Russellalbin_Customertab>
        </modules>
        <adminhtml>
            <layout>
                <updates>
                    <customertab>
                        <file>customertab.xml</file>
                    </customertab>
                </updates>
            </layout>
        </adminhtml>
        <admin>
            <routers>
                <adminhtml>
                    <args>
                        <modules>
                            <russellalbin_customertab before="Mage_Adminhtml">Russellalbin_Customertab_Adminhtml</russellalbin_customertab>
                        </modules>
                    </args>
                </adminhtml>
            </routers>
        </admin>
        <global>
            <blocks>
                <customertab>
                    <class>Russellalbin_Customertab_Block</class>
                </customertab>
            </blocks>
        </global>
    </config>
    

    app/code/local/Russellalbin/Customertab/Block/Adminhtml/Customer/Edit/Tab/Action.php

    /**
     * Adminhtml customer action tab
     *
     */
    class Russellalbin_Customertab_Block_Adminhtml_Customer_Edit_Tab_Action
        extends Mage_Adminhtml_Block_Template
        implements Mage_Adminhtml_Block_Widget_Tab_Interface
    {
    
        public function __construct()
        {
            $this->setTemplate('customertab/action.phtml');
        }
    
        public function getCustomtabInfo()
        {
            $customer = Mage::registry('current_customer');
            $customtab = 'Mail Order Comics Pull List';
            return $customtab;
        }
    
        /**
         * Return Tab label
         *
         * @return string
         */
        public function getTabLabel()
        {
            return $this->__('Customer Pull List');
        }
    
        /**
         * Return Tab title
         *
         * @return string
         */
        public function getTabTitle()
        {
            return $this->__('Pull List Tab');
        }
    
        /**
         * Can show tab in tabs
         *
         * @return boolean
         */
        public function canShowTab()
        {
            $customer = Mage::registry('current_customer');
            return (bool)$customer->getId();
        }
    
        /**
         * Tab is hidden
         *
         * @return boolean
         */
        public function isHidden()
        {
            return false;
        }
    
        /**
         * Defines after which tab, this tab should be rendered
         *
         * @return string
         */
        public function getAfter()
        {
            return 'tags';
        }
    }
    

    app/code/local/Russellalbin/Customertab/controllers/Adminhtml/CustomertabController.php

    class Russellalbin_Customertab_Adminhtml_CustomertabController extends Mage_Adminhtml_Controller_Action
    {
        function resetAction()
        {
            $params = array();
            $path = 'customer';
            $customer_id = (int)$this->getRequest()->getParam('customer_id');
    
            if($customer_id)
            {
                // Do your stuff here
    
    
                $params['id'] = $customer_id;
                $params['back'] = 'edit';
                $params['tab'] = 'customer_edit_tab_action';
                $path = 'adminhtml/customer/edit/';
            }
    
            $params['_store'] = Mage::getModel('core/store')->load(0);
    
            $url = Mage::getModel('adminhtml/url')->getUrl($path, $params);
            Mage::app()
                ->getResponse()
                ->setRedirect($url);
    
            Mage::app()
                ->getResponse()
                ->sendResponse();
    
            exit;
        }
    
    }
    

    app/design/adminhtml/default/default/layout/customertab.xml

    <?xml version="1.0"?>
    <layout version="0.1.0">
    <adminhtml_customer_edit>
        <reference name="customer_edit_tabs">
            <action method="addTab">
                <name>customer_edit_tab_action</name>
                <block>customertab/adminhtml_customer_edit_tab_action</block>
            </action>
        </reference>
    </adminhtml_customer_edit>
    </layout>
    

    app/design/adminhtml/default/default/template/customertab/action.phtml

    <div id="customer_info_tabs_customer_edit_tab_action_content">
        <div class="entry-edit">
            <div class="entry-edit-head">
                <h4 class="icon-head head-edit-form fieldset-legend">Pull List</h4>
            </div>
            <div id="group_fields4" class="fieldset fieldset-wide">
                <div class="hor-scroll">
                    <h2>This is my form and content that I can submit and return back to this page and tab</h2>
                    <div><form action="<?php echo $this->getUrl('adminhtml/customertab/reset/', array('customer_id'=> $customer_id)); ?>" method="get"><input type="submit" value="Post This Form" /></form></div>
                </div>
            </div>
        </div>
    </div>