2

How to override the following files in our custom module

/vendor/magento/module-sales/view/adminhtml/layout/sales_order_view.xml

and

/vendor/magento/module-sales/Block/Adminhtml/Order/View/Items/Renderer/DefaultRenderer.php

thanks

4 Answers 4

1

In order to override a layout file, you just need to create a new layout file with the same name i.e. layout handle in your custom module.

Like in your case, you need to create a layout file with the name sales_order_view.xml and place that into the same same adminhtml/layout dir in your module.

app/code/{NameSpace}/{Module}/view/adminhtml/layout/sales_order_view.xml

Moving further, you can override a php file by different ways in M2. Like if you want to tweak request/response of a public method then you can use Plugin.

You can also observe an event if there is any being dispatched from the class that you wish to override.

And last, you can also use Preference to override a class. Here are some references.

http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html http://devdocs.magento.com/guides/v2.0/extension-dev-guide/events-and-observers.html https://magento.stackexchange.com/a/85291/23943

0
Create custom module for same and you can override like this

Create sales_order_view.xml file in your custom module
Vendor/Mymodule/view/adminhtml/layout/sales_order_view.xml


For example if you want to override items.phtml file then you can override like this

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

Inside Module.xml file, you have to declare dependency of core module

<?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="Vendor_Modulename" setup_version="1.0.0" active="true">
        <sequence>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>

You have to override your xml file at right place,

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

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

     <preference for="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer" type="Vendor\Modulename\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer" /> 
</config>

Create DefaultRenderer.php in

Vendor\Modulename\Block\Adminhtml\Order\View\Items\Renderer.

<?php

namespace Vendor\Modulename\Block\Adminhtml\Order\View\Items\Renderer;

class DefaultRenderer extends \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer
{

}
1
  • thanks for the reply but how can I add code in sale_order_view.xml
    – Raghu
    Nov 17, 2017 at 6:47
0

app/code/vendorname/modulename/etc/adminhtml/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer" type="vendorname\modulename\Block\Adminhtml\Order\View\Items\Renderer" />
</config>

app/code/vendorname/modulename/Block/Adminhtml/Order/View/Items/Renderer/DefaultRenderer.php

<?php

namespace Vendor\Modulename\Block\Adminhtml\Order\View\Items\Renderer;

class DefaultRenderer extends \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer
{

}

app/code/vendorname/modulename/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>
        <referenceContainer name="order_items">
            <block class="Magento\Sales\Block\Adminhtml\Order\View\Items" name="order_items" template="Magento_Sales::order/view/items.phtml">
                <arguments>
                        <argument name="columns" xsi:type="array">

                                <item name="seller_demo" xsi:type="string" translate="true">Seller</item>

                        </argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>
8
  • Hi @Zala thanks for the ewply but how can I add code in sale_order_view.xml
    – Raghu
    Nov 17, 2017 at 6:26
  • which code add sale_order_view.xml. Nov 17, 2017 at 6:33
  • above code are Override DefaultRenderer.php file. Nov 17, 2017 at 6:34
  • ok but i need to add code in sale xml also
    – Raghu
    Nov 17, 2017 at 6:38
  • which code add sale_order_view.xml. Nov 17, 2017 at 6:48

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