Magento Admin Custom Button on View Order

Magento Admin Custom Button on View Order

September 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Admin Custom Button on View Order

Creating a Custom Button on Magento View Order Page

As an experienced developer, I’m often approached by clients who want to customize their Magento admin interface to streamline specific business processes. One common request is to add a custom button on the view order page that triggers a specific action upon click.

Recently, one of our clients wanted to create a “Dropship” button on the Magento View Order page that would email a vendor using information contained in the current order and its items when clicked. They had already created the module and the button but were struggling to make it perform the desired action instead of just redirecting to a URL.

Understanding the Current Implementation

The client provided their current implementation, which includes an XML configuration file (MG_Dropship.xml), a main configuration file (config.xml), and a view file for the custom block (View.php).

config.xml

<config>
    <modules>
        <MG_Dropship>
            <version>0.0.1</version>
        </MG_Dropship>
    </modules>

    <!-- Configure our module's behavior in the global scope -->
    <global>

    <blocks>
         <adminhtml>
            <rewrite>
                <sales_order_view>MG_Dropship_Block_Adminhtml_Sales_Order_View</sales_order_view>
            </rewrite>
        </adminhtml>
    </blocks>

    </global>

</config>

This file configures the module and rewrites the Mage_Adminhtml_Block_Sales_Order_View block to our custom block.

View.php

<?php

class MG_Dropship_Block_Adminhtml_Sales_Order_View extends Mage_Adminhtml_Block_Sales_Order_View {
    public function  __construct() {

        parent::__construct();

        $this->addButton('dropship', array(
            $message = "Are you sure you want to dropship?",
            'label'     => Mage::helper('Sales')->__('Dropship'),
            'onclick'   => "confirmSetLocation('{$message}','{$this->getUrl('MG_Dropship')}')"
        ));
    }
}

In the _construct method, a custom button is added to the view order page. The onclick attribute uses JavaScript to display a confirmation dialog and redirect to a URL (MG_Dropship).

The Problem: Redirect Instead of Action

The issue lies in the onclick attribute of the button. Currently, it’s set up to use confirmSetLocation which is used for redirects, not for executing custom actions.

To fix this, we need to change the way the button interacts with our module and implement a controller that handles the dropship action.

The Solution: Implementing a Custom Controller

First, you need to create an controllers directory under your module’s main directory (MG_Dropship). Inside this new directory, create a SalesController.php file:

SalesController.php

<?php

class MG_Dropship_SalesController extends Mage_Adminhtml_Controller_action {
    public function dropshipAction() {

        $orderId = $this->getRequest()->getParam('order_id');

        // Load the order object using the provided ID
        $order = Mage::getModel('sales/order')->load($orderId);

        // Validate if the order is loaded successfully
        if (!$order->getId()) {
            $this->_redirect('*/*/');
            return;
        }

        // Prepare and send the email to the vendor using the order data
        // Add your dropship logic here

        Mage::getSingleton('core/session')->addSuccess('Order has been dropshipped successfully.');
        $this->_redirect('*/*/view', array('order_id' => $order->getId()));
    }
}

This controller handles the dropship action. It retrieves the order ID from the request parameter, loads the order object, and then sends an email to the vendor using the order data (not shown in this example).

Updating the View Block

Now, update the _construct method of your view block (View.php) to point the button’s onclick attribute to our new controller:

Updated View.php

public function  __construct() {

    parent::__construct();

    $this->addButton('dropship', array(
        'label'     => Mage::helper('Sales')->__('Dropship'),
        'onclick'   => "setLocation('{$this->getUrl('MG_Dropship/sales/dropship', array('order_id' => $this->getOrderId()))}')"
    ));
}

We use setLocation instead of confirmSetLocation, and we pass the order ID as a parameter to the controller.

Conclusion

In this post, I demonstrated how to add a custom button on the Magento View Order page that performs an action when clicked. The key steps involved:

  1. Creating a new module with the necessary XML configuration files.
  2. Rewriting the default view block to our custom block.
  3. Adding a custom button to the view order page in the _construct method of the view block.
  4. Implementing a controller that handles the dropship action and sends an email using the order data.

Remember, the actual dropship logic (sending the email) is not included in this example. You will need to implement your specific business logic inside the dropshipAction method of the SalesController.php file.

For more detailed guides on Magento module development and customizing admin interfaces, I recommend visiting LaravelCompany. They provide comprehensive tutorials and resources for Magento developers.

Extensions Admin Configuration Email

Our network