Magento 2 – add sales_order column
In this tutorial I show you how to add sales_order column with a custom attribute. Sometimes it is necessary to save additional information for your order. A good and simple place for such data is you sales_order table. I show you how and what you need to know about that.
Magento 2 – add sales_order column
Magento 2 architecture is perfect if you want to add additional functionality with modules. In this small tutorial I show you how to add a new column in you sales_order table. With this, you can set and get this attribute from you order object everywhere you need it (which would be your new module). You need to set up a base module for this and add a InstallSchema file for database manipulation.
addColumn
Lets create a new module and add a Module/Setup/InstallSchema.php file to it. This file is executed the first time you run
1 | php bin/magento setup:upgrade |
from you command line, with that module enabled. The following code adds a new column to sales_order with title my_new_column:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | namespace MyCompany\MyModule\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\DB\Ddl\Table; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $connection = $installer->getConnection(); if ($connection->tableColumnExists('sales_order', 'my_new_column') === false) { $connection ->addColumn( $setup->getTable('sales_order'), 'my_new_column', [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'length' => 0, 'comment' => 'My New Order Column' ] ); } $installer->endSetup(); } } |
This code is quite basic, you may see it in other modules. First I check if that column already exists (you can use tableColumnExists() method for this). This is not necessary, because this script is only executed once, if you install this module. For development reasons, it is good to make such if statements. With it, you can rerun this script as often as you like (remove module line in setup_module table before!).
You just need to call addColumn() from your connection object. You need to set table, column name and arguments to get a new column. You can add a bunch of optional arguments if you like:
- type
is the only needed one. You have to set type of column. Could be a datatype like TYPE_INTEGER or else or a TYPE_TEXT field. - length
is optional for the most types. If your type is TYPE_TEXT, you get a text field if you set length to 0, or a varchar for a number > 0. - comment
another needed argument to set a column name.
For simple custom columns you won’t need more to know. Basically you can adjust all possible SQL column options with this arguments like primary keys, default values, nullables, and a lot more.
Use custom column
You can now you your defined sales_order attribute like you do with all other columns for example:
1 2 3 | $order->setMyNewColumn("Test"); $order->save(); echo $order->getData('my_new_column') |
Conclusion
It is quite easy to add sales_order column attributes to Magento 2. You just need to add a small script to your InstallSchema file.