Magento 2 add column in quote table and order table
This tutorial is about Magento 2 add column in quote table and order table .i.e how to get order and quote column value in Magento 2. And in this tutorial, I will try to explain it briefly and in a simple way.
If you have created a custom column for orders. I found a question and I would use it as a reference here and will share the piece of code as well.
namespace QaisarSatti\HelloWorld\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$quote = 'quote';
$orderTable = 'sales_order';
$setup->getConnection()
->addColumn(
$setup->getTable($quote),
'mediabasebestellnummer',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'comment' =>'MediabaseNummer'
]
);
//Order table
$setup->getConnection()
->addColumn(
$setup->getTable($orderTable),
'mediabasebestellnummer',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'comment' =>'MediabaseNummer'
]
);
$setup->endSetup();
}
}
And now you want to get the custom attribute when you have an order. how to do it?. Let us have a look at the ways to achieve it. If you are using the following method
It may not work properly. There are a couple of ways to do it. We will have a brief look at this tutorial.
First of all, check if the column is created in the sales_order table
Now we can get the values by
Or
For adding the data
OR
Or
I have tried to explain it in a simple way as there are many ways to perform this task.
Furthermore, make sure you have file fieldset.xml in your module to convert data from quote to order.
Following code will do the job for us.
xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
<scope id="global">
<fieldset id="sales_convert_quote">
<field name="mediabasebestellnummer">
<aspect name="to_order" />
</field>
</fieldset>
</scope>
</config>
Also, it is used when you have column in both quote tableand order table.
That’s it from this tutorial. I hope it serves the purpose. Since these are learning tutorials, please feel free to drop any suggestions or queries in the comments section. That will definitely be highly appreciated.