2

I have overridden the core layout and template files using custom theme in Magento 2

And I have my block file at Netenrich\Oms\Block\Adminhtml\Order\View

template file at [email protected] has sample text

So I have sales_order_view.xml and i have added this code snippet at the end of sales_order_view.xml

    <referenceContainer name="content">
        <block class="Magento\Sales\Block\Adminhtml\Order\View" name="sales_order_edit"/>
    </referenceContainer>
    <referenceContainer name="left">
        <block class="Magento\Sales\Block\Adminhtml\Order\View\Tabs" name="sales_order_tabs">
            <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Info" name="order_tab_info" template="order/view/tab/info.phtml">
                <block class="Magento\Sales\Block\Adminhtml\Order\View\Messages" name="order_messages"/>
                <block class="Netenrich\Oms\Block\Adminhtml\Order\View\View" name="sales_order_view_custom" template="order/view/custom.phtml" />
                <block class="Magento\Sales\Block\Adminhtml\Order\View\Info" name="order_info" template="order/view/info.phtml"/>
                <block class="Magento\Sales\Block\Adminhtml\Order\View\Items" name="order_items" template="order/view/items.phtml">
                    <arguments>
                        <argument name="columns" xsi:type="array">
                            <item name="product" xsi:type="string" translate="true">Product</item>
                            <item name="status" xsi:type="string" translate="true">Item Status</item>
                            <item name="price-original" xsi:type="string" translate="true">Original Price</item>
                            <item name="price" xsi:type="string" translate="true">Price</item>
                            <item name="ordered-qty" xsi:type="string" translate="true">Qty</item>
                            <item name="subtotal" xsi:type="string" translate="true">Subtotal</item>
                            <item name="tax-amount" xsi:type="string" translate="true">Tax Amount</item>
                            <item name="tax-percent" xsi:type="string" translate="true">Tax Percent</item>
                            <item name="discont" xsi:type="string" translate="true">Discount Amount</item>
                            <item name="total" xsi:type="string" translate="true">Row Total</item>
                        </argument>
                    </arguments>
                    <block class="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer" as="default" template="order/view/items/renderer/default.phtml">
                    <arguments>
                        <argument name="columns" xsi:type="array">
                            <item name="product" xsi:type="string" translate="false">col-product</item>
                            <item name="status" xsi:type="string" translate="false">col-status</item>
                            <item name="price-original" xsi:type="string" translate="false">col-price-original</item>
                            <item name="price" xsi:type="string" translate="false">col-price</item>
                            <item name="qty" xsi:type="string" translate="false">col-ordered-qty</item>
                            <item name="subtotal" xsi:type="string" translate="false">col-subtotal</item>
                            <item name="tax-amount" xsi:type="string" translate="false">col-tax-amount</item>
                            <item name="tax-percent" xsi:type="string" translate="false">col-tax-percent</item>
                            <item name="discont" xsi:type="string" translate="false">col-discont</item>
                            <item name="total" xsi:type="string" translate="false">col-total</item>
                        </argument>
                    </arguments>
                    </block>
                    <block class="Magento\Sales\Block\Adminhtml\Items\Column\Qty" name="column_qty" template="items/column/qty.phtml" group="column"/>
                    <block class="Magento\Sales\Block\Adminhtml\Items\Column\Name" name="column_name" template="items/column/name.phtml" group="column"/>
                    <block class="Magento\Framework\View\Element\Text\ListText" name="order_item_extra_info"/>
                </block>

                <container name="payment_additional_info" htmlTag="div" htmlClass="order-payment-additional" />
                <block class="Magento\Sales\Block\Adminhtml\Order\Payment" name="order_payment"/>
                <block class="Magento\Sales\Block\Adminhtml\Order\View\History" name="order_history" template="order/view/history.phtml"/>
                <block class="Magento\Backend\Block\Template" name="gift_options" template="Magento_Sales::order/giftoptions.phtml">
                    <block class="Magento\Sales\Block\Adminhtml\Order\View\Giftmessage" name="order_giftmessage" template="order/view/giftmessage.phtml"/>
                </block>
                <block class="Magento\Sales\Block\Adminhtml\Order\Totals" name="order_totals" template="order/totals.phtml">
                    <block class="Magento\Sales\Block\Adminhtml\Order\Totals\Tax" name="tax" template="order/totals/tax.phtml"/>
                </block>
            </block>
            <action method="addTab">
                <argument name="name" xsi:type="string">order_info</argument>
                <argument name="block" xsi:type="string">order_tab_info</argument>
            </action>
            <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Invoices" name="sales_order_invoice.grid.container"/>
            <action method="addTab">
                <argument name="name" xsi:type="string">order_invoices</argument>
                <argument name="block" xsi:type="string">sales_order_invoice.grid.container</argument>
            </action>
            <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Creditmemos" name="sales_order_creditmemo.grid.container"/>
            <action method="addTab">
                <argument name="name" xsi:type="string">order_creditmemos</argument>
                <argument name="block" xsi:type="string">sales_order_creditmemo.grid.container</argument>
            </action>
            <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Shipments" name="sales_order_shipment.grid.container"/>
            <action method="addTab">
                <argument name="name" xsi:type="string">order_shipments</argument>
                <argument name="block" xsi:type="string">sales_order_shipment.grid.container</argument>
            </action>
            <action method="addTab">
                <argument name="name" xsi:type="string">order_history</argument>
                <argument name="block" xsi:type="string">Magento\Sales\Block\Adminhtml\Order\View\Tab\History</argument>
            </action>
            <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Transactions" name="sales_transactions.grid.container"/>
            <action method="addTab">
                <argument name="name" xsi:type="string">order_transactions</argument>
                <argument name="block" xsi:type="string">sales_transactions.grid.container</argument>
            </action>
        </block>
    </referenceContainer>
    <referenceBlock name="head.components">
        <block class="Magento\Framework\View\Element\Js\Components" name="sales_page_head_components" template="Magento_Sales::page/js/components.phtml"/>
    </referenceBlock>
    <referenceBlock name="sales_order_invoice.grid.container">
        <uiComponent name="sales_order_view_invoice_grid"/>
    </referenceBlock>
    <referenceBlock name="sales_order_creditmemo.grid.container">
        <uiComponent name="sales_order_view_creditmemo_grid"/>
    </referenceBlock>
    <referenceBlock name="sales_order_shipment.grid.container">
        <uiComponent name="sales_order_view_shipment_grid"/>
    </referenceBlock>

    <referenceBlock name="order_info">
        <block class="Netenrich\Oms\Block\Adminhtml\Order\View\View" name="sales_order_view_custom" template="order/view/custom.phtml" />
    </referenceBlock>


    <referenceContainer name="left">
       <referenceContainer name="payment_additional_info">
        <block class="Netenrich\Oms\Block\Adminhtml\Order\View\Custom" name="sales_order_view_ad" template="Magento_Sales:/order/view/custom.phtml"/>
         </referenceContainer>
    </referenceContainer>

