0

I have Magento 1.7 and I'm trying to create a custom tab in the admin panel to change customer attributes that I have created.

First off I created: /app/code/local/Mage/Adminhtml/Block/Customer/Edit/Tabs.php which copies /app/code/core/Mage/Adminhtml/Block/Customer/Edit/Tabs.php

I want to do this correct, so I'm not changing the core files! Inside the function _beforeToHtml() I have the following code:

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

This code alone creates the Custom Info tab in the picture! enter image description here

When I click the tab nothing happens of course so I tried adding code to fix that!

I copied: /app/code/core/Mage/Adminhtml/controllers/CustomerController.php to /app/code/local/Mage/Adminhtml/controllers/CustomerController.php and added the following code:

public function customAction()
    {
        $this->_initCustomer();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('customtab/adminhtml_customer_edit_tab_custom','admin.customer.custom')->setCustomerId(Mage::registry('current_customer')->getId())
                ->setUseAjax(true)
                ->toHtml()
        );
    }

Next, I created the file: /app/code/local/Sean/CustomTab/Block/Adminhtml/Customer/Edit/Tab/Custom.php and that file contains:

<?php

class Sean_CustomTab_Block_Adminhtml_Customer_Edit_Tab_Custom extends Mage_Adminhtml_Block_Widget_Form
{
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('customtab/customer/tab/custom.phtml');
    }
}

Then, I created: /app/design/adminhtml/default/default/template/customtab/customer/tab/custom.phtml Where I just have

Hello World!

And Lastly I enable the extension through app/etc/modules/Sean_CustomTab where I have the code:

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

I Refresh the Magento Cache! but alas nothing happens when I click the Custom Info tab it just shows the loading icon then it goes back to as if I never clicked it! Please help I'll be here responding to answers and questions to clarify is there a typo? Or something I'm missing?

1

Try using an event instead. The code below is for the Admin User, but it should also work for the customer area:

/**
 * Adds a tab to the Admin User account (System > Permissions > Users)
 *
 * Event(s): core_block_abstract_prepare_layout_after
 *
 * @param  Varien_Event_Observer $observer
 */
public function injectTabs($observer)
{
    $block = $observer->getEvent()->getBlock();

    // add tab in admin edit page
    if ($block instanceof Mage_Adminhtml_Block_Permissions_User_Edit_Tabs) {
        $helper = Mage::helper('adminhtml');

        $block->addTabAfter(
            'workflow',
            array(
                'label'     => $helper->__('Workflow Info'),
                'title'     => $helper->__('Workflow Info'),
                'content'   => $block->getLayout()
                    ->createBlock('workflow/adminhtml_permissions_user_edit_tab_workflow')
                    ->toHtml(),
            ),
            'roles_section'
        );
    }

    return $this;
}

Make sure you only put it in the Adminhtml area of the config.xml:

<config>
    <modules>
        <Some_Module>
            <version>1.0.0</version>
        </Some_Module>
    </modules>
    <global>
        <helpers>
            <somemod>
                <class>Some_Module_Helper</class>
            </somemod>
        </helpers>
        <blocks>
            <somemod>
                <class>Some_Module_Block</class>
            </somemod>
        </blocks>
        <models>
            <somemod>
                <class>Some_Module_Model</class>
            </somemod>
        </models>
    </global>
    <adminhtml>
        <events>
            <core_block_abstract_prepare_layout_after>
                <observers>
                    <somemod_observer>
                        <type>singleton</type>
                        <class>somemod/event_observer</class>
                        <method>injectTabs</method>
                    </somemod_observer>
                </observers>
            </core_block_abstract_prepare_layout_after>
        </events>
    </adminhtml>
</config> 
  • Do I keep any of my current code? It sounds like you're saying I can create a new custom module sean/new_module that has an etc and a Model folder, where I put config.xml inside the etc and the observer in the model? – CodingMageSheen Nov 18 '16 at 23:19
  • You can keep your current code for now, but disable the call to "addTab()"... also you need a Block to render for the contents (your current Block above should be fine). – Phil M Nov 18 '16 at 23:26
  • okay sounds good let me try it. I'm going to put an etc folder and model folder inside CustomTab – CodingMageSheen Nov 18 '16 at 23:38
  • does anything else need to go into the config.xml besides what you listed above? – CodingMageSheen Nov 18 '16 at 23:39
  • also it's giving me a slight error i can't hve public function inside my observer since it's outside of the class so will it still work as just "function injectTabs?" – CodingMageSheen Nov 18 '16 at 23:44
0

I solved this a different way without using an observer (Sorry I'm not very experienced with observers).

I used this to tell Magento where to look to find my files. I created a custom module: app/etc/modules/Sean_CustomerTab.xml

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

Next, I created app/code/local/Sean/CustomerTab/etc/config.xml This defined the blocks, layouts, and modules I'm going to use in Admin

<config>
    <modules>
        <Sean_CustomerTab>
            <version>0.0.1</version>
        </Sean_CustomerTab>
    </modules>
    <adminhtml>
        <layout>
            <updates>
                <customertab>
                    <file>customertab.xml</file>
                </customertab>
            </updates>
        </layout>
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <sean_customertab before="Mage_Adminhtml">Sean_CustomerTab_Adminhtml</sean_customertab>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <global>
        <blocks>
            <customertab>
                <class>Sean_CustomerTab_Block</class>
            </customertab>
        </blocks>
    </global>
</config>

Then I added app/code/local/Sean/CustomerTab/Block/Adminhtml/Customer/Edit/Tab/Action.php So that I could mimic how the other tabs work in the Admin backend

(My Tab isn't Pinocchio, he's a real boy!)

<?php

/**
 * Adminhtml customer action tab
 *
 */
class Sean_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';
    }
}

After that I created app/design/adminhtml/default/default/layout/customertab.xml Here I'm saying use the method addTab and insert the block

<?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>

Now I have app/design/adminhtml/default/default/template/customertab/action.phtml Where I can do all my awesome html and it shows perfectly

AND I also have app/code/local/Sean/CustomerTab/controllers/Adminhtml/CustomerTabController.php for a custom controller to act with that html page!

@Phil Mobley I'll upvote your answer, you helped me think of a way to do this!

RESULT: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.