How to add a custom attribute column with its data to the Items Ordered table at the order view page.
I have followed this link and but it is not working for Magento 2.4.2 How to show product custom attribute in items ordered section in magento2.2.0?
Here is the code of each file.
Vendor/Orderitem/view/adminhtml/layout/sales_order_view.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<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="default_order_items_renderer">
<action method="setTemplate">
<argument name="template" translate="true" xsi:type="string">Vendor_Orderitem::order/view/items/renderer/default.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
Vendor/Orderitem/view/adminhtml/templates/order/view/items/renderer/default.phtml
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?php /** @var \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer $block */ ?>
<?php $_item = $block->getItem() ?>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('\Magento\Catalog\Model\ProductFactory')->create()->load($_item->getProductId());
?>
<?php $block->setPriceDataObject($_item) ?>
<tr>
<?php $i = 0;
$columns = $block->getColumns();
$lastItemNumber = count($columns) ?>
<?php foreach ($columns as $columnName => $columnClass):?>
<?php $i++; ?>
<td class="<?= /* @noEscape */ $columnClass ?><?= /* @noEscape */ ($i === $lastItemNumber ? ' last' : '') ?>"><?= /* @escapeNotVerified */ $block->getColumnHtml($_item, $columnName) ?>
<?php if($columnClass == "col-product"){ ?>
<span><?php echo __("Sourced From: ") ?></span>
<?php echo $product->getData('sourced_from');; ?>
<?php } ?>
</td>
<?php endforeach; ?>
</tr>