</body>

=> custom.php

namespace Netenrich\Oms\Block\Adminhtml\Order\View;
class View extends \Magento\Backend\Block\Template
{
    //
}

app/design/adminhtml/Netenrich/oms/Magento_Sales/templates/order/view/custom.phtml

<h>hi..hello</>
9
  • are you overriding or trying to inserting your custom new block?
    – Bojjaiah
    Mar 9, 2017 at 10:09
  • NO..where we have to create di.xml for what purpose.
    – vijay b
    Mar 9, 2017 at 10:11
  • are you overriding or trying to inserting your custom new block?
    – Bojjaiah
    Mar 9, 2017 at 10:12
  • i haved overridden the layout file already and trying to add new custom block to the overridden layout
    – vijay b
    Mar 9, 2017 at 10:14
  • post your full of code, so that we will update you where you went wrong?
    – Bojjaiah
    Mar 9, 2017 at 10:16

3 Answers 3

11

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

app/design/adminhtml/Netenrich/oms/Magento_Sales/templates/order/view/info.phtml‌​

<?php echo $block->getChildHtml('sales_order_view_custom') ?>

after completing this try to execute setup:upgrade and setup:static-content:deploy and Open any order.

7
  • change app/design/adminhtml/Netenrich/oms/Magento_Sales/templates/order/view/custom.phtml into app/design/adminhtml/Netenrich/oms/Magento_Sales/templates/order/view/info.phtml.
    – vijay b
    Mar 9, 2017 at 11:47
  • Yes I missed it.
    – Bojjaiah
    Mar 9, 2017 at 12:10
  • how can we get the customer id in our Learning/RewriteSales/Block/Adminhtml/Order/View/Custom.php.If we could get the order id or customer id in this custom.php .we can insert any data of respective customer dynamically
    – vijay b
    Mar 9, 2017 at 12:15
  • i got the answer .. extend the \Magento\Sales\Block\Adminhtml\Order\View\Info in custom.php.-->class View extends \Magento\Sales\Block\Adminhtml\Order\View\Info..So we can use the core methods from \Magento\Sales\Block\Adminhtml\Order\View\Info which has the methods to get the order details
    – vijay b
    Mar 9, 2017 at 12:20
  • @vijayb do you want to answer how to get customer detail from custom.php or you got answer from somewhere?
    – Bojjaiah
    Mar 9, 2017 at 12:42
