1

I was wondering is someone has any idea of why my code is not working. I'm trying to override the sales order view to show an extra item on the "Items Ordered list". For this I created a module that is replacing the blocks on the 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 class="Magento\Sales\Block\Adminhtml\Order\View\Items" name="order_items" template="Company_Module::order/view/items.phtml">
        <block class="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer" as="default" template="order/view/items/renderer/default.phtml"/>
        <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"/>

    </referenceBlock>
</body>

So far, the order_items template is overridden as expected but I can't not find a way to override the default.phtml one. I have tried with referenceBlock but it doesn't seem to be working. How could I target a block that is not using a name but an alias instead?

1
  • Nested referenceBlock seems to be ignored on Magento 2.
    – Pablo
    Commented Jun 29, 2016 at 9:05

2 Answers 2

1

With the <referenceBlock> tag I'm pretty sure you can either use the name or the alias.

So in your case, I'm pretty sure you can do the following:

<?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_items">
        <arguments>
            <argument name="template" xsi:type="string">Company_Module::order/view/items.phtml</argument>
        </arguments>
        <referenceBlock name="default">
            <arguments>
                <argument name="template" xsi:type="string">Company_Module::order/view/items/renderer/default.phtml</argument>
            </arguments>
        </referenceBlock>
    </referenceBlock>
</body>
0

I tried with this code but has do some modification.I copied sales_order_view.xml in my module.Here is my code

<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_tab_info">
        <action method="setTemplate">
            <argument name="template" translate="true" xsi:type="string">order/view/tab/info.phtml</argument>
        </action>
        <referenceBlock name="order_items">
            <action method="setTemplate">
                <argument name="template" translate="true" xsi:type="string">order/view/items.phtml</argument>
            </action>
            <referenceBlock name="default">
                <action method="setTemplate">
                    <argument name="template" translate="true" xsi:type="string">Custom_Order::order/view/items/renderer/default.phtml</argument>
                </action>                    
            </referenceBlock>
        </referenceBlock>
    </referenceBlock>
</body>

Then I copied default.phtml to my module directory, and added my custom code to display. Then I extend the dependency injection. In di.xml I given preferences for my block as

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

Now create your block DefaultRenderer.php

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

It should extend

\Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer

Then clear cache and run. It works.

Your Answer

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

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