0

How to add custom block under payment method(admin order invoice)

enter image description here

  • Do you want to add static block? – Sukumar Gorai Dec 27 '18 at 10:29
  • no ... ......... – Mano M Dec 27 '18 at 10:33
2

Create a module with name Vendor_Module by following below steps:

Step 1: Create registration.php

app/code/Vendor/Module/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Step 2: Create module.xml

app/code/Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="0.0.1">
        <sequence>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>

Step 3: Create sales_order_invoice_view.xml

app/code/Vendor/Module/view/adminhtml/layout/sales_order_invoice_view.xml

<?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="form" template="Vendor_Module::invoice/view/form.phtml"/>
        <referenceBlock name="form">
            <block class="Vendor\Module\Block\Adminhtml\Invoice" name="custom.invoice" as="custom_invoice" template="Vendor_Module::invoice/view/invoice.phtml"/>
        </referenceBlock>
    </body>
</page>

Step 4: Create form.phtml

app/code/Vendor/Module/view/adminhtml/templates/invoice/view/form.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php $_invoice = $block->getInvoice() ?>
<?php $_order = $_invoice->getOrder() ?>
<?= $block->getChildHtml('order_info') ?>

<section class="admin__page-section order-view-billing-shipping">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Payment &amp; Shipping Method') ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-payment-method<?php if ($_order->getIsVirtual()): ?> order-payment-method-virtual<?php endif; ?> admin__fieldset-wrapper">
            <?php /*Billing Address */ ?>
            <div class="admin__page-section-item-title">
                <span class="title"><?= /* @escapeNotVerified */ __('Payment Information') ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <div class="order-payment-method-title"><?= $block->getChildHtml('order_payment') ?></div>
                <div class="order-payment-currency">
                    <?= /* @escapeNotVerified */ __('The order was placed using %1.', $_order->getOrderCurrencyCode()) ?>
                </div>
                <div class="order-payment-additional"><?= $block->getChildHtml('order_payment_additional') ?></div>
            </div>
        </div>

        <?php if (!$_order->getIsVirtual()): ?>
            <div class="admin__page-section-item order-shipping-address">
                <?php /*Shipping Address */ ?>
                <div class="admin__page-section-item-title">
                    <span class="title"><?= /* @escapeNotVerified */ __('Shipping Information') ?></span>
                </div>
                <div class="admin__page-section-item-content shipping-description-wrapper">
                    <div class="shipping-description-title">
                        <?= $block->escapeHtml($_order->getShippingDescription()) ?>
                    </div>
                    <div class="shipping-description-content">
                        <?= /* @escapeNotVerified */ __('Total Shipping Charges') ?>:

                        <?php if ($this->helper('Magento\Tax\Helper\Data')->displayShippingPriceIncludingTax()): ?>
                            <?php $_excl = $block->displayShippingPriceInclTax($_order); ?>
                        <?php else: ?>
                            <?php $_excl = $block->displayPriceAttribute('shipping_amount', false, ' '); ?>
                        <?php endif; ?>
                        <?php $_incl = $block->displayShippingPriceInclTax($_order); ?>

                        <?= /* @escapeNotVerified */ $_excl ?>
                        <?php if ($this->helper('Magento\Tax\Helper\Data')->displayShippingBothPrices() && $_incl != $_excl): ?>
                            (<?= /* @escapeNotVerified */ __('Incl. Tax') ?> <?= /* @escapeNotVerified */ $_incl ?>)
                        <?php endif; ?>
                        <div><?= $block->getChildHtml('shipment_tracking') ?></div>
                    </div>
                </div>
            </div>
        <?php endif; ?>

    </div>
</section>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Custom Title') ?></span>
    </div>
    <div id="invoice_custom_container" class="admin__page-section-content">
        <?= $block->getChildHtml('custom_invoice') ?>
    </div>
