2

How to add an extra column in Item ordered tab in Magento 2?

PFA

enter image description here

Any ideas?

1 Answer 1

4

If you want to add a new column at then you have to work on below files:

sales_order_view.xml

Create sales_order_view.xml at your module app/code/{Vendor}/{ModuleName}/view/adminhtml/layout

and add a new column using below code

<?xml version="1.0" encoding="UTF-8"?>
<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="customcol" xsi:type="string" translate="true">Custom column</item>
                </argument>
            </arguments>  
            <block class="{Vendor}\{ModuleName}\Block\Adminhtm\Items\Column\Custom" name="column_customcol" 
                   template="{Vendor}_{ModuleName}::items/column/custom.phtml" group="column"/>         
        </referenceBlock>
        <referenceBlock name="default_order_items_renderer">
            <arguments>
                <argument name="columns" xsi:type="array">
                    <item name="customcol" xsi:type="string" translate="false">col-customcol</item>
                </argument>  
            </arguments>             
        </referenceBlock>
    </body>
</page>

Here customcol is a unique identifier for the new column.

BLock class Column.php for showing content.Location app/code/{Vendor}/{ModuleName}/Block/Adminhtml/Items/Column/Custom.php

and Code:

<?php
namespace {Vendor}\{ModuleName}\Block\Adminhtml\Items\Column;


class Custom  extends \Magento\Sales\Block\Adminhtml\Items\Column\DefaultColumn
{

}

Phtml custom.phtml file for this block app/code/{Vendor}/Magento/view/adminhtml/templates/items/column/custom.phtml

and Code is:

<?php
// phpcs:disable Magento2.Templates.ThisInTemplate
?>
<?php
/* @var $block \Magento\Sales\Block\Adminhtml\Items\Column\Name */
?>
<?php if ($_item = $block->getItem()) : ?>
    <div id="order_item_<?= (int) $_item->getId() ?>_title"
         class="product-title">
        <?= $block->escapeHtml($_item->getName()) ?>
    </div>
<?php endif; ?>

Note

Replace {Vendor} with Your module vendor name and {ModuleName} replace Module Name.

4
  • thank you sir. it's working now
    – Devidas
    Aug 21, 2019 at 12:32
  • @Devidas please mark the answer as solved if the answer solves your question. Aug 21, 2019 at 20:16
  • @RendyEkoPrastiyo, thank but you cannot ask the user accept this his/her answer
    – Amit Bera
    Aug 22, 2019 at 5:35
  • @AmitBera I have tried this solution for 2.3.5 version and it is not working anymore. Can u please guide here. I have defined Block Custom in magentoroot/app/code/MyModule/Customadmin/etc/adminhtml/di.xml. But still it not working .
    – Rahul
    Nov 27, 2020 at 13:10

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.