How to Add Custom Block in Admin Sales Order View in Magento 2
The default Magento 2 displays the address information, payment, and shipping information, etc. on the admin sales order view.
However, sometimes it may not be enough based on the business requirements.
Therefore, one needs to add custom block in admin sales order view in Magento 2.
For example, you are offering customized products and the customers add instructions for their personalization. To display those instruction texts in the admin sales order view, you can use the below solution.
Or, if you are an Indian store, you may display GST information in the sales order view.
Display any custom block based on your business requirements as shown in the below figure:
Steps to add custom block to admin sales order view in Magento 2:
- Create registration.php file at app\code\Vendor\Module directory
123<?phpuse \Magento\Framework\Component\ComponentRegistrar;ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__); - Create module.xml file at app\code\Vendor\Module\etc directory
12345<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="Vendor_Module" setup_version="1.0.0"/></config> - Create sales_order_view.xml file at app\code\Vendor\Module\view\adminhtml\layout
12345678910<?xml version="1.0"?><page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left"xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"><body><!--add custom block --><referenceBlock name="order_additional_info"><block class="Vendor\Module\Block\Adminhtml\Order\View\View" name="custom_view" template="order/view/view.phtml"/></referenceBlock></body></page> - Create block file View.php in Vendor\Module\Block\Adminhtml\Order\View
12345678910<?phpnamespace Vendor\Module\Block\Adminhtml\Order\View;class View extends \Magento\Backend\Block\Template{public function myFunction(){//your codereturn "Customers' Instruction";}} - Create view.phtml in Vendor\Module\view\adminhtml\templates\order\view
123456<div><h1><?php echo $block->myFunction()?></h1><h3>I want two different sizes, i.e., M and XL for the customized tshirts ordered here. Also, I want to increase thefont size of the text to 25.</h3></br><h3>Thank you.</h3></div>
That’s it.
You can also add a custom Phtml in Magento 2 admin to change the complete layout.
Any doubts about the solution can be mentioned in the Comments section below.
I’d be happy to help.
Also, do share the solution with the Magento Community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Check Which Shopping Cart Rule is Applied to Cart in Magento
How To Create Magento 2 Catalog Price Rules
Next