</section>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Items Invoiced') ?></span>
    </div>

    <div id="invoice_item_container" class="admin__page-section-content">
        <?= $block->getChildHtml('invoice_items') ?>
    </div>
</section>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Order Total') ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-comments-history">
            <div class="admin__page-section-item-title">
                <span class="title"><?= /* @escapeNotVerified */ __('Invoice History') ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <?= $block->getChildHtml('order_comments') ?>
            </div>
        </div>

        <div id="history_form" class="admin__page-section-item order-totals">
            <div class="admin__page-section-item-title">
                <span class="title"><?= /* @escapeNotVerified */ __('Invoice Totals') ?></span>
            </div>
            <?= $block->getChildHtml('invoice_totals') ?>
        </div>
    </div>
</section>

Step 5: Create invoice.phtml

app/code/Vendor/Module/view/adminhtml/templates/invoice/view/invoice.phtml

<?= $block->getText() ?>

Step 6: Create Invoice.php

app/code/Vendor/Module/Block/Adminhtml/Invoice.php

<?php
namespace Vendor\Module\Block\Adminhtml;
use Magento\Framework\View\Element\Template;

class Invoice extends \Magento\Framework\View\Element\Template
{
    public function __construct(Template\Context $context, array $data = array())
    {
        parent::__construct($context, $data);
    }

    public function getText(){
        return 'Custom text';
    }
}

Step 7: Create adminhtml_order_shipment_view.xml

app/code/Vendor/Module/view/adminhtml/layout/adminhtml_order_shipment_view.xml

<?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="form" template="Vendor_Module::shipping/view/form.phtml">
            <block class="Vendor\Module\Block\Adminhtml\Shipping" name="custom.shipping" as="custom_shipping" template="Vendor_Module::shipping/view/shipping.phtml"/>
        </referenceBlock>
    </body>
</page>

Step 8: Create form.phtml

app/code/Vendor/Module/view/adminhtml/templates/shipping/view/form.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile
/**
 * @var \Magento\Shipping\Block\Adminhtml\View\Form $block
 */
