0

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?

enter image description here

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>

2 Answers 2

1

LET’S FOLLOW STEP BY STEP INSTRUCTIONS TO CREATE CUSTOMER ATTRIBUTE IN MAGENTO 2: Step 1: Create a setup file InstallData.php Firstly, we will create InstallData.php file.

It will be located at app/code/Magedelight/HelloWorld/Setup/InstallData.php

<?php
namespace Magedelight\HelloWorld\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
       private $eavSetupFactory;
       public function __construct(EavSetupFactory $eavSetupFactory)
       {
              $this->eavSetupFactory = $eavSetupFactory;
       }
}

Step 2: Define the install() Method Now, we will define the install() method and create eav setup model using the below-mentioned code:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
       {
              $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
       }

Next, we will use eavSetup object to add the attribute:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'sample_attribute',
[
'type' => 'varchar',
'label' => 'Sample Attribute',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
);
}

Step 3: Create Custom Attribute In the end, we would be required to set the forms in which the attributes will be used. We need to define the eavConfig object that will allow us to call the attribute back and set the data for it.

<?php
namespace Magedelight\HelloWorld\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;
class InstallData implements InstallDataInterface
{
       private $eavSetupFactory;
       public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
       {
              $this->eavSetupFactory = $eavSetupFactory;
              $this->eavConfig       = $eavConfig;
       }
       public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
       {
              $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
              $eavSetup->addAttribute(
                     \Magento\Customer\Model\Customer::ENTITY,
                     'sample_attribute',
                     [
                            'type'         => 'varchar',
                            'label'        => 'Sample Attribute',
                            'input'        => 'text',
                            'required'     => false,
                            'visible'      => true,
                            'user_defined' => true,
                            'position'     => 999,
                            'system'       => 0,
                     ]
              );
              $sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');

              // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
              $sampleAttribute->setData(
                     'used_in_forms',
                     ['adminhtml_customer']

              );
              $sampleAttribute->save();
       }
}

Now, run the command line to install the module:

php magento setup:upgrade and php bin/magento setup:static-content:deploy

There you go! You’ll have your sample attribute ready.

1
  • 1
    See my updated question, also I have created attributes from the admin panel manually want to so in ordered items table only.
    – ishaq
    Nov 16, 2021 at 13:28
0

Create sales_order_view.xml inside Vendor/Module/view/adminhtml/layout/sales_order_view.xml

Add below code inside sales_order_view.xml

<?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">
    <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="custom_column" xsi:type="string" translate="true">Custom Column</item>
                <!--here inside item name add your column name-->
            </argument>
        </arguments>
    </referenceBlock>
    <referenceBlock name="default_order_items_renderer">
        <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="custom_column" xsi:type="string" translate="true">col-custom</item> 
                <!--here inside item name add your column name-->
            </argument>
        </arguments>
    </referenceBlock>
</page>

You need to assign column name in both the reference blocks

Change custom_column with your column name.

2
  • See my updated question, I have removed the content of sales_order_view.xml and put your code but nothing showing.
    – ishaq
    Nov 16, 2021 at 13:27
  • Can you please share the code of your sales_order_view.xml ? and also check the path of it
    – Arun Kumar
    Nov 18, 2021 at 5:41

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.