Magento 2 How to create new order attribute programaticallyMagento 2 new order attributeMagento 2 saving custom attribute to sales_order_itemsAdd Custom Attribute To OrderMagento 2 - Delivery Note on Cart PageMagento 2: Plugin to add data to quote_address table when Place Order clickedWhat new tables need to be created when building module for custom attributes?Add custom attribute to myorder magento2.1Magento: create order programatically: Configurable product issueAdd Customer Group attribute to Sales OrderAdd new Item Order Attribute Magento 2How to call for an custom attribute in magento backend order page?Magento 2 new order attributeMagento 2 Order Attribute CreationsHow we can create an order programmatically using “**paypal_express**” method in Magento 2.1.xHow to add a new attribute in order response json in get order apiHow to create duplicate new quote from old order quote Magento 1.7?Magento 1.9.x: How to change the system so that new orders start from a specific number going up?

Adjective for 'made of pus' or 'corrupted by pus' or something of something of pus

Missing root certificates on Windows Server 2016 (fresh install)

Active wildlife outside the window- Good or Bad for Cat psychology?

Quantum jump/leap, exist or not, and instantaneous or not (for electrons)?

Traversing Eurasia: A Cryptic Journey

How useful would a hydroelectric plant be in the post-apocalypse world?

Can a successful book series let the bad guy win?

Why can't you move another user's directory when you can move their file?

How do I tell the reader that my character is autistic in Fantasy?

Calculus, Water Poured into a Cone: Why is Derivative Non-linear?

Why wasn't ASCII designed with a contiguous alphanumeric character order?

How did they film the Invisible Man being invisible, in 1933?

Can European countries bypass the EU and make their own individual trade deal with the U.S.?

How could an armless race establish civilization?

Are Valenar elves and Aereni elves different races of elves?

Checkmate in 1 on a Tangled Board

Making a wall made from glass bricks

Translation of the Sator Square

Head on collision of two black holes

Reusable spacecraft: why still have fairings detach, instead of open/close?

How can I deal with extreme temperatures in a hotel room?

Is ALTER TABLE ... DROP COLUMN really a metadata only operation?

Two palindromes are not enough

Bin Packing with Relational Penalization



Magento 2 How to create new order attribute programatically


Magento 2 new order attributeMagento 2 saving custom attribute to sales_order_itemsAdd Custom Attribute To OrderMagento 2 - Delivery Note on Cart PageMagento 2: Plugin to add data to quote_address table when Place Order clickedWhat new tables need to be created when building module for custom attributes?Add custom attribute to myorder magento2.1Magento: create order programatically: Configurable product issueAdd Customer Group attribute to Sales OrderAdd new Item Order Attribute Magento 2How to call for an custom attribute in magento backend order page?Magento 2 new order attributeMagento 2 Order Attribute CreationsHow we can create an order programmatically using “**paypal_express**” method in Magento 2.1.xHow to add a new attribute in order response json in get order apiHow to create duplicate new quote from old order quote Magento 1.7?Magento 1.9.x: How to change the system so that new orders start from a specific number going up?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








11















I've been searching the web on how to create an order attribute (if thats whats called), essentially I just want a new database column to appear in the sales_order database, obviously I could create it manually but is there a way I can create it via an upgrade script/programmatically?










