Magento 2 – Show Additional Column from Sales Order Item in Order View

magento-2.1sales-order

I'm trying to show additional column in the order view -> information page this is the url page : rootDomain/admin_an2/sales/order/view/order_id/40/key.

enter image description here

From what I understand this data is coming from the sales_order_item table, now I have added additional column to that table called expected_delivery_time But I don't know how can show it there?

I found out that this UI is created from here : `/rootProject/vendor/magento/module-sales/view/adminhtml/templates/order/view/items.phtml

<?php
/**
 * @var \Magento\Sales\Block\Adminhtml\Order\View\Items $block
 */
$_order = $block->getOrder() ?>
<div class="admin__table-wrapper">
    <table class="data-table admin__table-primary edit-order-table">
        <thead>
            <tr class="headings">
                <?php $i = 0;
                $columns = $block->getColumns();
                $lastItemNumber = count($columns) ?>
                <?php foreach ($columns as $columnName => $columnTitle):?>
                    <?php $i++; ?>
                    <th class="col-<?php /* @noEscape */ echo $columnName ?><?php /* @noEscape */ echo ($i === $lastItemNumber ? ' last' : '')?>"><span><?php /* @noEscape */ echo $columnTitle ?></span></th>
                <?php endforeach; ?>
            </tr>
        </thead>
        <?php $_items = $block->getItemsCollection();?>
        <?php $i = 0; foreach ($_items as $_item):?>
            <?php if ($_item->getParentItem()) {
                continue;
            } else {
                $i++;
            }?>
            <tbody class="<?php /* @noEscape */ echo $i%2 ? 'even' : 'odd' ?>">
                <?php echo $block->getItemHtml($_item) ?>
                <?php echo $block->getItemExtraInfoHtml($_item) ?>
            </tbody>
        <?php endforeach; ?>
    </table>
</div>

I tried to find a block Items class.
I get the getColumn() :

/**
 * @return array
 */
public function getColumns()
{
    $columns = array_key_exists('columns', $this->_data) ? $this->_data['columns'] : [];
    return $columns;
}

How to add my column to the $this->_data['columns'] ? getting stuck here.
any idea?

Best Answer

After a several hours finally I have found the solution. Here is the step :

  • create layout view/adminhtml/layout/sales_order_view.xml like this :

        <referenceContainer name="left">
    <block class="Magento\Sales\Block\Adminhtml\Order\View\Tabs" name="sales_order_tabs">
        <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Info" name="order_tab_info" template="order/view/tab/info.phtml">
            <block class="Magento\Sales\Block\Adminhtml\Order\View\Messages" name="order_messages"/>
            <block class="Magento\Sales\Block\Adminhtml\Order\View\Info" name="order_info" template="order/view/info.phtml"/>
            <block class="Magento\Sales\Block\Adminhtml\Order\View\Items" name="order_items" template="order/view/items.phtml">
                <arguments>
                    <argument name="columns" xsi:type="array">
                        <item name="product" xsi:type="string" translate="true">Product</item>
                        <item name="status" xsi:type="string" translate="true">Item Status</item>
                        <item name="price-original" xsi:type="string" translate="true">Original Price</item>
                        <item name="price" xsi:type="string" translate="true">Price</item>
                        <item name="ordered-qty" xsi:type="string" translate="true">Qty</item>
                        <item name="subtotal" xsi:type="string" translate="true">Subtotal</item>
                        <item name="tax-amount" xsi:type="string" translate="true">Tax Amount</item>
                        <item name="tax-percent" xsi:type="string" translate="true">Tax Percent</item>
                        <item name="discont" xsi:type="string" translate="true">Discount Amount</item>
                        <item name="total" xsi:type="string" translate="true">Row Total</item>
                        <item name="expected-delivery-time-column" xsi:type="string" translate="true">Expected Delivery Time</item>
                    </argument>
                </arguments>
                <block class="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer" as="default" template="order/view/items/renderer/default.phtml">
                    <arguments>
                        <argument name="columns" xsi:type="array">
                            <item name="product" xsi:type="string" translate="false">col-product</item>
                            <item name="status" xsi:type="string" translate="false">col-status</item>
                            <item name="price-original" xsi:type="string" translate="false">col-price-original</item>
                            <item name="price" xsi:type="string" translate="false">col-price</item>
                            <item name="qty" xsi:type="string" translate="false">col-ordered-qty</item>
                            <item name="subtotal" xsi:type="string" translate="false">col-subtotal</item>
                            <item name="tax-amount" xsi:type="string" translate="false">col-tax-amount</item>
                            <item name="tax-percent" xsi:type="string" translate="false">col-tax-percent</item>
                            <item name="discont" xsi:type="string" translate="false">col-discont</item>
                            <item name="total" xsi:type="string" translate="false">col-total</item>
                            <item name="expected-delivery-time" xsi:type="string" translate="true">col-expected-delivery-time</item>
                        </argument>
                    </arguments>
                </block>
                <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"/>
            </block>
    
            <container name="payment_additional_info" htmlTag="div" htmlClass="order-payment-additional" />
            <block class="Magento\Sales\Block\Adminhtml\Order\Payment" name="order_payment"/>
            <block class="Magento\Sales\Block\Adminhtml\Order\View\History" name="order_history" template="order/view/history.phtml"/>
            <block class="Magento\Backend\Block\Template" name="gift_options" template="Magento_Sales::order/giftoptions.phtml">
                <block class="Magento\Sales\Block\Adminhtml\Order\View\Giftmessage" name="order_giftmessage" template="order/view/giftmessage.phtml"/>
            </block>
            <block class="Magento\Sales\Block\Adminhtml\Order\Totals" name="order_totals" template="order/totals.phtml">
                <block class="Magento\Sales\Block\Adminhtml\Order\Totals\Tax" name="tax" template="order/totals/tax.phtml"/>
            </block>
        </block>
        <action method="addTab">
            <argument name="name" xsi:type="string">order_info</argument>
            <argument name="block" xsi:type="string">order_tab_info</argument>
        </action>
        <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Invoices" name="sales_order_invoice.grid.container"/>
        <action method="addTab">
            <argument name="name" xsi:type="string">order_invoices</argument>
            <argument name="block" xsi:type="string">sales_order_invoice.grid.container</argument>
        </action>
        <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Creditmemos" name="sales_order_creditmemo.grid.container"/>
        <action method="addTab">
            <argument name="name" xsi:type="string">order_creditmemos</argument>
            <argument name="block" xsi:type="string">sales_order_creditmemo.grid.container</argument>
        </action>
        <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Shipments" name="sales_order_shipment.grid.container"/>
        <action method="addTab">
            <argument name="name" xsi:type="string">order_shipments</argument>
            <argument name="block" xsi:type="string">sales_order_shipment.grid.container</argument>
        </action>
        <action method="addTab">
            <argument name="name" xsi:type="string">order_history</argument>
            <argument name="block" xsi:type="string">Magento\Sales\Block\Adminhtml\Order\View\Tab\History</argument>
        </action>
        <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Transactions" name="sales_transactions.grid.container"/>
        <action method="addTab">
            <argument name="name" xsi:type="string">order_transactions</argument>
            <argument name="block" xsi:type="string">sales_transactions.grid.container</argument>
        </action>
    </block>
    

  • create plugin to override the method for showing the expected delivery date :

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

class DefaultRendererPlugin
{
    public function aroundGetColumnHtml(DefaultRenderer $defaultRenderer, \Closure $proceed,\Magento\Framework\DataObject $item, $column, $field=null)
    {
        if ($column == 'expected-delivery-time'){
            $html = $item->getExpectedDeliveryTime();
            $result = $html;
        }else{
            if ($field){
                $result = $proceed($item,$column,$field);
            }else{
                $result = $proceed($item,$column);

            }
        }

        return $result;
    }
}
  • etc config :

    <type name="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer">
        <plugin name="fabelio-order-view-information" type="Fabelio\Sales\Plugin\DefaultRendererPlugin" sortOrder="1" />
    </type>
    

that's it.

Related Topic