1

I want to add some new functionality to the order create view in adminhtml

Following magento 2 docs guide I just have to reference the block I want to extend, as far as I understood. My block is recognized just fine but in at the wrong position.

I'd like to have it at the position where the comments are placed

Here is my layout app/code/Namespace/FancyModule/view/adminhtml/layout/sales_order_create_index.xml

<?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>
        <referenceBlock name="content">
            <block class="Namespace\FancyModule\Block\Rewrite\Order\View" name="custom.order.discount" template="test.phtml" after="comment"/>
        </referenceBlock>
    </body>
</page>

and here is the magento layout including the block I'd like my bloc to appear after/before

 <referenceBlock name="content">
            <block class="Magento\Sales\Block\Adminhtml\Order\Create" name="order_content">
                <block class="Magento\Sales\Block\Adminhtml\Order\Create\Form" template="order/create/form.phtml" name="order_create_form" as="form">

                    <block class="Magento\Sales\Block\Adminhtml\Order\Create\Data" template="order/create/data.phtml" name="data">
                      <!-- THIS ONE -->
                        <block class="Magento\Sales\Block\Adminhtml\Order\Create\Comment" template="order/create/comment.phtml" name="comment"/>

                    </block>
                </block>
            </block>
        </referenceBlock>

Thanks for help

1 Answer 1

1

You'll need to override the Magento_Sales/view/adminhtml/templates/order/create/data.phtml template file. Copy this file to {module_directory}/view/adminhtml/templates/order/create/data.phtml and then find line 80 which is where the comments block is rendered. Place the custom block wherever you like with this code:

<?php echo $block->getChildHtml('custom.order.discount') ?> 

Now in {module}/view/adminhtml/layout/sales_order_create_index.xml, set the data block to use the new template and add your block to it:

<?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>
        <referenceBlock name="data">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Namespace_FancyModule::order/create/data.phtml</argument>
            </action>

            <block class="Namespace\FancyModule\Block\Rewrite\Order\View" name="custom.order.discount" template="test.phtml"/>
        </referenceBlock>
    </body>
</page>

Also, you should have Magento_Sales listed in the <sequence> element of your module.xml.

2
  • Thank you. I think that the whole magento layout extending thing is more complicated then it should be, but maybe it's just me having trouble with that
    – xhallix
    Commented Mar 23, 2017 at 18:41
  • Cases like this where the layout is baked into a template file can be problematic, but most of the time it's pretty simple to add/remove/move blocks. Commented Mar 23, 2017 at 20:41

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.