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.
1 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> - Thanks @Aziz for the answer. You saved my day.Parth Thakkar– Parth Thakkar2019-11-19 14:33:18 +00:00Commented Nov 19, 2019 at 14:33
- @aziz i tried this way but it is not displyaingSabareesh– Sabareesh2020-04-23 07:55:42 +00:00Commented Apr 23, 2020 at 7:55
- @Sabareesh This works fine for me, please check the steps again. Maybe you are missing somethingAziz– Aziz2020-04-26 05:23:19 +00:00Commented Apr 26, 2020 at 5:23
- @Aziz I tried this on Magento 2.3.4 as well and it is not rendering the column data. I have followed your example exactly. The template for column_custom_item_column block is never rendered.DaChronDon– DaChronDon2020-05-08 20:55:38 +00:00Commented May 8, 2020 at 20:55
- Can I add it after the original price ?Bhavesh Prajapati– Bhavesh Prajapati2021-01-06 07:13:53 +00:00Commented Jan 6, 2021 at 7:13