:::: MENU ::::
Posts tagged with: get product attribute

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.

<?php

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

$custom = $order->getCustomAttribute('mediabasebestellnummer');

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

$order->getMediabasebestellnummer();

Or

$order->getData('mediabasebestellnummer');

For adding the data

$order->setMediabasebestellnummer('test value')->save();

OR

$order->setData('mediabasebestellnummer','test value')->save();

Or

$data = array('mediabasebestellnummer'=>'test');
$order->setData($data)->save();

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.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.

Author: Qaisar Satti
Category: Magento 2
Last Modified: February 4, 2019