4

I need to add custom block after order comment section in admin order create form using version(magento2.2.5)

7

First,you have to create a custom module.On your custom

Create sales_order_create_index.xml at app/code/{VendorName}/{ModuleName}/view/adminhtml/layout/ and add below code:

<?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>
        <referenceContainer name="content">
            <block class="{YourBlockClass}" template="{yourTemplate}" after="comment" name="{nameOnLayout}"/>
        </referenceContainer>
    </body>
</page>

Update

You can achieve this via plugin

Create plugin on class Magento\Sales\Block\Adminhtml\Order\Create\Comment

and add you custom block output with this block

namespace Magento\Sales\Block\Adminhtml\Order\Create;

class CommentPlugin
{
    public function afterToHtml(\Magento\Sales\Block\Adminhtml\Order\Create\Comment $subject, $html)
    {
        $newBlockHtml = $subject->getLayout()->createBlock('Full\Block\Class\Name\Here')->setTemplate({yourTemplate})->toHtml();

    return $html.$newBlockHtml;
    }
}
2
  • It displayed after the order submit button and also select customer section....But i need this ..after order comment text box and before order submit button. – Mahi M Aug 1 '18 at 5:23
  • Thanks, your code worked for me, Can you please also share, how to share this custom value to order attribute? – GunJan Mehta Dec 21 '20 at 14:05
4

Taking ref from @AmitBera

Create etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Block\Adminhtml\Order\Create\Comment">
        <plugin name="after_order_create_comment" type="NameSpace\ModuleName\Plugin\Sales\Block\Adminhtml\Order\Create\CommentPlugin" sortOrder="10"/>
    </type>
</config>

And your NameSpace\ModuleName\Plugin\Sales\Block\Adminhtml\Order\Create\CommentPlugin.php

<?php namespace DigitalCinema\Pos\Plugin\Sales\Block\Adminhtml\Order\Create;

class CommentPlugin
{
    public function afterToHtml(\Magento\Sales\Block\Adminhtml\Order\Create\Comment $subject, $html)
    {
        $newBlockHtml = $subject->getLayout()->createBlock('\Magento\Framework\View\Element\Template')->setTemplate('NameSpace_ModuleName::path/to/template.phtml')->toHtml();

        return $html.$newBlockHtml;
    }
}

You should change \Magento\Framework\View\Element\Template to your own block class.

Can confirm, it is working in my M2 instance.

2
  • Working on magento 2.3.3 , thanks my friend! – wightman Feb 17 '20 at 8:18
  • @AmitBera is there a way to add block in store switcher page in admin order create and save that data? I am not able to access customer Id there. – Pallavi Sinha Jan 12 at 11:39
3

I took a different approach to this which is slightly more complicated but allows more flexibility around where the block is added.

Rather than explicitly modify the comment block I wrapped my plugin around the parent Magento\Sales\Block\Adminhtml\Order\Create\Data blocks getChildHtml method. Within the plugin I then check the name of the block being rendered and inject my new block where appropriate. E.g

etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Block\Adminhtml\Order\Create\Data">
        <plugin name="after_order_create_data" type="Namespace\Module\Plugin\Sales\Block\Adminhtml\Order\Create\DataPlugin" sortOrder="10"/>
    </type>
</config>

view/adminhtml/layout/sales_order_create_index.xml

<?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>
        <referenceBlock name="data">
            <block class="Namespace\Module\Block\Adminhtml\Order\Create\Form\CustomBlock" template="Namespace_Module::order/create/form/newsection.phtml" name="new_section" />
        </referenceBlock>
    </body>
</page>

Plugin/Sales/Block/Adminhtml/Order/Create/DataPlugin.php

<?php
namespace Namespace\Module\Plugin\Sales\Block\Adminhtml\Order\Create;

class DataPlugin
{

    public function aroundGetChildHtml(\Magento\Sales\Block\Adminhtml\Order\Create\Data $subject, callable $proceed, $id, $useCache = true)
    {
        $html = $proceed($id, $useCache);

        if ($id == 'form_account') { // <- different sections can be targeted by changing block name
            $block = $subject->getChildHtml('new_section');
            $html = $html . $block;
        }

        return $html;
    }
}

view/adminhtml/templates/order/create/form/newsection.phtml

<!-- we are injecting into an existing section so close it first and then open new one. This may need to be adjusted depending on what block the new block is injected in -->
</section> 

<section id="order-customblock" class="admin__page-section order-customblock">

   <!-- put new block content here -->
1
  • Thanks, your code worked for me, Can you please also share, how to share this custom value to order attribute? – GunJan Mehta Dec 21 '20 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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