$order = $block->getShipment()->getOrder();
?>
<?= $block->getChildHtml('order_info'); ?>
<section class="admin__page-section order-shipment-billing-shipping">
    <div class="admin__page-section-title">
        <span class="title"><?= $block->escapeHtml(__('Payment &amp; Shipping Method')); ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-payment-method">
            <div class="admin__page-section-item-title">
                <span class="title"><?= $block->escapeHtml(__('Payment Information')); ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <div><?= $block->getChildHtml('order_payment') ?></div>
                <div class="order-payment-currency">
                    <?= $block->escapeHtml(__('The order was placed using %1.', $order->getOrderCurrencyCode())); ?>
                </div>
            </div>
        </div>

        <div class="admin__page-section-item order-shipping-address">
            <div class="admin__page-section-item-title">
                <span class="title"><?= $block->escapeHtml(__('Shipping and Tracking Information')); ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <div class="shipping-description-wrapper">
                    <?php if ($block->getShipment()->getTracksCollection()->count()): ?>
                        <p>
                            <a href="#" id="linkId" onclick="popWin('<?= $block->escapeUrl($this->helper('Magento\Shipping\Helper\Data')->getTrackingPopupUrlBySalesModel($block->getShipment())); ?>','trackshipment','width=800,height=600,resizable=yes,scrollbars=yes')"
                               title="<?= $block->escapeHtml(__('Track this shipment')); ?>">
                                <?= $block->escapeHtml(__('Track this shipment')); ?>
                            </a>
                        </p>
                    <?php endif; ?>
                    <div class="shipping-description-title">
                        <?= $block->escapeHtml($order->getShippingDescription()); ?>
                    </div>

                    <?= $block->escapeHtml(__('Total Shipping Charges')); ?>:

                    <?php if ($this->helper('Magento\Tax\Helper\Data')->displayShippingPriceIncludingTax()): ?>
                        <?php $excl = $block->displayShippingPriceInclTax($order); ?>
                    <?php else: ?>
                        <?php $excl = $block->displayPriceAttribute('shipping_amount', false, ' '); ?>
                    <?php endif; ?>
                    <?php $incl = $block->displayShippingPriceInclTax($order); ?>

                    <?= /* @noEscape */ $excl; ?>
                    <?php if ($this->helper('Magento\Tax\Helper\Data')->displayShippingBothPrices() && $incl != $excl): ?>
                        (<?= $block->escapeHtml(__('Incl. Tax')); ?> <?= /* @noEscape */ $incl; ?>)
                    <?php endif; ?>
                </div>

                <p>
                    <?php if ($block->canCreateShippingLabel()): ?>
                        <?= /* @noEscape */ $block->getCreateLabelButton(); ?>
                    <?php endif ?>
                    <?php if ($block->getShipment()->getShippingLabel()): ?>
                        <?= /* @noEscape */ $block->getPrintLabelButton(); ?>
                    <?php endif ?>
                    <?php if ($block->getShipment()->getPackages()): ?>
                        <?= /* @noEscape */ $block->getShowPackagesButton(); ?>
                    <?php endif ?>
                </p>
                <?= $block->getChildHtml('shipment_tracking'); ?>

                <?= $block->getChildHtml('shipment_packaging'); ?>
                <script>
                    require([
                        'jquery',
                        'prototype'
                    ], function (jQuery) {
                        var setCallbacks = function () {
                            window.packaging.setConfirmPackagingCallback(function () {
                                window.packaging.sendCreateLabelRequest();
                            });
                            window.packaging.setLabelCreatedCallback(function () {
                                setLocation("<?php echo $block->escapeUrl($block->getUrl(
                                    'adminhtml/order_shipment/view',
                                    ['shipment_id' => $block->getShipment()->getId()])
                                ); ?>");
                            });
                        };

                        if (jQuery(document).data('packagingInited')) {
                            setCallbacks();
                        } else {
                            jQuery(document).on('packaging:inited', setCallbacks);
                        }
                    });
                </script>
            </div>
        </div>
    </div>
</section>
<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Custom Title') ?></span>
    </div>
    <div id="invoice_custom_container" class="admin__page-section-content">
        <?= $block->getChildHtml('custom_shipping') ?>
    </div>
</section>
<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= $block->escapeHtml(__('Items Shipped')); ?></span>
    </div>
    <?= $block->getChildHtml('shipment_items'); ?>
</section>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= $block->escapeHtml(__('Order Total')); ?></span>
    </div>
    <div class="admin__page-section-content">
        <?= $block->getChildHtml('shipment_packed'); ?>

        <div class="admin__page-section-item order-comments-history">
            <div class="admin__page-section-item-title">
                <span class="title"><?= $block->escapeHtml(__('Shipment History')); ?></span>
            </div>
            <div class="admin__page-section-item-content"><?= $block->getChildHtml('order_comments'); ?></div>
        </div>
    </div>
</section>

Step 9: Create shipping.phtml

app/code/Vendor/Module/view/adminhtml/templates/shipping/view/shipping.phtml

<?= $block->getText() ?>

Step 10: Create Shipping.php

app/code/Vendor/Module/Block/Adminhtml/Shipping.php

<?php
namespace Vendor\Module\Block\Adminhtml;
use Magento\Framework\View\Element\Template;

class Shipping extends \Magento\Framework\View\Element\Template
{
    public function __construct(Template\Context $context, array $data = array())
    {
        parent::__construct($context, $data);
    }

    public function getText(){
        return 'Custom text';
    }
}

Step 11: Create sales_order_creditmemo_view.xml

app/code/Vendor/Module/view/adminhtml/layout/sales_order_creditmemo_view.xml

