3

I have been trying to create a new order attribute in Magento 2, but not successful yet.

I have created a new module and this is already showing.

I added the different codes available in my setup InstallData.php but nothing worked so far.

Anybody who have successfully created an order attribute?

This is one of the code I have tried in InstallSchema.php

<?php
namespace Namespace\Module\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{

/**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
   public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
   {
    $installer = $setup;
    $installer->startSetup();

    $installer->getConnection()->addColumn(
        $installer->getTable('quote'),
        'delivery_eta',
        [
            'type' => 'varchar',
            'nullable' => false,
            'comment' => 'Delivery ETA',
        ]
    );

    $installer->getConnection()->addColumn(
        $installer->getTable('sales_order'),
        'delivery_eta',
        [
            'type' => 'datetime',
            'nullable' => false,
            'comment' => 'Delivery ETA',
        ]
    );

    $setup->endSetup();
   }
}

2 Answers 2

3

The schema scripts is to create a database structure. For example, creating a table and add columns to existing table. The install data scripts is to manage the data within existing tables, adding some sample data to tables.

At first time, we run php bin/magento setup:upgrade, Magento will execute the files in following order: (Creating table structure first, and then add the data to table)

  • InstallSchema.php
  • UpgradeSchema.php
  • InstallData.php
  • UpgradeData.php

In the next time, run setup upgrade command again, only two files:

  • UpgradeSchema.php
  • UpgradeData.php

So in your case, when editing the install data script that will not effect your database anymore. If we want to re-install, we may need to remove our module from table setup_module table (and, may need to remove the old database). And run setup upgrade command again.

For the adding new order attribute, we should take a look: https://magento.stackexchange.com/a/131969/33057

4
  • Thank you so much. As you mentioned the 'setup_module' table was preventing my install script from running.Deleted the table entry and now all is set..Also one moe query if i need to add another attribute i should be using the UpgradeSchema.php set up right..? Commented Dec 14, 2016 at 8:45
  • Yes, you have to use UpgradeSchema.php to add another attribute but you also have to change module version (increasing it) or delete entry in setup_module table again.
    – WaPoNe
    Commented Dec 14, 2016 at 15:58
  • @GeethikaAntony it depends, if you don't try to create new column, we can use UpgradeSchema.php. I'm not sure for all attributes. We need to check again. For example, add new product attribute, we don't need to use UpgradeSchema.php, we should use install data script. Commented Dec 14, 2016 at 16:03
  • @KhoaTruongDinh yes I would like to add another column to the quote and order table. Actually i still did not get the difference between the usage of UpgradeSchema.php and UpgradeInstall.php. Shall try the different scenarios and update here. Commented Dec 15, 2016 at 5:03
0

Please check the below steps to add a new attribute to sales_order, quote, and sales_invoice tables according to the new db_schema.xml format.

Custom/Module/etc/db_schema.xml

<?xml version="1.0"?>

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="sales_order" resource="sales" engine="innodb" comment="Sales Order">
        <column xsi:type="varchar" name="custom_attribute" nullable="true" length="255" comment="custom attribute"/>
    </table>
    <table name="quote" resource="checkout" engine="innodb" comment="Quote">
        <column xsi:type="varchar" name="custom_attribute" nullable="true" length="255" comment="custom attribute"/>
    </table>
    <table name="sales_invoice" resource="sales" engine="innodb" comment="Sales Invoice">
        <column xsi:type="varchar" name="custom_attribute" nullable="true" length="255" comment="custom attribute"/>
    </table>
</schema>

After that please run php bin/magento setup:upgrade command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.