3

This would be much easier, In sales_order_view.xml

 <referenceBlock name="order_additional_info">
        <block class="Vendor\Module\Block\Adminhtml\Order\View" name="sales_order_custom_view" template="custom_view.phtml" />
 </referenceBlock>

Then create custom_view.phtml and place your custom code there. Your code block will reflect before the Item section block in the admin order view interface.

In info.phtml in core file, already they allocated a section for additional custom blocks,

<?= $block->getChildHtml('order_additional_info') ?>
1
  • Which folder should the custom_view.phtml be put in?
    – user68878
    May 4, 2020 at 12:13
1

Magento 2.3 onwards

sales_order_view.xml

<referenceBlock name="order_additional_info">
            <container name="basys_info" htmlTag="div" htmlClass="basys_info">
                <block name="basysinfo.viewmodel" template="BA_BasysOrders::order/view/basysinfo.phtml">
                    <arguments>
                        <argument name="view_model" xsi:type="object">BA\BasysOrders\ViewModel\BasysInfo</argument>
                    </arguments>
                </block>
            </container>
        </referenceBlock>

View Model Class

    <?php
namespace BA\BasysOrders\ViewModel;

use Psr\Log\LoggerInterface;
use Magento\Framework\App\Request\Http;

class BasysInfo implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
    protected $logger;
    protected $request;
    protected $order;

    public function __construct(
        LoggerInterface $logger,
        Http $request,
        \Magento\Sales\Api\OrderRepositoryInterface $order
    ) {
        $this->logger = $logger;
        $this->request = $request;
        $this->order = $order;
    }
    public function getOrderId()
    {
        $this->logger->info('order id '.$this->request->getParam('order_id'));
        $order = $this->order->get(60);
        $this->logger->info('basys order id '.$order->getData('basys_order_id'));
        return $order->getData('basys_order_id');
    }
}

Phtml template

    <?php
    $viewModel = $block->getViewModel();
?>
<h3>Additional Information</h3>
<table>
    <tr>
        <td>Order Id : <?= $block->escapeHtml($viewModel->getOrderId()); ?></td>
    </tr>
</table>
1
  • Isn't this sample supposed to use getOrderId() differently? What is the point of using a static $this->order->get(60) static value? Isn't the dynamic order_id stored in the request? $this->request->getParam('order_id'). Was this just an oversight, otherwise how do we get/use the order id? Aug 5, 2021 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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