<?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="form" template="Vendor_Module::creditmemo/view/form.phtml">
            <block class="Vendor\Module\Block\Adminhtml\Creditmemo" name="custom.creditmemo" as="custom_creditmemo" template="Vendor_Module::creditmemo/view/creditmemo.phtml"/>
        </referenceBlock>
    </body>
</page>

Step 12: Create form.phtml

app/code/Vendor/Module/view/adminhtml/templates/creditmemo/view/form.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php  $_order = $block->getCreditmemo()->getOrder() ?>
<?= $block->getChildHtml('order_info') ?>
<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Payment &amp; Shipping Method') ?></span>
    </div>
    <div class="admin__page-section-content">

        <?php if (!$_order->getIsVirtual()): ?>
        <div class="admin__page-section-item order-payment-method">
        <?php else: ?>
        <div class="admin__page-section-item order-payment-method order-payment-method-virtual">
        <?php endif; ?>
            <?php /* Billing Address */?>
            <div class="admin__page-section-item-title">
                <span class="title"><?= /* @escapeNotVerified */ __('Payment Information') ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <div class="order-payment-method-title"><?= $block->getChildHtml('order_payment') ?></div>
                <div class="order-payment-currency"><?= /* @escapeNotVerified */ __('The order was placed using %1.', $_order->getOrderCurrencyCode()) ?></div>
                <div class="order-payment-additional"><?= $block->getChildHtml('order_payment_additional') ?></div>
            </div>
        </div>

        <?php if (!$_order->getIsVirtual()): ?>
        <div class="admin__page-section-item order-shipping-address">
            <?php /* Shipping Address */ ?>
            <div class="admin__page-section-item-title">
                <span class="title"><?= /* @escapeNotVerified */ __('Shipping Information') ?></span>
            </div>
            <div class="shipping-description-wrapper admin__page-section-item-content">
                <div class="shipping-description-title"><?= $block->escapeHtml($_order->getShippingDescription()) ?></div>
                <div class="shipping-description-content">
                    <?= /* @escapeNotVerified */ __('Total Shipping Charges') ?>:

                    <?php if ($this->helper('Magento\Tax\Helper\Data')->displayShippingPriceIncludingTax()): ?>
                        <?php $_excl = $block->displayShippingPriceInclTax($_order); ?>
                    <?php else: ?>
                        <?php $_excl = $block->displayPriceAttribute('shipping_amount', false, ' '); ?>
                    <?php endif; ?>
                    <?php $_incl = $block->displayShippingPriceInclTax($_order); ?>

                    <?= /* @escapeNotVerified */ $_excl ?>
                    <?php if ($this->helper('Magento\Tax\Helper\Data')->displayShippingBothPrices() && $_incl != $_excl): ?>
                        (<?= /* @escapeNotVerified */ __('Incl. Tax') ?> <?= /* @escapeNotVerified */ $_incl ?>)
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</section>
<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Custom Title') ?></span>
    </div>
    <div id="invoice_custom_container" class="admin__page-section-content">
        <?= $block->getChildHtml('custom_creditmemo') ?>
    </div>
</section>
<?php $_items = $block->getCreditmemo()->getAllItems() ?>

<?php if (count($_items)): ?>
<div id="creditmemo_items_container">
    <?= $block->getChildHtml('creditmemo_items') ?>
</div>
<?php else: ?>
<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Items Refunded') ?></span>
    </div>
    <div class="no-items admin__page-section-content"><?= /* @escapeNotVerified */ __('No Items') ?></div>
</section>
<?php endif; ?>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Memo Total') ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-comments-history">
            <div class="admin__page-section-item-title">
                <span class="title"><?= /* @escapeNotVerified */ __('Credit Memo History') ?></span>
            </div>
            <div class="admin__page-section-item-content"><?= $block->getChildHtml('order_comments') ?></div>
        </div>
        <div class="admin__page-section-item order-totals" id="history_form">
            <div class="admin__page-section-item-title">
                <span class="title"><?= /* @escapeNotVerified */ __('Credit Memo Totals') ?></span>
            </div>
            <div class="admin__page-section-content"><?= $block->getChildHtml('creditmemo_totals') ?></div>
        </div>
    </div>
