{"id":2712,"date":"2024-12-31T20:26:01","date_gmt":"2024-12-31T20:26:01","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/how-to-create-custom-order-attribute-in-magento-2-and-show-it-in-admin-grid\/"},"modified":"2025-04-09T17:42:11","modified_gmt":"2025-04-09T12:12:11","slug":"create-custom-order-attribute-in-magento-2-and-show-it-in-admin-grid","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-custom-order-attribute-in-magento-2-and-show-it-in-admin-grid\/","title":{"rendered":"How to Create Custom Order Attribute in Magento 2 and Show it in Admin Grid"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Do you want to add a custom order attribute to the\u00a0<a href=\"https:\/\/developer.adobe.com\/commerce\/docs\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 admin grid<\/a>? Read this tutorial to learn the step-by-step method to\u00a0create custom order attribute in Magento 2 and show it in the admin grid.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Businesses use order information to manage &amp; 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&nbsp;<strong>Sales &gt; Orders<\/strong>&nbsp;backend grid, making it easy for the merchants to manage &amp; process the orders. Furthermore, the&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-magento-2-order-status-order-state\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento order status &amp; states<\/a>&nbsp;make the process easier.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/add-category-attribute-to-custom-attribute-group-in-magento-2\/\">add category attribute to custom attribute group in Magento 2<\/a>&nbsp;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 &amp; show it in the admin grid.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Create Custom Order Attribute in Magento 2 and Show it in Admin Grid<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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\u2019ll update the sales_order_grid.xml file to show the custom attribute in Magento 2 order grid.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s see how to do it using an example. Create the following files in Magento 2:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vendor_Module\/etc\/module.xml<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" ?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"vendor_module\" setup_version=\"1.0.0\">\n        &lt;sequence>\n            &lt;module name=\"Magento_Sales\"\/>\n        &lt;\/sequence>\n    &lt;\/module>\n&lt;\/config><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vendor_Module\/Setup\/UpgradeData.php<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Module\\Setup;\n\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nuse Magento\\Framework\\Setup\\UpgradeDataInterface;\nuse Magento\\Sales\\Setup\\SalesSetupFactory;\n\n\nclass UpgradeData implements UpgradeDataInterface\n{\n    private $salesSetupFactory;\n\n    \/**\n     * Constructor\n     *\n     * @param \\Magento\\Sales\\Setup\\SalesSetupFactory $salesSetupFactory\n     *\/\n    public function __construct(SalesSetupFactory $salesSetupFactory)\n    {\n        $this->salesSetupFactory = $salesSetupFactory;\n    }\n\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function upgrade(\n        ModuleDataSetupInterface $setup,\n        ModuleContextInterface $context\n    ) {\n        if (version_compare($context->getVersion(), \"1.0.1\", \"&lt;\")) {\n            $salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);\n            $salesSetup->addAttribute(\n                'order',\n                'checkmo_cash_info',\n                [\n                    'type' => 'varchar',\n                    'length' => 100,\n                    'visible' => false,\n                    'required' => false,\n                    'grid' => true\n                ]\n            );\n        }\n    }\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vendor_Module\/etc\/events.xml<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" ?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\">\n    &lt;event name=\"sales_order_save_after\">\n        &lt;observer instance=\"Vendor\\Module\\Observer\\Sales\\OrderSaveAfter\" name=\"ordersmodification_observer_sales_ordersaveafter_sales_order_save_after\"\/>\n    &lt;\/event>\n&lt;\/config><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vendor_Module\/Observer\/Sales\/OrderSaveAfter.php<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Observer\\Sales;\n\n\nclass OrderSaveAfter implements \\Magento\\Framework\\Event\\ObserverInterface\n{\n    \/**\n     * @var \\Magento\\Catalog\\Model\\ProductRepository\n     *\/\n    protected $productRepository;\n\n    public function __construct(\n        \\Psr\\Log\\LoggerInterface $logger,\n        \\Magento\\Catalog\\Model\\ProductRepository $productRepository\n    ) {\n        $this->logger = $logger;\n        $this->productRepository = $productRepository;\n    }\n\n    \/**\n     * Execute observer\n     *\n     * @param \\Magento\\Framework\\Event\\Observer $observer\n     * @return void\n     * @throws \\Magento\\Framework\\Exception\\NoSuchEntityException\n     *\/\n    public function execute(\n        \\Magento\\Framework\\Event\\Observer $observer\n    ) {\n    \n         $order= $observer->getData('order');\n     \n          if(isset($postData['payment']['ckm_cash_number'])){\n            $order->setCheckmoCashInfo('y');\n        }\n        $order->save();\n    }\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vendor_Module\/etc\/di.xml<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;!--Sync the sales_order table and sales_order_grid-->\n    &lt;virtualType name=\"Magento\\Sales\\Model\\ResourceModel\\Order\\Grid\" type=\"Magento\\Sales\\Model\\ResourceModel\\Grid\">\n        &lt;arguments>\n            &lt;argument name=\"columns\" xsi:type=\"array\">\n                &lt;item name=\"checkmo_cash_info\" xsi:type=\"string\">sales_order.checkmo_cash_info&lt;\/item>\n            &lt;\/argument>\n        &lt;\/arguments>\n    &lt;\/virtualType>\n&lt;\/config><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vendor_Module\/view\/adminhtml\/ui_component\/sales_order_columns.xml<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;listing xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Ui:etc\/ui_configuration.xsd\">\n    &lt;columns name=\"sales_order_columns\">\n        &lt;column name=\"checkmo_cash_info\">\n            &lt;argument name=\"data\" xsi:type=\"array\">\n                &lt;item name=\"config\" xsi:type=\"array\">\n                    &lt;item name=\"filter\" xsi:type=\"string\">textRange&lt;\/item>\n                    &lt;item name=\"bodyTmpl\" xsi:type=\"string\">ui\/grid\/cells\/html&lt;\/item>\n                    &lt;item name=\"label\" xsi:type=\"string\" translate=\"true\">checkmo Cash Information&lt;\/item>\n                &lt;\/item>\n            &lt;\/argument>\n        &lt;\/column>\n    &lt;\/columns>\n&lt;\/listing><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Yeahhh\u2026!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The custom order attribute will be created and displayed in the admin order grid in Magento 2. You can also learn&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-order-in-magento-2-admin-panel\/\">Magento 2 create order<\/a>&nbsp;in admin Panel to assist your customer in placing order when they require.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 &amp; put your own logic. Try the solution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Don\u2019t you think this solution deserves a five-star rating?  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">  Help us spread the Magento knowledge. Share this blog post on social media &amp; online forums.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading\u2026!  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you want to add a custom order attribute to the\u00a0Magento 2 admin grid? Read this tutorial to learn the step-by-step method to\u00a0create custom order&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2712","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2712"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2712\/revisions"}],"predecessor-version":[{"id":11738,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2712\/revisions\/11738"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}