How To

Magento 2: How to Add Custom Text after Billing Address and Shipping Address in Admin Order View Page?

Hello Magento Friends,

In the current article, we are going to focus on Magento 2: How to Add Custom Text after Billing Address and Shipping Address in Admin Order View Page.

With Magento 2 you can customize any part of the store. Admin order view page is where the store admin manages all orders after the order is placed by customers. Admin sales order view page in Magento 2 can be customized for better order processing.

You can Add Custom Block After Shipping and Billing Address in Admin Sales order view page in Magento 2. Same way today we will learn How to Add Custom Text after Billing Address and Shipping Address in the Admin Order View Page in Magento 2.

Steps to Add Custom Text after Billing Address and Shipping Address in Admin Order View Page in Magento 2:

Step 1:  First, we need to create a “sales_order_view.xml“ file inside our extension at the following path

app\code\Vendor\Extension\view\adminhtml\layout\

Then add the below code

<?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">
                <action method="setTemplate">
                    <argument name="template" translate="true" xsi:type="string">Vendor_Extension::order/info.phtml</argument>
                </action>
        </referenceBlock>
    </body>
</page>

Step 2:  After that, we need to create an “info.phtml” file inside our extension at the following path

app\code\Vendor\Extension\view\adminhtml\templates\order\

Now add the code as follows

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
/**
 * @var \Magento\Sales\Block\Adminhtml\Order\View\Info $block
 */$order = $block->getOrder();

$baseCurrencyCode = (string)$order->getBaseCurrencyCode();
$globalCurrencyCode = (string)$order->getGlobalCurrencyCode();
$orderCurrencyCode = (string)$order->getOrderCurrencyCode();

$orderAdminDate = $block->formatDate(
    $block->getOrderAdminDate($order->getCreatedAt()),
    \IntlDateFormatter::MEDIUM,
    true
);

$orderStoreDate = $block->formatDate(
    $order->getCreatedAt(),
    \IntlDateFormatter::MEDIUM,
    true,
    $block->getTimezoneForStore($order->getStore())
);

$customerUrl = $block->getCustomerViewUrl();

$allowedAddressHtmlTags = ['b', 'br', 'em', 'i', 'li', 'ol', 'p', 'strong', 'sub', 'sup', 'ul'];
?>