</section>

Step 13: Create creditmemo.phtml

app/code/Vendor/Module/view/adminhtml/templates/creditmemo/view/creditmemo.phtml

<?= $block->getText() ?>

Step 14: Create Creditmemo.php

app/code/Vendor/Module/Block/Adminhtml/Creditmemo.php

<?php
namespace Vendor\Module\Block\Adminhtml;
use Magento\Framework\View\Element\Template;

class Creditmemo extends \Magento\Framework\View\Element\Template
{
    public function __construct(Template\Context $context, array $data = array())
    {
        parent::__construct($context, $data);
    }

    public function getText(){
        return 'Custom text';
    }
}

Step 15: Create sales_order_view.xml

app/code/Vendor/Module/view/adminhtml/layout/sales_order_view.xml

<?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_tab_info" template="Vendor_Module::order/view/tab/info.phtml">
            <block class="Vendor\Module\Block\Adminhtml\Order" name="custom.order" as="custom_order" template="Vendor_Module::order/view/order.phtml"/>
        </referenceBlock>
    </body>
</page>

Step 16: Create info.phtml

app/code/Vendor/Module/view/adminhtml/templates/order/view/tab/info.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php /** @var $block \Magento\Sales\Block\Adminhtml\Order\View\Tab\Info */ ?>
<?php $_order = $block->getOrder() ?>

<div id="order-messages">
    <?= $block->getChildHtml('order_messages') ?>
</div>

<?= $block->getChildHtml('order_info') ?>
<input type="hidden" name="order_id" value="<?= /* @escapeNotVerified */ $_order->getId() ?>"/>

<section class="admin__page-section order-view-billing-shipping">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Payment &amp; Shipping Method') ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-payment-method<?php if ($_order->getIsVirtual()): ?> order-payment-method-virtual<?php endif; ?>">
            <?php /* Payment Method */ ?>
            <div class="admin__page-section-item-title">
                <span class="title"><?= /* @escapeNotVerified */ __('Payment Information') ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <div class="order-payment-method-title"><?= $block->getPaymentHtml() ?></div>
                <div class="order-payment-currency"><?= /* @escapeNotVerified */ __('The order was placed using %1.', $_order->getOrderCurrencyCode()) ?></div>
                <div class="order-payment-additional">
                    <?= $block->getChildHtml('order_payment_additional') ?>
                    <?= $block->getChildHtml('payment_additional_info') ?>
                </div>
            </div>
        </div>
        <?= $block->getChildHtml('order_shipping_view') ?>
    </div>
</section>
<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Custom Title') ?></span>
    </div>
    <div id="invoice_custom_container" class="admin__page-section-content">
        <?= $block->getChildHtml('custom_order') ?>
    </div>
</section>
<?= $block->getChildHtml('order_additional_info') ?>

<?= $block->getGiftOptionsHtml() ?>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Items Ordered') ?></span>
    </div>
    <?= $block->getItemsHtml() ?>
</section>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= /* @escapeNotVerified */ __('Order Total') ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-comments-history">
            <div class="admin__page-section-item-title">
                <span class="title"><?= /* @escapeNotVerified */ __('Notes for this Order') ?></span>
            </div>
            <?= $block->getChildHtml('order_history') ?>
        </div>

        <div class="admin__page-section-item order-totals">
            <div class="admin__page-section-item-title">
                <span class="title"><?= /* @escapeNotVerified */ __('Order Totals') ?></span>
            </div>
            <?= $block->getChildHtml('order_totals') ?>
        </div>
    </div>
</section>

<?= $block->getChildHtml('popup_window') ?>

