How to Add Custom Message to Admin Sales Order View, Invoice, and Credit Memo in Magento 2
Magento 2 store admins not only strive to make their store convenient to customers but also continuously implement various tactics to make the store convenient for themselves!
Default Magento 2 provides a user-friendly interface and customization facility for the admin to address their requirements efficiently.
Store admins tend to improve their stores’ admin panel by customizing admin functionalities, adding custom messages, columns, adding filters or removing them in the grid, etc.
One such example that I have mentioned here is to add custom message to admin sales order view, invoice, and credit memo in Magento 2.
For example, you may want to display a custom message when an order contains gift while processing that order from the admin panel.
Check out the below solution for implementing such examples in your store:
Steps to Add Custom Message to Admin Sales Order View, Invoice, and Credit Memo in Magento 2
- Create layout files.
- Method to add a custom message to admin sales order view
Create sales_order_view.xml at Vendor/Module/view/adminhtml/layout/
1 2 3 4 5 6 7 8 9 10 |
<?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> <referenceBlock name="order_info"> <block class="Vendor\Module\Block\Adminhtml\Order\View\CustomMessage" name="sales_order_view_custom_msg" template="order/view/customMessage.phtml"/> </referenceBlock> </body> </page> |
- Method to add a custom message to the invoice
Create sales_order_invoice.xml at Vendor/Module/view/adminhtml/layout/
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="order_info"> <block class="Vendor\Module\Block\Adminhtml\Order\View\CustomMessage" name="invoice_view_custom_msg" template="order/view/customMessage.phtml"/> </referenceBlock> </body> </page> |
- Method to add a custom message to the credit memo
Create sales_order_creditmemo.xml at Vendor/Module/view/adminhtml/layout/
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="order_info"> <block class="Vendor\Module\Block\Adminhtml\Order\View\CustomMessage" name="credirmemo_view_custom_msg" template="order/view/customMessage.phtml"/> </referenceBlock> </body> </page> |
2. Now, create customMessage.php at Vendor/Module/Block/Adminhtml/Order/View/
1 2 3 4 5 6 |
<?php namespace Vendor\Module\Block\Adminhtml\Order\View; class customMessage extends \Magento\Backend\Block\Template { // Here you can fetch order detail if you want to display in order information as an extra detail } |
3. Create customMessage.phtml at Vendor/Module/view/adminhtml/templates/order/view
1 |
<h3>This order contains gift</h3> |
That’s all!
In case you want to change the whole layout of the backend, you can add a custom Phtml file in Magento 2 admin panel.
If you have any doubts, just mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with 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 Disable Filter for Column in Magento 2 Grid
How to Get All Product URLs in Magento 2
Next