1

I am trying to add a button in the Customers->Account Information tab. I want the button to do an action when it is clicked. I would like to do this in a custom module. I'm not too fond of the idea of rewriting core files or any classes. From what I have googled people say you can do this using an Observer, an example is here , if that is true then I would like to do it this way.

I know how to make a basic module, what I need help on is how do you put a button in a specific tab without rewriting the files/classes?

Update 11/3/2013 11am:

Here is the screenshot here

I would like to add the button on this tab.

Update 2:48pm 11/3/2014

Here is my code so far, maybe I am making a mistake somewhere.

My file structure

-app
    -local
        -Rdtmodules
            -ChangeGroupNotification
                -etc
                    -config.xml
                -Model
                    -Observer.php
    -etc
        -modules
            -Rdtmodules_ChangeGroupNotification.xml

config.xml

<?xml version="1.0" encoding="UTF-8"?>
    <config>
        <modules>
            <Rdtmodules_ChangeGroupNotification>
                <version>1.0.0</version>
            </Rdtmodules_ChangeGroupNotification>
        </modules>
        <global>
            <models>
                <rdtmodules_changegroupnotification>
                    <class>Rdtmodules_ChangeGroupNotification_Model</class>
                </rdtmodules_changegroupnotification>
            </models>
            <events>
                <adminhtml_block_html_before>
                    <observers>
                        <rdtmodules_changegroupnotification>
                            <class>rdtmodules_changegroupnotification/observer</class>
                            <method>sendCustomerGroupChangeNotification</method>
                            <type>singleton</type>
                        </rdtmodules_changegroupnotification>
                    </observers>
                </adminhtml_block_html_before>
            </events>
        </global>
    </config>

Observer.php

<?php 

class Rdtmodules_ChangeGroupNotification_Model_Observer {
    public function sendCustomerGroupChangeNotification(Varien_Event_Observer $observer) {
        $block = $observer->getEvent()->getData('block');
        if($block->getId() == 'customer_edit' && $block->getRequest()->getControllerName() == 'customer_edit') {
            $block->addButton('test_print', array(
                'label'     => 'Test',
                'onclick'   =>'setLocation(\'' . $block->getUrl('html/sales_order/print') . '\')',
                'class'     => 'go'
        ));
        }
    }
}
2
  • Can you add a screen shot not sure what you meant by "put a button in a specific tab " Nov 3, 2014 at 20:41
  • I want to put the button in the tab under all of the fields.
    – Aedonis
    Nov 3, 2014 at 21:26

2 Answers 2

1

So I figured it out. @R.S. helped me get to the right direction but gave me the wrong conditions for my if statement :l. I appreciate the guidance though. Thank you.

So I figured the problem was with my if statements since everything else was working. So first here is my code for the observer.

Observer.php

<?php
class Rdtmodules_GroupNotification_Model_Observer
{
    public function sendNotification(Varien_Event_Observer $observer){
        $block = $observer->getEvent()->getData('block');
        if($block->getNameInLayout() == 'customer_edit' && $block->getRequest()->getControllerName() == 'customer') {
            $block->addButton('test_print', array(
                'label'     => 'Test',
                'onclick'   =>'setLocation(\'' . $block->getUrl('html/sales_order/print') . '\')',
                'class'     => 'go'
            ));
        }
    }       
}

the $block->getId() didn't return anything which is why it wasn't working. Also the controller name was not customer_edit it was just customer o.o.

How i figured this out was I printed the controller names action names and block names in the layout.

echo "Controller Name: " . $block->getRequest()->getControllerName();
echo "Action Name: " . $block->getRequest()->getActionName();
echo "Block Name: " . $block->getNameInLayout();

That is how I figured out what the problem was.

Very useful.

1

To add a button next to "Back, Reset, ...", try create a custom module with an observer.

in config.xml

<events>
    <adminhtml_block_html_before>
        <observers>
        <MagePal_CustomerButton>
            <class>MagePal_CustomerButton_Model_Observer</class>
            <method>orderPageButton</method>
            <type>model</type>
        </MagePal_CustomerButton>
        </observers>
    </adminhtml_block_html_before>
</events>

In observer.php

class MagePal_CustomerButton_Model_Observer
{
    public function orderPageButton( Varien_Event_Observer $observer )
    {
      $block = $observer->getEvent()->getData( 'block' );

        if($block->getType() == 'adminhtml/customer_edit' && $block->getRequest()->getControllerName() == 'customer')
        { 
            $block->addButton('test_print', array(
                'label'     => 'Test',
                'onclick'   => 'setLocation(\'' . $block->getUrl('html/sales_order/print') . '\')',
                'class'     => 'go'
            ));
        }
    }
}
9
  • I added the screenshot. So from what I see in your code in the config.xml the adminhtml_block_html_before is the observer that I am trying to use right? and then inside is the information about my custom observer right? And in the observer.php you are looking for the customer_edit block. So does that mean you can decide what block you want to add things too /edit by giving the block id? Also, how the heck did you know that was its id? Is there a reference? Thanks for this, I am learning alot.
    – Aedonis
    Nov 3, 2014 at 21:24
  • Okay, I think I get where to find the block id. It is the block name in the customer.xml file right? So I want to add it underneath the customer_group. so the code would look like this if($block->getId() == 'customer_group' && $block->getRequest()->getControllerName() == 'customer_group') and in the config.xml i would use adminhtml_block_html_after right?
    – Aedonis
    Nov 3, 2014 at 21:37
  • Im not getting this to work. I will post my code up, maybe im doing something wrong.
    – Aedonis
    Nov 3, 2014 at 22:48
  • The code I suggested is to put the button in the top row of buttons. What you are trying to do is either very difficult or maybe it can be done using the customer eav. That form is generated by Mage_Adminhtml_Block_Customer_Edit_Tab_Account Nov 4, 2014 at 14:19
  • I tried out your code exactly how you put it, if you look at the code i put in my question you can see that. It isnt working.
    – Aedonis
    Nov 4, 2014 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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