Salesattribute

Install Magento 2 sales order attributes programmatically.

Use the snippet in the Magento 2 module creator.

Files

Setup/InstallData.php

<?php


namespace Mage2Gen\Module\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Quote\Setup\QuoteSetupFactory;

class InstallData implements InstallDataInterface
{

    private $quoteSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Quote\Setup\QuoteSetupFactory $quoteSetupFactory
     */
    public function __construct(QuoteSetupFactory $quoteSetupFactory)
    {
        $this->quoteSetupFactory = $quoteSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $quoteSetup = $this->quoteSetupFactory->create(['setup' => $setup]);
        $quoteSetup->addAttribute('quote', 'test',
            [
                'type' => 'None',
                'length' => null,
                'visible' => false,
                'required' => true,
                'grid' => false
            ]
        );
    }
}