<script>
require([
    "prototype",
    "Magento_Sales/order/giftoptions_tooltip"
], function(){

//<![CDATA[
    /**
     * Retrieve gift options tooltip content
     */
    function getGiftOptionsTooltipContent(itemId) {
        var contentLines = [];
        var headerLine = null;
        var contentLine = null;

        $$('#gift_options_data_' + itemId + ' .gift-options-tooltip-content').each(function (element) {
            if (element.down(0)) {
                headerLine = element.down(0).innerHTML;
                contentLine = element.down(0).next().innerHTML;
                if (contentLine.length > 30) {
                    contentLine = contentLine.slice(0,30) + '...';
                }
                contentLines.push(headerLine + ' ' + contentLine);
            }
        });
        return contentLines.join('<br/>');
    }
    giftOptionsTooltip.setTooltipContentLoaderFunction(getGiftOptionsTooltipContent);
    window.getGiftOptionsTooltipContent = getGiftOptionsTooltipContent;
//]]>

});
</script>

Step 17: Create order.phtml

app/code/Vendor/Module/view/adminhtml/templates/order/view/order.phtml

<?= $block->getText() ?>

Step 18: Create Order.php

app/code/Vendor/Module/Block/Adminhtml/Order.php

<?php
namespace Vendor\Module\Block\Adminhtml;
use Magento\Framework\View\Element\Template;

class Order extends \Magento\Framework\View\Element\Template
{
    public function __construct(Template\Context $context, array $data = array())
    {
        parent::__construct($context, $data);
    }

    public function getText(){
        return 'Custom text';
    }
}

Thats it!. Now run setup upgrade and other required commands and check.

  • I want to add some text under payment method in following layout files like sales_order_view, sales_order_invoice,sales_order_invoice_view, sales_order_creditmemo – Mano M Dec 27 '18 at 12:07
  • Does the above is working for invoice view? Can you add a screenshot how you want? – Sukumar Gorai Dec 27 '18 at 12:10
  • yes working....... – Mano M Dec 27 '18 at 12:13
  • So you want the same thing for others as well. Right? – Sukumar Gorai Dec 27 '18 at 12:14
  • yes ...like adding fee in total section of all layout files – Mano M Dec 27 '18 at 12:17
1

You can use Magento 2 plugin system to show custom HTML block or text under payment information in the backend. It shows on Orders, Invoices, Shipments and Credit Memos just by changing one method.

Here, I just created a plugin class MilanDev\PaymentText\Plugin\Magento\Framework\View\Element\AbstractBlock and append custom text in the afterGetChildHtml method while order_payment block is loaded. Please follow the codes below:

app/code/MilanDev/PaymentText/etc/module.xml

<?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="MilanDev_PaymentText" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Framework"/>
        </sequence>
    </module>
</config>

app/code/MilanDev/PaymentText/etc/adminhtml/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Element\AbstractBlock">
        <plugin disabled="false" name="MilanDev_PaymentText_Plugin_Magento_Framework_View_Element_AbstractBlock" sortOrder="10" type="MilanDev\PaymentText\Plugin\Magento\Framework\View\Element\AbstractBlock"/>
    </type>
</config>

app/code/MilanDev/PaymentText/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'MilanDev_PaymentText',
    __DIR__
);

app/code/MilanDev/PaymentText/Plugin/Magento/Framework/View/Element/AbstractBlock.php

<?php
namespace MilanDev\PaymentText\Plugin\Magento\Framework\View\Element;

    class AbstractBlock
    {

        public function afterGetChildHtml(
            \Magento\Framework\View\Element\AbstractBlock $subject,
            $result
        ) {

            // you can pull the custom text dynamically here
            $customText = '<p style="color:red;">Custom text for payment information!</p>'; 

            if ($subject->getNameInLayout() == 'order_payment') {
                return $result . $customText;
            } 

            return $result;
        }
    }
  • How to add template for this block – Mano M Dec 28 '18 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.