<section class="admin__page-section order-view-account-information">
    <div class="admin__page-section-title">
        <span class="title"><?= $block->escapeHtml(__('Order & Account Information')) ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-information">
            <?php /* Order Information */ ?>
            <?php $confirmationEmailStatusMessage = $order->getEmailSent() ? __('The order confirmation email was sent') : __('The order confirmation email is not sent'); ?>
            <div class="admin__page-section-item-title">
                <span class="title">
                    <?php if ($block->getNoUseOrderLink()) : ?>
                        <?= $block->escapeHtml(__('Order # %1', $order->getRealOrderId())) ?> (<span><?= $block->escapeHtml($confirmationEmailStatusMessage) ?></span>)
                    <?php else : ?>
                        <a href="<?= $block->escapeUrl($block->getViewUrl($order->getId())) ?>"><?= $block->escapeHtml(__('Order # %1', $order->getRealOrderId())) ?></a>
                        <span>(<?= $block->escapeHtml($confirmationEmailStatusMessage) ?>)</span>
                    <?php endif; ?>
                </span>
            </div>
            <div class="admin__page-section-item-content">
                <table class="admin__table-secondary order-information-table">
                <tr>
                    <th><?= $block->escapeHtml(__('Order Date')) ?></th>
                    <td><?= $block->escapeHtml($orderAdminDate) ?></td>
                </tr>
                <?php if ($orderAdminDate != $orderStoreDate) : ?>
                    <tr>
                        <th><?= $block->escapeHtml(__('Order Date (%1)', $block->getTimezoneForStore($order->getStore()))) ?></th>
                        <td><?= $block->escapeHtml($orderStoreDate) ?></td>
                    </tr>
                <?php endif;?>
                <tr>
                    <th><?= $block->escapeHtml(__('Order Status')) ?></th>
                    <td><span id="order_status"><?= $block->escapeHtml($order->getStatusLabel()) ?></span></td>
                </tr>
                <?= $block->getChildHtml() ?>
                <?php if ($block->isSingleStoreMode() == false) : ?>
                    <tr>
                        <th><?= $block->escapeHtml(__('Purchased From')) ?></th>
                        <td><?= $block->escapeHtml($block->getOrderStoreName(), ['br']) ?></td>
                    </tr>
                <?php endif; ?>
                <?php if ($order->getRelationChildId()) : ?>
                    <tr>
                        <th><?= $block->escapeHtml(__('Link to the New Order')) ?></th>
                        <td>
                            <a href="<?= $block->escapeUrl($block->getViewUrl($order->getRelationChildId())) ?>">
                                <?= $block->escapeHtml($order->getRelationChildRealId()) ?>
                            </a>
                        </td>
                    </tr>
                <?php endif; ?>
                <?php if ($order->getRelationParentId()) : ?>
                    <tr>
                        <th><?= $block->escapeHtml(__('Link to the Previous Order')) ?></th>
                        <td>
                            <a href="<?= $block->escapeUrl($block->getViewUrl($order->getRelationParentId())) ?>">
                                <?= $block->escapeHtml($order->getRelationParentRealId()) ?>
                            </a>
                        </td>
                    </tr>
                <?php endif; ?>
                <?php if ($order->getRemoteIp() && $block->shouldDisplayCustomerIp()) : ?>
                    <tr>
                        <th><?= $block->escapeHtml(__('Placed from IP')) ?></th>
                        <td><?= $block->escapeHtml($order->getRemoteIp()); ?><?= $order->getXForwardedFor() ? ' (' . $block->escapeHtml($order->getXForwardedFor()) . ')' : ''; ?></td>
                    </tr>
                <?php endif; ?>
                <?php if ($globalCurrencyCode !== $baseCurrencyCode) : ?>
                    <tr>
                        <th><?= $block->escapeHtml(__('%1 / %2 rate:', $globalCurrencyCode, $baseCurrencyCode)) ?></th>
                        <td><?= $block->escapeHtml($order->getBaseToGlobalRate()) ?></td>
                    </tr>
                <?php endif; ?>
                <?php if ($baseCurrencyCode !== $orderCurrencyCode && $globalCurrencyCode !== $orderCurrencyCode) : ?>
                    <tr>
                        <th><?= $block->escapeHtml(__('%1 / %2 rate:', $orderCurrencyCode, $baseCurrencyCode)) ?></th>
                        <td><?= $block->escapeHtml($order->getBaseToOrderRate()) ?></td>
                    </tr>
                <?php endif; ?>
            </table>
            </div>
        </div>

        <div class="admin__page-section-item order-account-information">
            <?php /* Account Information */ ?>
            <div class="admin__page-section-item-title">
                <span class="title"><?= $block->escapeHtml(__('Account Information')) ?></span>
                <div class="actions">
                    <?php if ($customerUrl) : ?>
                        <a href="<?= /* @noEscape */ $customerUrl ?>" target="_blank">
                            <?= $block->escapeHtml(__('Edit Customer')) ?>
                        </a>
                    <?php endif; ?>
                </div>
            </div>
            <div class="admin__page-section-item-content">
                <table class="admin__table-secondary order-account-information-table">
                    <tr>
                        <th><?= $block->escapeHtml(__('Customer Name')) ?></th>
                        <td>
                            <?php if ($customerUrl) : ?>
                                <a href="<?= $block->escapeUrl($customerUrl) ?>" target="_blank">
                                    <span><?= $block->escapeHtml($order->getCustomerName()) ?></span>
                                </a>
                            <?php else : ?>
                                <?= $block->escapeHtml($order->getCustomerName()) ?>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <tr>
                        <th><?= $block->escapeHtml(__('Email')) ?></th>
                        <td><a href="mailto:<?= $block->escapeHtmlAttr($order->getCustomerEmail()) ?>"><?= $block->escapeHtml($order->getCustomerEmail()) ?></a></td>
                    </tr>
                    <?php if ($groupName = $block->getCustomerGroupName()) : ?>
                        <tr>
                            <th><?= $block->escapeHtml(__('Customer Group')) ?></th>
                            <td><?= $block->escapeHtml($groupName) ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php foreach ($block->getCustomerAccountData() as $data) : ?>
                        <tr>
                            <th><?= $block->escapeHtml($data['label']) ?></th>
                            <td><?= $block->escapeHtml($data['value'], ['br']) ?></td>
                        </tr>
                    <?php endforeach;?>
                    <?= $block->getChildHtml('extra_customer_info') ?>
                </table>
            </div>
        </div>
    </div>
</section>

<section class="admin__page-section order-addresses">
    <div class="admin__page-section-title">
        <span class="title"><?= $block->escapeHtml(__('Address Information')) ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-billing-address">
            <?php /* Billing Address */ ?>
            <div class="admin__page-section-item-title">
                <span class="title"><?= $block->escapeHtml(__('Billing Address')) ?></span>
                <div class="actions"><?= /* @noEscape */ $block->getAddressEditLink($order->getBillingAddress()); ?></div>
            </div>
            <address class="admin__page-section-item-content"><?= $block->escapeHtml($block->getFormattedAddress($order->getBillingAddress()), $allowedAddressHtmlTags); ?>
                <div><strong><?php echo __('Custom Billing Text') ?></strong></div> 
            </address>
        </div>
        <?php if (!$block->getOrder()->getIsVirtual()) : ?>
            <div class="admin__page-section-item order-shipping-address">
                <?php /* Shipping Address */ ?>
                <div class="admin__page-section-item-title">
                    <span class="title"><?= $block->escapeHtml(__('Shipping Address')) ?></span>
                    <div class="actions"><?= /* @noEscape */ $block->getAddressEditLink($order->getShippingAddress()); ?></div>
                </div>
                <address class="admin__page-section-item-content"><?= $block->escapeHtml($block->getFormattedAddress($order->getShippingAddress()), $allowedAddressHtmlTags); ?>
                    <div><strong><?php echo __('Custom Shipping Text') ?></strong></div> 
                </address>
            </div>
        <?php endif; ?>
    </div>
</section>

Conclusion:

So now you have successfully added custom text after billing and shipping address in Magento 2 admin sales order view page. Alternatively, you can also Add Custom Text after Billing Address and Shipping Address in Magento 2 Credit Memo PDF.

Share the article with others and let me know if you have any doubts.

Happy Coding!

Click to rate this post!
[Total: 8 Average: 3.3]
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

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

6 days ago