1

I'm new to magento. here I have created custom module to add custom tab on customer information in admin panel. for that I have written following code. but it's returning an error. Any how here I'm not creating field to save the creditlimit attribute i will create after it's working.

Error: Class 'Mage_Adminhtml_CustomerController' not found.

my code is:

magento\app\etc\modules\Easylife_CreditLimit.xml

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

magento\app\code\local\Easylife\CreditLimit\Block\Adminhtml\Customer\Tab.php

<?php
class Easylife_CreditLimit_Block_Adminhtml_Customer_Tab
    extends Mage_Adminhtml_Block_Template
    implements Mage_Adminhtml_Block_Widget_Tab_Interface

{
    /**
     * Set the template for the block
     *
     */
    public function _construct()
    {
        parent::_construct();
        $this->setTemplate('creditlimit/customer/tab.phtml');
    }
    /**
     * Retrieve the label used for the tab relating to this block
     *
     * @return string
     */
    public function getTabLabel()
    {
        return $this->__('My Custom Tab');
    }
    /**
     * Retrieve the title used by this tab
     *
     * @return string
     */
    public function getTabTitle()
    {
        return $this->__('Click here to view your custom tab content');
    }
    /**
     * Determines whether to display the tab
     * Add logic here to decide whether you want the tab to display
     *
     * @return bool
     */
    public function canShowTab()
    {
        return true;
    }
    /**
     * Stops the tab being hidden
     *
     * @return bool
     */
    public function isHidden()
    {
        return false;
    }
}

magento\app\code\local\Easylife\CreditLimit\Block\Adminhtml\Customer\Edit\Tabs.php

<?php

class Easylife_CreditLimit_Block_Adminhtml_Customer_Edit_Tabs extends Mage_Adminhtml_Block_Customer_Edit_Tabs
{

    public function __construct()
    {
        parent::__construct();

    }

    protected function _beforeToHtml()
    {

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

    protected function _updateActiveTab()
    {
        parent::_updateActiveTab();
    }

}

magento\app\code\local\Easylife\CreditLimit\Block\Adminhtml\Customer\Edit\Tab\Custom.php

<?php

class Easylife_CreditLimit_Block_Adminhtml_Customer_Edit_Tab_Custom extends Mage_Adminhtml_Block_Widget_Form
{

    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('creditlimit/customer/tab/custom.phtml');
    }    

}

magento\app\code\local\Easylife\CreditLimit\controllers\Adminhtml\CustomerController.php

<?php

class Easylife_CreditLimit_Adminhtml_CustomerController extends Mage_Adminhtml_CustomerController
{

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

}

magento\app\code\local\Easylife\CreditLimit\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_CreditLimit>
            <version>0.1.0</version>
        </Easylife_CreditLimit>
    </modules>

    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <creditlimit before="Mage_Adminhtml">Easylife_CreditLimit_Adminhtml</creditlimit>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>

    <adminhtml>
        <layout>
            <updates>
                <creditlimit>
                    <file>creditlimit.xml</file>
                </creditlimit>
            </updates>
        </layout>
    </adminhtml>

    <global>
        <blocks>
            <creditlimit>
                <class>Easylife_CreditLimit_Block</class>
            </creditlimit>
        </blocks>
        <helpers>
            <creditlimit>
                <class>Easylife_CreditLimit_Helper</class>
            </creditlimit>
        </helpers>
        <events>
            <core_block_abstract_prepare_layout_after>
                <observers>
                    <creditlimit>
                        <type>model</type>
                        <class>Easylife_CreditLimit_Model_Observer</class>
                        <method>InjectTab</method>
                    </creditlimit>
                </observers>
            </core_block_abstract_prepare_layout_after>
        </events>
    </global>

</config>

magento\app\code\local\Easylife\CreditLimit\Model\Observer.php

<?php

class Easylife_CreditLimit_Model_Observer
{

    public function __construct()
    {

    }


    public function InjectTab(Varien_Event_Observer $observer)
    {
        $block = $observer->getEvent()->getBlock();
        // add tab in customer edit page
        if ($block instanceof Mage_Adminhtml_Block_Customer_Edit_Tabs)
        {
            if ($this->_getRequest()->getActionName() == 'edit' || $this->_getRequest()->getParam('type'))
            {
                $block->addTab('domains', array('label'=> Mage::helper('customer')->__('Manage Licensed Domains'),'url'=> $block->getUrl('*/*/custom', array('_current' => true)),'class'=> 'ajax'));
            }
        }

    }

    protected function _getRequest()
    {
        return Mage::app()->getRequest();
    }


}

magento\app\design\adminhtml\default\easylife\layout\creditlimit.xml

<?xml version="1.0"?>
<layout version="0.1.0">

    <adminhtml_customer_edit>
        <reference name="customer_edit_tabs">
            <action method="addTab">
                <name>my_custom_tab</name>
                <block>creditlimit/adminhtml_customer_tab</block>
            </action>
        </reference>
    </adminhtml_customer_edit>

</layout>

magento\app\design\adminhtml\default\easylife\template\creditlimit\customer\tab.phtml

<div class="input-field">
    <label for="custom_field">Custom Field</label>
    <input type="text" class="input-text" name="custom_field" id="custom_field" />
</div>

magento\app\design\adminhtml\default\easylife\template\creditlimit\customer\tab\custom.phtml

<div class="input-field">
    <label>Hi</label>

</div>

Can you please suggest me where I went wrong?

Thanks in advance.

  • Why the downvotes? This seams like a legit question. – Marius May 7 '14 at 8:00
5

Controllers are not autoloaded, so this is why you get an error. You need to load the controller with:

require_once Mage::getModuleDir('controllers', 'Mage_Adminhtml').DS.'CustomerController.php';

class Easylife_CreditLimit_Adminhtml_CustomerController extends Mage_Adminhtml_CustomerController {
...
}

Anyway, it's not the best to rewrite the Mage_Adminhtml_CustomerController, better use event-dispatching and observer for your actions or create your own controller:

class Easylife_CreditLimit_Adminhtml_CustomerController extends Mage_Adminhtml_Controller_Action {
...
}

See also here: Trying to override a controller

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.