How to Create Custom Order Attribute in Magento 2 and Show it in Admin Grid

Do you want to add a custom order attribute to the Magento 2 admin grid? Read this tutorial to learn the step-by-step method to create custom order attribute in Magento 2 and show it in the admin grid. Instead, you can also checkout our Magento 2 Order Attributes extension.

Businesses use order information to manage & fulfill online orders efficiently. Order details such as shipping address and payment status make it easy to process online orders. The default Magento 2 displays such essential order information in the Sales > Orders backend grid, making it easy for the merchants to manage & process the orders. Furthermore, the Magento order status & states make the process easier.

Creating a custom order attribute in Magento 2 and showing it in the admin grid can make order processing more efficient as you scale your business. Also You add category attribute to custom attribute group in Magento 2 will help you integrating a third-party API and using a category attribute! You can follow the steps in this blog post to create a custom attribute in Magento 2 & show it in the admin grid.

Method to Create Custom Order Attribute in Magento 2 and Show it in Admin Grid

In order to create a custom order attribute in Magento 2, we will create a custom module and a UpgradeData file and save the custom attribute using an observer. Lastly, we’ll update the sales_order_grid.xml file to show the custom attribute in Magento 2 order grid.

Let’s see how to do it using an example. Create the following files in Magento 2:

  • Vendor_Module/etc/module.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="vendor_module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>
  • Vendor_Module/Setup/UpgradeData.php
<?php
namespace Vendor\Module\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Sales\Setup\SalesSetupFactory;


class UpgradeData implements UpgradeDataInterface
{
    private $salesSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Sales\Setup\SalesSetupFactory $salesSetupFactory
     */
    public function __construct(SalesSetupFactory $salesSetupFactory)
    {
        $this->salesSetupFactory = $salesSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        if (version_compare($context->getVersion(), "1.0.1", "<")) {
            $salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);
            $salesSetup->addAttribute(
                'order',
                'checkmo_cash_info',
                [
                    'type' => 'varchar',
                    'length' => 100,
                    'visible' => false,
                    'required' => false,
                    'grid' => true
                ]
            );
        }
    }
}
  • Vendor_Module/etc/events.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_save_after">
        <observer instance="Vendor\Module\Observer\Sales\OrderSaveAfter" name="ordersmodification_observer_sales_ordersaveafter_sales_order_save_after"/>
    </event>
</config>
  • Vendor_Module/Observer/Sales/OrderSaveAfter.php
<?php

namespace Vendor\Module\Observer\Sales;


class OrderSaveAfter implements \Magento\Framework\Event\ObserverInterface
{
    /**
     * @var \Magento\Catalog\Model\ProductRepository
     */
    protected $productRepository;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Catalog\Model\ProductRepository $productRepository
    ) {
        $this->logger = $logger;
        $this->productRepository = $productRepository;
    }

    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
    
         $order= $observer->getData('order');
     
          if(isset($postData['payment']['ckm_cash_number'])){
            $order->setCheckmoCashInfo('y');
        }
        $order->save();
    }
}
  • Vendor_Module/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!--Sync the sales_order table and sales_order_grid-->
    <virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="checkmo_cash_info" xsi:type="string">sales_order.checkmo_cash_info</item>
            </argument>
        </arguments>
    </virtualType>
</config>
  • Vendor_Module/view/adminhtml/ui_component/sales_order_columns.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <column name="checkmo_cash_info">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
                    <item name="label" xsi:type="string" translate="true">checkmo Cash Information</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Yeahhh…!

The custom order attribute will be created and displayed in the admin order grid in Magento 2. You can also learn Magento 2 create order in admin Panel to assist your customer in placing order when they require.

You can use the above code to create a custom order attribute in Magento 2 and show it in the admin grid as per your requirement & put your own logic. Try the solution.

Don’t you think this solution deserves a five-star rating?

Help us spread the Magento knowledge. Share this blog post on social media & online forums.

Thanks for reading…!

Table of Contents