share|improve this question




























    11















    I've been searching the web on how to create an order attribute (if thats whats called), essentially I just want a new database column to appear in the sales_order database, obviously I could create it manually but is there a way I can create it via an upgrade script/programmatically?










    share|improve this question
























      11












      11








      11


      6






      I've been searching the web on how to create an order attribute (if thats whats called), essentially I just want a new database column to appear in the sales_order database, obviously I could create it manually but is there a way I can create it via an upgrade script/programmatically?










      share|improve this question














      I've been searching the web on how to create an order attribute (if thats whats called), essentially I just want a new database column to appear in the sales_order database, obviously I could create it manually but is there a way I can create it via an upgrade script/programmatically?







      magento2 sales-order






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 18 '16 at 9:58









      André FerrazAndré Ferraz

      1,8871 gold badge11 silver badges33 bronze badges




      1,8871 gold badge11 silver badges33 bronze badges




















          1 Answer
          1






          active

          oldest

          votes


















          24














          Practically, there are two main ways to add an order attribute (a new column) to order via an upgrade script.



          --Using $setup->getConnection()->addColumn()



          app/code/Vendor/SalesOrder/Setup/UpgradeSchema.php



          <?php

          namespace VendorSalesOrderSetup;

          use MagentoFrameworkSetupUpgradeSchemaInterface;
          use MagentoFrameworkSetupSchemaSetupInterface;
          use MagentoFrameworkSetupModuleContextInterface;

          class UpgradeSchema implements UpgradeSchemaInterface

          /**
          * Upgrades DB schema for a module
          *
          * @param SchemaSetupInterface $setup
          * @param ModuleContextInterface $context
          * @return void
          */
          public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

          $setup->startSetup();

          $quote = 'quote';
          $orderTable = 'sales_order';

          $setup->getConnection()
          ->addColumn(
          $setup->getTable($quote),
          'custom_attribute',
          [
          'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'length' => 255,
          'comment' =>'Custom Attribute'
          ]
          );
          //Order table
          $setup->getConnection()
          ->addColumn(
          $setup->getTable($orderTable),
          'custom_attribute',
          [
          'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'length' => 255,
          'comment' =>'Custom Attribute'
          ]
          );

          $setup->endSetup();




          --Using Quote and Sale Setup Factory



          app/code/Vendor/SalesOrder/Setup/UpgradeData.php



          <?php

          namespace VendorSalesOrderSetup;

          use MagentoFrameworkSetupModuleContextInterface;
          use MagentoFrameworkSetupModuleDataSetupInterface;
          use MagentoFrameworkSetupUpgradeDataInterface;
          use MagentoQuoteSetupQuoteSetupFactory;
          use MagentoSalesSetupSalesSetupFactory;

          class UpgradeData implements UpgradeDataInterface

          /**
          * @var QuoteSetupFactory
          */
          protected $quoteSetupFactory;

          /**
          * @var SalesSetupFactory
          */
          protected $salesSetupFactory;

          /**
          * @param QuoteSetupFactory $quoteSetupFactory
          * @param SalesSetupFactory $salesSetupFactory
          */
          public function __construct(
          QuoteSetupFactory $quoteSetupFactory,
          SalesSetupFactory $salesSetupFactory
          )
          $this->quoteSetupFactory = $quoteSetupFactory;
          $this->salesSetupFactory = $salesSetupFactory;

          /**
          * Upgrades DB for a module
          *
          * @param ModuleDataSetupInterface $setup
          * @param ModuleContextInterface $context
          * @return void
          */
          public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

          /** @var MagentoQuoteSetupQuoteSetup $quoteInstaller */
          $quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);

          /** @var MagentoSalesSetupSalesSetup $salesInstaller */
          $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);

          $setup->startSetup();

          //Add multiple attributes to quote
          $entityAttributesCodes = [
          'custom_attribute' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'custom_attribute1' => MagentoFrameworkDBDdlTable::TYPE_TEXT

          ];

          foreach ($entityAttributesCodes as $code => $type)

          $quoteInstaller->addAttribute('quote', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);
          $salesInstaller->addAttribute('order', $code, ['type' => $type, 'length'=> 255, 'visible' => false,'nullable' => true,]);
          $salesInstaller->addAttribute('invoice', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);


          $setup->endSetup();







          share|improve this answer

























          • Thank you very much, I was just about to add a solution.

            – André Ferraz
            Aug 18 '16 at 12:55






          • 4





            This is only for creating an empty extra column in order table right? How can we copy the data from a custom attribute into this extra column in order table?

            – Magento Learner
            Apr 24 '17 at 10:58






          • 1





            What is the better of the two ways?

            – Alex
            Apr 20 '18 at 8:16











          • Is this attribute pass in any third party order manage api?

            – Dhaduk Mitesh
            Mar 1 at 4:50













          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f131935%2fmagento-2-how-to-create-new-order-attribute-programatically%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          24














          Practically, there are two main ways to add an order attribute (a new column) to order via an upgrade script.



          --Using $setup->getConnection()->addColumn()



          app/code/Vendor/SalesOrder/Setup/UpgradeSchema.php



          <?php

          namespace VendorSalesOrderSetup;

          use MagentoFrameworkSetupUpgradeSchemaInterface;
          use MagentoFrameworkSetupSchemaSetupInterface;
          use MagentoFrameworkSetupModuleContextInterface;

          class UpgradeSchema implements UpgradeSchemaInterface

          /**
          * Upgrades DB schema for a module
          *
          * @param SchemaSetupInterface $setup
          * @param ModuleContextInterface $context
          * @return void
          */
          public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

          $setup->startSetup();

          $quote = 'quote';
          $orderTable = 'sales_order';

          $setup->getConnection()
          ->addColumn(
          $setup->getTable($quote),
          'custom_attribute',
          [
          'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'length' => 255,
          'comment' =>'Custom Attribute'
          ]
          );
          //Order table
          $setup->getConnection()
          ->addColumn(
          $setup->getTable($orderTable),
          'custom_attribute',
          [
          'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'length' => 255,
          'comment' =>'Custom Attribute'
          ]
          );

          $setup->endSetup();




          --Using Quote and Sale Setup Factory



          app/code/Vendor/SalesOrder/Setup/UpgradeData.php



          <?php

          namespace VendorSalesOrderSetup;

          use MagentoFrameworkSetupModuleContextInterface;
          use MagentoFrameworkSetupModuleDataSetupInterface;
          use MagentoFrameworkSetupUpgradeDataInterface;
          use MagentoQuoteSetupQuoteSetupFactory;
          use MagentoSalesSetupSalesSetupFactory;

          class UpgradeData implements UpgradeDataInterface

          /**
          * @var QuoteSetupFactory
          */
          protected $quoteSetupFactory;

          /**
          * @var SalesSetupFactory
          */
          protected $salesSetupFactory;

          /**
          * @param QuoteSetupFactory $quoteSetupFactory
          * @param SalesSetupFactory $salesSetupFactory
          */
          public function __construct(
          QuoteSetupFactory $quoteSetupFactory,
          SalesSetupFactory $salesSetupFactory
          )
          $this->quoteSetupFactory = $quoteSetupFactory;
          $this->salesSetupFactory = $salesSetupFactory;

          /**
          * Upgrades DB for a module
          *
          * @param ModuleDataSetupInterface $setup
          * @param ModuleContextInterface $context
          * @return void
          */
          public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

          /** @var MagentoQuoteSetupQuoteSetup $quoteInstaller */
          $quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);

          /** @var MagentoSalesSetupSalesSetup $salesInstaller */
          $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);

          $setup->startSetup();

          //Add multiple attributes to quote
          $entityAttributesCodes = [
          'custom_attribute' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'custom_attribute1' => MagentoFrameworkDBDdlTable::TYPE_TEXT

          ];

          foreach ($entityAttributesCodes as $code => $type)

          $quoteInstaller->addAttribute('quote', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);
          $salesInstaller->addAttribute('order', $code, ['type' => $type, 'length'=> 255, 'visible' => false,'nullable' => true,]);
          $salesInstaller->addAttribute('invoice', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);


          $setup->endSetup();







          share|improve this answer

























          • Thank you very much, I was just about to add a solution.

            – André Ferraz
            Aug 18 '16 at 12:55






          • 4





            This is only for creating an empty extra column in order table right? How can we copy the data from a custom attribute into this extra column in order table?

            – Magento Learner
            Apr 24 '17 at 10:58






          • 1





            What is the better of the two ways?

            – Alex
            Apr 20 '18 at 8:16











          • Is this attribute pass in any third party order manage api?

            – Dhaduk Mitesh
            Mar 1 at 4:50















          24














          Practically, there are two main ways to add an order attribute (a new column) to order via an upgrade script.



          --Using $setup->getConnection()->addColumn()



          app/code/Vendor/SalesOrder/Setup/UpgradeSchema.php



          <?php

          namespace VendorSalesOrderSetup;

          use MagentoFrameworkSetupUpgradeSchemaInterface;
          use MagentoFrameworkSetupSchemaSetupInterface;
          use MagentoFrameworkSetupModuleContextInterface;

          class UpgradeSchema implements UpgradeSchemaInterface

          /**
          * Upgrades DB schema for a module
          *
          * @param SchemaSetupInterface $setup
          * @param ModuleContextInterface $context
          * @return void
          */
          public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

          $setup->startSetup();

          $quote = 'quote';
          $orderTable = 'sales_order';

          $setup->getConnection()
          ->addColumn(
          $setup->getTable($quote),
          'custom_attribute',
          [
          'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'length' => 255,
          'comment' =>'Custom Attribute'
          ]
          );
          //Order table
          $setup->getConnection()
          ->addColumn(
          $setup->getTable($orderTable),
          'custom_attribute',
          [
          'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'length' => 255,
          'comment' =>'Custom Attribute'
          ]
          );

          $setup->endSetup();




          --Using Quote and Sale Setup Factory



          app/code/Vendor/SalesOrder/Setup/UpgradeData.php



          <?php

          namespace VendorSalesOrderSetup;

          use MagentoFrameworkSetupModuleContextInterface;
          use MagentoFrameworkSetupModuleDataSetupInterface;
          use MagentoFrameworkSetupUpgradeDataInterface;
          use MagentoQuoteSetupQuoteSetupFactory;
          use MagentoSalesSetupSalesSetupFactory;

          class UpgradeData implements UpgradeDataInterface

          /**
          * @var QuoteSetupFactory
          */
          protected $quoteSetupFactory;

          /**
          * @var SalesSetupFactory
          */
          protected $salesSetupFactory;

          /**
          * @param QuoteSetupFactory $quoteSetupFactory
          * @param SalesSetupFactory $salesSetupFactory
          */
          public function __construct(
          QuoteSetupFactory $quoteSetupFactory,
          SalesSetupFactory $salesSetupFactory
          )
          $this->quoteSetupFactory = $quoteSetupFactory;
          $this->salesSetupFactory = $salesSetupFactory;

          /**
          * Upgrades DB for a module
          *
          * @param ModuleDataSetupInterface $setup
          * @param ModuleContextInterface $context
          * @return void
          */
          public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

          /** @var MagentoQuoteSetupQuoteSetup $quoteInstaller */
          $quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);

          /** @var MagentoSalesSetupSalesSetup $salesInstaller */
          $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);

          $setup->startSetup();

          //Add multiple attributes to quote
          $entityAttributesCodes = [
          'custom_attribute' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'custom_attribute1' => MagentoFrameworkDBDdlTable::TYPE_TEXT

          ];

          foreach ($entityAttributesCodes as $code => $type)

          $quoteInstaller->addAttribute('quote', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);
          $salesInstaller->addAttribute('order', $code, ['type' => $type, 'length'=> 255, 'visible' => false,'nullable' => true,]);
          $salesInstaller->addAttribute('invoice', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);


          $setup->endSetup();







          share|improve this answer

























          • Thank you very much, I was just about to add a solution.

            – André Ferraz
            Aug 18 '16 at 12:55






          • 4





            This is only for creating an empty extra column in order table right? How can we copy the data from a custom attribute into this extra column in order table?

            – Magento Learner
            Apr 24 '17 at 10:58






          • 1





            What is the better of the two ways?

            – Alex
            Apr 20 '18 at 8:16











          • Is this attribute pass in any third party order manage api?

            – Dhaduk Mitesh
            Mar 1 at 4:50













          24












          24








          24







          Practically, there are two main ways to add an order attribute (a new column) to order via an upgrade script.



          --Using $setup->getConnection()->addColumn()



          app/code/Vendor/SalesOrder/Setup/UpgradeSchema.php



          <?php

          namespace VendorSalesOrderSetup;

          use MagentoFrameworkSetupUpgradeSchemaInterface;
          use MagentoFrameworkSetupSchemaSetupInterface;
          use MagentoFrameworkSetupModuleContextInterface;

          class UpgradeSchema implements UpgradeSchemaInterface

          /**
          * Upgrades DB schema for a module
          *
          * @param SchemaSetupInterface $setup
          * @param ModuleContextInterface $context
          * @return void
          */
          public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

          $setup->startSetup();

          $quote = 'quote';
          $orderTable = 'sales_order';

          $setup->getConnection()
          ->addColumn(
          $setup->getTable($quote),
          'custom_attribute',
          [
          'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'length' => 255,
          'comment' =>'Custom Attribute'
          ]
          );
          //Order table
          $setup->getConnection()
          ->addColumn(
          $setup->getTable($orderTable),
          'custom_attribute',
          [
          'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'length' => 255,
          'comment' =>'Custom Attribute'
          ]
          );

          $setup->endSetup();




          --Using Quote and Sale Setup Factory



          app/code/Vendor/SalesOrder/Setup/UpgradeData.php



          <?php

          namespace VendorSalesOrderSetup;

          use MagentoFrameworkSetupModuleContextInterface;
          use MagentoFrameworkSetupModuleDataSetupInterface;
          use MagentoFrameworkSetupUpgradeDataInterface;
          use MagentoQuoteSetupQuoteSetupFactory;
          use MagentoSalesSetupSalesSetupFactory;

          class UpgradeData implements UpgradeDataInterface

          /**
          * @var QuoteSetupFactory
          */
          protected $quoteSetupFactory;

          /**
          * @var SalesSetupFactory
          */
          protected $salesSetupFactory;

          /**
          * @param QuoteSetupFactory $quoteSetupFactory
          * @param SalesSetupFactory $salesSetupFactory
          */
          public function __construct(
          QuoteSetupFactory $quoteSetupFactory,
          SalesSetupFactory $salesSetupFactory
          )
          $this->quoteSetupFactory = $quoteSetupFactory;
          $this->salesSetupFactory = $salesSetupFactory;

          /**
          * Upgrades DB for a module
          *
          * @param ModuleDataSetupInterface $setup
          * @param ModuleContextInterface $context
          * @return void
          */
          public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

          /** @var MagentoQuoteSetupQuoteSetup $quoteInstaller */
          $quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);

          /** @var MagentoSalesSetupSalesSetup $salesInstaller */
          $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);

          $setup->startSetup();

          //Add multiple attributes to quote
          $entityAttributesCodes = [
          'custom_attribute' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'custom_attribute1' => MagentoFrameworkDBDdlTable::TYPE_TEXT

          ];

          foreach ($entityAttributesCodes as $code => $type)

          $quoteInstaller->addAttribute('quote', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);
          $salesInstaller->addAttribute('order', $code, ['type' => $type, 'length'=> 255, 'visible' => false,'nullable' => true,]);
          $salesInstaller->addAttribute('invoice', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);


          $setup->endSetup();







          share|improve this answer















          Practically, there are two main ways to add an order attribute (a new column) to order via an upgrade script.



          --Using $setup->getConnection()->addColumn()



          app/code/Vendor/SalesOrder/Setup/UpgradeSchema.php



          <?php

          namespace VendorSalesOrderSetup;

          use MagentoFrameworkSetupUpgradeSchemaInterface;
          use MagentoFrameworkSetupSchemaSetupInterface;
          use MagentoFrameworkSetupModuleContextInterface;

          class UpgradeSchema implements UpgradeSchemaInterface

          /**
          * Upgrades DB schema for a module
          *
          * @param SchemaSetupInterface $setup
          * @param ModuleContextInterface $context
          * @return void
          */
          public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

          $setup->startSetup();

          $quote = 'quote';
          $orderTable = 'sales_order';

          $setup->getConnection()
          ->addColumn(
          $setup->getTable($quote),
          'custom_attribute',
          [
          'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'length' => 255,
          'comment' =>'Custom Attribute'
          ]
          );
          //Order table
          $setup->getConnection()
          ->addColumn(
          $setup->getTable($orderTable),
          'custom_attribute',
          [
          'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'length' => 255,
          'comment' =>'Custom Attribute'
          ]
          );

          $setup->endSetup();




          --Using Quote and Sale Setup Factory



          app/code/Vendor/SalesOrder/Setup/UpgradeData.php



          <?php

          namespace VendorSalesOrderSetup;

          use MagentoFrameworkSetupModuleContextInterface;
          use MagentoFrameworkSetupModuleDataSetupInterface;
          use MagentoFrameworkSetupUpgradeDataInterface;
          use MagentoQuoteSetupQuoteSetupFactory;
          use MagentoSalesSetupSalesSetupFactory;

          class UpgradeData implements UpgradeDataInterface

          /**
          * @var QuoteSetupFactory
          */
          protected $quoteSetupFactory;

          /**
          * @var SalesSetupFactory
          */
          protected $salesSetupFactory;

          /**
          * @param QuoteSetupFactory $quoteSetupFactory
          * @param SalesSetupFactory $salesSetupFactory
          */
          public function __construct(
          QuoteSetupFactory $quoteSetupFactory,
          SalesSetupFactory $salesSetupFactory
          )
          $this->quoteSetupFactory = $quoteSetupFactory;
          $this->salesSetupFactory = $salesSetupFactory;

          /**
          * Upgrades DB for a module
          *
          * @param ModuleDataSetupInterface $setup
          * @param ModuleContextInterface $context
          * @return void
          */
          public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

          /** @var MagentoQuoteSetupQuoteSetup $quoteInstaller */
          $quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);

          /** @var MagentoSalesSetupSalesSetup $salesInstaller */
          $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);

          $setup->startSetup();

          //Add multiple attributes to quote
          $entityAttributesCodes = [
          'custom_attribute' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
          'custom_attribute1' => MagentoFrameworkDBDdlTable::TYPE_TEXT

          ];

          foreach ($entityAttributesCodes as $code => $type)

          $quoteInstaller->addAttribute('quote', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);
          $salesInstaller->addAttribute('order', $code, ['type' => $type, 'length'=> 255, 'visible' => false,'nullable' => true,]);
          $salesInstaller->addAttribute('invoice', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);


          $setup->endSetup();








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 21 at 2:55









          AleGrinGo

          2532 silver badges12 bronze badges




          2532 silver badges12 bronze badges










          answered Aug 18 '16 at 12:54









          Khoa TruongDinhKhoa TruongDinh

          22.9k6 gold badges45 silver badges91 bronze badges




          22.9k6 gold badges45 silver badges91 bronze badges












          • Thank you very much, I was just about to add a solution.

            – André Ferraz
            Aug 18 '16 at 12:55






          • 4





            This is only for creating an empty extra column in order table right? How can we copy the data from a custom attribute into this extra column in order table?

            – Magento Learner
            Apr 24 '17 at 10:58






          • 1





            What is the better of the two ways?

            – Alex
            Apr 20 '18 at 8:16











          • Is this attribute pass in any third party order manage api?

            – Dhaduk Mitesh
            Mar 1 at 4:50

















          • Thank you very much, I was just about to add a solution.

            – André Ferraz
            Aug 18 '16 at 12:55






          • 4





            This is only for creating an empty extra column in order table right? How can we copy the data from a custom attribute into this extra column in order table?

            – Magento Learner
            Apr 24 '17 at 10:58






          • 1





            What is the better of the two ways?

            – Alex
            Apr 20 '18 at 8:16











          • Is this attribute pass in any third party order manage api?

            – Dhaduk Mitesh
            Mar 1 at 4:50
















          Thank you very much, I was just about to add a solution.

          – André Ferraz
          Aug 18 '16 at 12:55





          Thank you very much, I was just about to add a solution.

          – André Ferraz
          Aug 18 '16 at 12:55




          4




          4





          This is only for creating an empty extra column in order table right? How can we copy the data from a custom attribute into this extra column in order table?

          – Magento Learner
          Apr 24 '17 at 10:58





          This is only for creating an empty extra column in order table right? How can we copy the data from a custom attribute into this extra column in order table?

          – Magento Learner
          Apr 24 '17 at 10:58




          1




          1





          What is the better of the two ways?

          – Alex
          Apr 20 '18 at 8:16





          What is the better of the two ways?

          – Alex
          Apr 20 '18 at 8:16













          Is this attribute pass in any third party order manage api?

          – Dhaduk Mitesh
          Mar 1 at 4:50





          Is this attribute pass in any third party order manage api?

          – Dhaduk Mitesh
          Mar 1 at 4:50

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f131935%2fmagento-2-how-to-create-new-order-attribute-programatically%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form