Question

How to add a custom block in the sales_order_shipment_view page in backend M2? I have to override the sales_order_shipment_view.xml file in my module then I have added the below code for the override comment view.phtml is working fine but I need to add a separate template.

<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_comments">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Custom_Module::order/comments/view.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

enter image description here

Was it helpful?

Solution

If you want to create separate file, then you can do like this below way :

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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">
            <referenceBlock name="form">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">Custom_Module::view/form.phtml</argument>
                </action>
                <block class="Magento\Sales\Block\Adminhtml\Order\Comments\View" name="order_comments_new" template="Custom_Module::view/form_other.phtml"/>
            </referenceBlock>
        </referenceContainer>
    </body>
</page>

Now, copy vendor/magento/module-shipping/view/adminhtml/templates/view/form.phtml file and paste at app/code/Custom/Module/view/adminhtml/templates/view/form.phtml

After replace last section tag in this file with below code :

<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 class="admin__page-section-item order-comments-history">
            <div class="admin__page-section-item-title">
                <span class="title"><?= $block->escapeHtml(__('Custom Shipment History')); ?></span>
            </div>
            <div class="admin__page-section-item-content"><?= $block->getChildHtml('order_comments_new'); ?></div> <!-- new block call here -->
        </div>
    </div>
</section>

You can add code in form_other.phtml file which you want to display.

Output :

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top