Adding a Custom Button to Magento Order View Page
As an experienced developer working with e-commerce platforms, I often receive requests for customizing the admin panel interface of platforms like Magento. One common task is adding new buttons to existing pages, such as the order view page in the admin section.
In this guide, I’ll walk you through the process of adding a custom button next to the “Back” and “Edit” buttons on an order’s view page in Magento 2. This technique allows you to extend the functionality of your store without requiring extensive core modifications.
Step 1: Understanding the Layout Structure
The critical layout file for this task is vendor/magento/module-sales/view/adminhtml/layout/sales_order_view.xml. Here, we’ll add a new button using a layout update.
Step 2: Creating a New Block
First, let’s create a simple block to hold our custom button logic. In your custom theme or module, navigate to the Block directory and create a new file named CustomButton.php.
// File path: app/code/Vendor/Module/Block/CustomButton.php
namespace Vendor\Module\Block;
use Magento\Framework\App\RequestInterface;
use Magento\Sales\Model\OrderRepository;
class CustomButton
{
/**
* @var RequestInterface
*/
protected $request;
/**
* @var OrderRepository
*/
protected $orderRepository;
public function __construct(
RequestInterface $request,
OrderRepository $orderRepository
) {
$this->request = $request;
$this->orderRepository = $orderRepository;
}
public function getOrderId()
{
return (int) $this->request->getParam('order_id');
}
public function getCustomButtonUrl()
{
return $this->getUrl('vendor_module/index/perform_action', ['order_id' => $this->getOrderId()]);
}
}
In this block, we’re using the request object to fetch the current order ID and generating a URL for our custom action.
Step 3: Adding the Button to the Layout
Now, let’s add the button to the layout file. In your theme or module config file (app/code/Vendor/Module/etc/adminhtml/di.xml), add the following XML:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Sales\Block\Adminhtml\Order\View">
<arguments>
<argument name="blocks" xsi:type="array">
<item name="custom_button_block" xsi:type="string">Vendor_Module::block_custom_button</item>
</argument>
</arguments>
</type>
</config>
Step 4: Customizing the Button in the Template
Finally, let’s customize the button appearance in the template file. Open vendor/magento/module-sales/view/adminhtml/templates/order/view.phtml (create a copy in your theme if it doesn’t exist) and add the following code:
<!-- Add this line after the "Edit" button -->
<div class="order-action-toolbar">
<div class="toolbar-item">
<button data-ui-id="sales-order-view-custom-button-button"
type="button"
class="action-secondary action-tertiary sales-admin__custom-button">
<span>Custom Button</span>
</button>
</div>
</div>
Step 5: Creating the Custom Action
To make our button functional, we need to create a controller that handles the “perform_action” request.
// File path: app/code/Vendor/Module/Controller/Adminhtml/Index.php
namespace Vendor\Module\Controller\Adminhtml;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
class Index extends Action
{
public function execute()
{
$orderId = (int) $this->getRequest()->getParam('order_id');
// Perform your custom action here, e.g., fetch specific order data
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
}
}
Conclusion
Adding a custom button to the Magento admin panel is an essential skill for any developer working with this platform. By following the steps outlined aboveâcreating a block, updating the layout, customizing the template, and implementing the controllerâyou can easily extend the functionality of your store’s order view page.
Remember that proper coding practices, such as using an event listener or layout updates, can help you maintain a clean and manageable codebase. Also, consider using a third-party extension marketplace like Laravel Company for more advanced customizations and features.
The key takeaway from this guide is that Magento’s modular architecture allows for extensive customization without the need to modify core files directly. By leveraging blocks, layouts, and controllers, you can create powerful extensions tailored to your specific business requirements.