Magento – Magento2 How to custom column in sales order item renderer in admin order view

magento2renderersales

I need to add custom column in admin sales_order_view for each item listing with the help of renderer.
For example, there are some predefined columns like name, status, price, qty, discount etc are in order item listing. I need to order custom column in that listing.

Best Answer

Assuming you have created 'custom_item_column' in sales_order_item table you can add a custom column to Items Grid in sales order view by following:

1.Create sales_order_view.xml file in app/code/[Vendor]/[Modulename]/view/adminhtml/layout folder and add the below code.

<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">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="custom_item_column" xsi:type="string" translate="true">Custom Item Column</item>
            </argument>
        </arguments>
        <referenceBlock name="default_order_items_renderer">
            <arguments>
                <argument name="columns" xsi:type="array">
                    <item name="custom_item_column" xsi:type="string" translate="true">col-custom_item_column</item>
                </argument>
            </arguments>
        </referenceBlock>

        <block class="Magento\Sales\Block\Adminhtml\Items\Column\DefaultColumn"
               name="column_custom_item_column"
               template="Namespace_Modulename::custom_item_column.phtml" group="column" />
    </referenceBlock>
</body>

2.Create "custom_item_column.phtml" file in [Namespace]/[Modulename]/view/adminhtml/templates folder and add the below code.

<span><?= /* @escapeNotVerified */ __($block->getItem()->getData('custom_item_column')) ?></span>

source : https://github.com/magento/magento2/pull/11076