Add custom columns in sales order table in Magento 2

Add custom columns in sales order table in Magento 2

Magento 2 is a configuration based ecommerce CMS. Magento 2 allow high level of customization in each area involved in application. This tutorial will explain how to Add custom columns in sales order table in Magento 2.

In your module create folder with name Setup like Vendor\Module\Setup

Create InstallSchema.php file in Setup folder like Vendor\Module\Setup\InstallSchema.php and paste the following code.

[php]</pre>
<?php
namespace Vendor\Module\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class InstallSchema implements InstallSchemaInterface
{

public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{

$connection = $setup->getConnection();

$columns = [
‘column_1’ =>[
‘type’ => Table::TYPE_SMALLINT,
‘length’ => 6,
‘nullable’ => true,
‘comment’ => ‘Column 1’,
],

‘column_2’ =>[
‘type’ => Table::TYPE_TEXT,
‘length’ => 255,
‘nullable’ => true,
‘comment’ => ‘Column 2’,
],

‘column_3’ =>[
‘type’ => Table::TYPE_TEXT,
‘length’ => 255,
‘nullable’ => true,
‘comment’ => ‘Column 3’,
]
];

foreach ($columns as $name => $definition) {
$connection->addColumn($setup->getTable(‘sales_order’), $name, $definition);
}

}
}
<pre>[/php]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.