How To

Magento 2: Add Custom Message to Admin Sales Order View, Invoice, and Credit Memo

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.

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

<?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

<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

<?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

<?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

<?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

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!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago