I'm brand new to Magento 2 programming, although I have a number of years of PHP behind me.

I need to store an id from an external system alongside an order that that system creates.

I've got the code that creates the quote and turns it into an order working, alongside my custom payment method, but I can't get it to save my extra ID.

I've used and altered code found elsewhere on this site to create the order:

public function doCreate($dataArray, $order=null) {
    foreach($dataArray->Orders as $orderData) {
    $websiteId = $this->storeManager->getStore()->getWebsiteId();
    $customer->loadByEmail($orderData->ShippingInfo->Email);// load customet by email address
    $customerName=explode(' ',(string)$orderData->ShippingInfo->RecipientName);
    echo "First $firstName  Last $lastName \n";
        //If not avilable then create this customer
    $quote=$this->quote->create(); //Create object of quote
    $quote->setStore($store); //set store for which you create quote
    // if you have allready buyer id then you can load customer directly
    $customer= $this->customerRepository->getById($customer->getEntityId());
    $quote->assignCustomer($customer); //Assign quote to customer

    //add items in quote
    foreach($orderData->OrderItems as $item){

    //Set Address to quote

    $address=array(     //address Details
        'firstname'     => (string)$firstName, 
        'lastname'      => (string)$lastName,
        'street'        => (string)$shippingInfo->Address1,
        'city'          => (string)$shippingInfo->City,
        'country_id'    => (string)$shippingInfo->Country,
        'region'        => (string)$shippingInfo->State,
        'postcode'      => (string)$shippingInfo->PostalCode,
        'telephone'     => (string)$shippingInfo->PhoneNumber,
        'save_in_address_book' => 0


    // Collect Rates and Set Shipping & Payment Method

                    ->setShippingMethod('freeshipping_freeshipping'); //shipping method  $shippingInfo->Service
    $quote->setPaymentMethod('cpayment'); //payment method
    $quote->setInventoryProcessed(false); //not effetc inventory
    $quote->save(); //Now Save quote and your quote is ready

    // Set Sales Order Payment
    $quote->getPayment()->importData(['method' => 'cpayment']);

    // Collect Totals & Save Quote

    // Create Order From Quote
    $order = $this->quoteManagement->submit($quote);
    $increment_id = $order->getRealOrderId();
        $result['order_id']= $order->getRealOrderId();
        $order->addData('external_id',$dataArray->ExtId);//THIS IS THE ID I NEED TO SAVE
        $result=['error'=>1,'msg'=>'Your custom message'];
    echo "Complete: Final result is\n";
    return $result;

If I add debugging near the end, eg echo $order->getData('external_id') then it shows up, but it's not saved in the database.

I guess I must be missing some XML somewhere, but as I said I'm brand new to Magento and finding the lack of documentation frustrating (mostly seems to be 'copy and paste this and it will work'!)

Thanks in advance...

  • You try to use $order->setData(). Commented Aug 5, 2016 at 12:20
  • Sorry, yes I should point out I have already tried $order->setData() too. Commented Aug 5, 2016 at 13:31
  • In table sales_order has column external_id? Commented Aug 5, 2016 at 15:44
  • No, that column doesn't exist. Do I have to do something to create it? (I'm writing a plugin by the way, so I can't just manually create a column in the DB - it would need to be done programatically). Thanks for your help on this by the way! Commented Aug 5, 2016 at 16:04
  • Bump... Can anybody shed light on how to do this with a plugin - ie not making changes to the core code? Commented Aug 15, 2016 at 8:07

1 Answer 1


From my experience:

First of all, we need to create a new column to two tables: quote and sales_order tables.



namespace Vendor\SalesOrder\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

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)

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

                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'comment' =>'External ID'
        //Order Grid table
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'comment' =>'External ID'


Second, create fieldset.xml


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <scope id="global">
        <fieldset id="sales_convert_quote">
            <field name="external_id">
                <aspect name="to_order" />

Basically, this fieldset is used for defining the attribute which will be copied from quote object to order object automatically.

Don't forget to create registration.php, module.xml and run setup upgrade command.

After that, now we can create more functions to work with quote and order objects.

  • Hi, thank you very much for this. Apologies for the newbie questions! As I'm developing this plugin that will end up in the Magento store, how would I get this code into the Vendor/SalesOrder/Setup folder? I was under the impression that I could only provide code in my own module's folder? I did try creating a SalesOrder folder in my own module, but, predicatably, this didn't work! Commented Aug 8, 2016 at 11:52

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.