1

I am currently creating a button for a single order in Magento(next to invoice, ship, reorder etc.). I would like the button to link to a javascript function inside a template with variables from that order passed to the function.

The way I currently have is I call a custom controller which goes to a new page("setLocation('{$block->getUrl('*/shiprush_orderbutton/indexaction')}')").

Is there any way I could avoid this and just call the function straight on the click of the button.

app\code\local\MyApp\OrderButton\etc\config.xml

<config>
<modules>
    <MyApp_OrderButton>
        <version>0.0.1</version>
    </MyApp_OrderButton>
</modules>
<global>
    <models>
        <myapp_orderbutton>
            <class>MyApp_OrderButton_Model</class>
        </myapp_orderbutton>
    </models>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <myapp_orderbutton before="Mage_Adminhtml">
                        MyApp_OrderButton_Adminhtml
                        </myapp_orderbutton>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</global>
<adminhtml>
    <events>
        <adminhtml_widget_container_html_before>
            <observers>
                <myapp_orderbutton>
                    <class>myapp_orderbutton/observer</class>
                    <method>adminhtmlWidgetContainerHtmlBefore</method>
                    <type>singleton</type>
                </myapp_orderbutton>
            </observers>
        </adminhtml_widget_container_html_before>
    </events>
    <layout>
        <updates>
            <orderbutton>
                <file>mybutton.xml</file>
            </orderbutton>
        </updates>
    </layout>
</adminhtml>
</config>

app\design\adminhtml\default\default\layout\mybutton.xml

<layout>
    <adminhtml_sales_order_view>
        <reference name="head">
            <action method="setTemplate><template>shiprush/sales/order/view/info.phtml</template>
            </action>
        </reference>
    </adminhtml_sales_order_view>

app\code\local\MyApp\OrderButton\Model\Observer.php:

class MyApp_OrderButton_Model_Observer {
public function adminhtmlWidgetContainerHtmlBefore($event) {

    $block = $event->getBlock();
    $magemodel = Mage::getModel('sales/order');
    $orderid = $magemodel->getIncrementId();
    $order = $magemodel->loadByIncrementId($orderid);
    if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
        // $block->prepareLayout();
        $block->addButton('do_something_crazy', array(
            'label'     => 'my button',
            'onclick'   => "setLocation('{$block->getUrl('*/myapp_orderbutton/indexaction')}')",
            'class'     => 'go'
        ));           
    }
}
}

app\code\local\MyApp\OrderButton\controllers\Adminhtml\ButtonController.php:

class MyApp_OrderButton_Adminhtml_ButtonController extends Mage_Adminhtml_Controller_Action {
public function indexAction()
{
    $this->loadLayout();
    $this->renderLayout();
}
}
8
  • What does the setLocation look like after it gets added to the DOM? You've got those curly braces there and are calling a PHP method in the context of Javascript. Commented Jul 1, 2015 at 18:31
  • @qwerty123.. you could try using ajax and do something like 'onclick' => "new Ajax.Request('$block->getUrl('*/myapp_orderbutton/indexaction')" Commented Jul 1, 2015 at 18:44
  • @R.S if I do a simple alert("x") inside the onclick js, it allows it, but it doesn't let me add external src or functions, how would I do this Commented Jul 1, 2015 at 20:02
  • @R.S tried both, with the whole function I got: Parse error: syntax error, unexpected 'https' (T_STRING), expecting ')' because I tried adding an external source that the function relies on. if I just did function(){alert("x")}, nothing happened when I clicked the button Commented Jul 1, 2015 at 20:11
  • Try 'onclick' => "(function(e){ alert('x'); })(event)", Commented Jul 1, 2015 at 20:45

1 Answer 1

0

Assuming that you have created a custom module for this, in your config.xml add

<adminhtml>
    <layout>
        <updates>
            <magepal module="magepal">
                <file>magepal.xml</file>
            </magepal>
        </updates>
    </layout>
</adminhtml>

JS file location

skin/adminhtml/default/default/[companyName]/[moduel]/js/my.js

In magepal.xml add

<adminhtml_sales_order_view>
    <reference name="head">
         <action method="addItem"><type>skin_js</type><script>[companyName]/[module]/js/my.js</sc‌​ript></action>
    </reference>
</adminhtml_sales_order_view>  

Then call the function from your block

$block->addButton('do_something_crazy', array(
   'label'     => 'my button',
   'onclick'   => "(function(e){callMyFunction()}'; })(event)",
   'class'     => 'go'
));  
10
  • where should the path of the js file be? Starting from app where do I navigate to? Thanks Commented Jul 8, 2015 at 18:49
  • Assuming your js file is in skin/adminhtml/default/default/[companyName]/[moduel]/js/my.js<action method="addItem"><type>skin_js</type><script>[companyName]/[module]/js/my.js</script></action> Commented Jul 8, 2015 at 20:12
  • no, that didn't work, I put the xml layout file in app/design/adminhtml/default/default/layout and put the js file where you said. Nothing was triggered(made a simple alert in the new function). Commented Jul 8, 2015 at 21:05
  • View page source, is the file included? Commented Jul 8, 2015 at 21:17
  • yes, the source given is localhost/magento/js/MyApp/OrderButton/js/my.js Commented Jul 8, 2015 at 21:24

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.