Hello Magento Friends,
In today’s article, I will be providing a solution for Magento 2: Add Custom Message to Admin Sales Order View, Invoice, and Credit Memo.
Customization is the number one feature of Magento 2. Not only the storefront but also the admin panel can be customized as per the requirements. Adding custom elements to the admin panel makes the admin work easy and more understandable.
Admin can add custom columns, elements, messages, and components in the admin grid. It will help the admin efficiently manage and fulfill orders.
Here we will be adding a custom message to the admin sales order view, invoice, and credit memo in the Magento 2 admin grid.
Contents
How to Add Custom Message to Admin Sales Order View, Invoice, and Credit Memo in Magento 2?
Step 1: Add custom content in the order summary on the checkout page in Magento 2
Go to the below file path
app\code\Vendor\Extension\view\adminhtml\layout\sales_order_view.xml
Then add the below code
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\Extension\Block\Adminhtml\Order\View\CustomContent" name="sales_order_view_customcontent" template="order/view/custom_content.phtml"/> </referenceBlock> </body> </page> |
Now navigate to the below file path
app\code\Vendor\Extension\view\adminhtml\templates\order\view\custom_content.phtml
And add your custom content
1 |
<h3>Add any of your custom content</h3> |
Next move to the following file path
app\code\Vendor\Extension\Block\Adminhtml\Order\View\ CustomContent.php
Then add the code as follows
1 2 3 4 5 6 7 8 |
<?php namespace Vendor\Extension\Block\Adminhtml\Order\View; class CustomContent extends \Magento\Backend\Block\Template { // Here you can fetch order detail and add conditions as per your requirement and can use in template file } |
Step 2: Add a custom message to the invoice in Magento 2
For that, you need to create an invoice layout file
Go to the below file path
app\code\Vendor\Extension\view\adminhtml\layout\sales_order_invoice_view.xml
Add the code mentioned below
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\Extension\Block\Adminhtml\Order\View\CustomContent" name="sales_invoice_view_customcontent" template="order/view/custom_content.phtml"/> </referenceBlock> </body> </page> |
Step 3: Add a custom message to the credit memo in Magento 2
You need to create a credit memo layout file
Navigate to the below-mentioned file path
app\code\Vendor\Extension\view\adminhtml\layout\sales_order_creditmemo_view.xml
Append the code as follows
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="sales_creditmemo_view_customcontent" template="order/view/custom_content.phtml"/> </referenceBlock> </body> </page> |
Step 4: Run CLI command in Magento 2
Once the above files are created, kindly run the below command
1 |
sudo php bin/magento cache:flush |
Result:
After performing all the steps successfully, you can see that your custom message is added.
Custom message on Magento 2 Admin sales order view
Custom message on Magento 2 invoice
Custom message on Magento 2 credit memo
Conclusion:
Accordingly you can include your custom message in the admin sales order view, invoice, and credit memo in Magento 2.
Add more customization to the admin sales order view with the below elements
Need any further customization for your Magento 2 store? Contact us
Happy Coding!