Magento 2 – How to Add Content to Order View

magento2sales-order

I'm trying to add content to Magento 2 Admin page; specifically the "Order and Account" section of the Information page in "Order View".

I've done the following but my content is not rendering

  1. created my_module (this is working – I see it in enabled modules and other functions are working)
  2. Created layout file within my_module

    <?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>
        <referenceContainer name="content">
            <block class="Magento\Sales\Block\Adminhtml\Order\View\Info" name="my_module_name" template="my_module::list.phtml"/>
        </referenceContainer>
    </body>
</page>
  1. Created template file within my_module (my\module\view\adminhtml\templates\list.phtml)

<?php ?>

<h1><?php echo __('Latest Orders') ?></h1>
<?php /** @var $post \\Model\Post */ echo 'hello';?>

The above template does not appear on the Order admin page. What am I doing wrong?

Best Answer

try this

Learning/RewriteSales/Block/Adminhtml/Order/View/Custom.php

<?php
namespace Learning\RewriteSales\Block\Adminhtml\Order\View;
class Custom extends \Magento\Backend\Block\Template
{

}

Learning/RewriteSales/view/adminhtml/layout/sales_order_view.xml

<?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="Learning\RewriteSales\Block\Adminhtml\Order\View\Custom" name="sales_order_view_custom" template="order/view/custom.phtml" />
        </referenceBlock>
    </body>
</page>

Learning/RewriteSales/view/adminhtml/templates/order/view/custom.phtml

<h1>Hi, I am here!</h1>

clear the cache and run it.