0

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?

4

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.

  • he, this will add the column, can we hide/disable if $item->getExpectedDeliveryTime() is empty or null – jafar pinjar May 28 '19 at 12:36
1

I tried and searched for your solution and found below links.

https://stackoverflow.com/questions/3958965/adding-new-column-in-sales-order-view-items-in-magento-admin.

Hope it helps.

=====================Edit my answer =====================. I consider you have created expected_delivery_time column in "sales_order_item" table and saved value in this field when order place.

To display a value of this field makes below changes.

Create "sales_order_view.xml" file in [company/[module]/view/adminhtml/layout. and add below codes.

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  <referenceBlock name="order_items">
    <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="exp_delivery_time" 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="exp_delivery_time" xsi:type="string" translate="false">new-col-exp_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"/>
</referenceBlock>
</page>.

Let me know if it works.

  • thanks for giving this link, but my question still stands – Gujarat Santana Oct 10 '17 at 6:20
  • Please check my editable answer – Amitkumar solanki Oct 10 '17 at 7:46
  • nope this doesn't show my expected_delivery_time only the title column. but thanks upvoted – Gujarat Santana Oct 10 '17 at 8:21
  • Thanks, Gujarat, I will look into this shortly and get back to you. – Amitkumar solanki Oct 10 '17 at 8:58
1

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

  • How to make as first column in table – Yasar Arafath Dec 20 '18 at 5:59
  • this will add the column, how can we hide/disable the coulmn based on certain condition? – jafar pinjar May 28 '19 at 12:29
  • that is, $block->getItem()->getData('custom_item_column'); is empty, then i need to hide column – jafar pinjar May 28 '19 at 12:35
0

Assuming you have a structure of module. In your sales_order_view.xml extended, put:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="order_tab_info">
            <block class="Magento\Sales\Block\Adminhtml\Order\View\Items" name="order_items" template="Magento_Sales::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="custom_item_column" xsi:type="string" translate="true">Custom Item Column</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>
                    </argument>
                </arguments>
                <block class="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer" as="default" name="default_order_items_renderer" template="Magento_Sales::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="custom_item_column" xsi:type="string" translate="true">col-custom_item_column</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>
                        </argument>
                    </arguments>
                </block>
                <block class="Magento\Sales\Block\Adminhtml\Items\Column\Qty" name="column_qty" template="Magento_Sales::items/column/qty.phtml" group="column"/>
                <block class="Magento\Sales\Block\Adminhtml\Items\Column\Name" name="column_name" template="Magento_Sales::items/column/name.phtml" group="column"/>
                <block class="Magento\Framework\View\Element\Text\ListText" name="order_item_extra_info"/>

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

Now, you can order or remove a column.

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.