Question

I wan't to add data in a custom field that I created, I used InstallSchema like this:

Setup/InstallSchema.php

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

class InstallSchema implements InstallSchemaInterface

{

public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
    $installer = $setup;
    $installer->startSetup();

    $installer->getConnection()->addColumn(
        $installer->getTable('quote'),
        'pakke_shipmentid',
        [
            'type' => 'text',
            'nullable' => true,
            'comment' => 'Pakke Shipment Id',
        ]
    );


    $installer->getConnection()->addColumn(
        $installer->getTable('sales_order'),
        'pakke_shipmentid',
        [
            'type' => 'text',
            'nullable' => true,
            'comment' => 'Pakke Shipment Id',
        ]
    );


    $installer->getConnection()->addColumn(
        $installer->getTable('sales_order_grid'),
        'pakke_shipmentid',
        [
            'type' => 'text',
            'nullable' => true,
            'comment' => 'Pakke Shipment Id',
        ]
    );


    $setup->endSetup();
}

}

I will need to use it in observer.

Greetings!

Was it helpful?

Solution

  1. Setup/InstallSchema.php
<?php
namespace [Vendor]\[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'),
        'pakke_shipmentid',
        [
            'type' => 'text',
            'nullable' => true,
            'comment' => 'Pakke Shipment Id',
        ]
    );

    $installer->getConnection()->addColumn(
        $installer->getTable('sales_order'),
        'pakke_shipmentid',
        [
            'type' => 'text',
            'nullable' => true,
            'comment' => 'Pakke Shipment Id',
        ]
    );


    $installer->getConnection()->addColumn(
        $installer->getTable('sales_order_grid'),
        'pakke_shipmentid',
        [
            'type' => 'text',
            'nullable' => true,
            'comment' => 'Pakke Shipment Id',
        ]
    );

    $setup->endSetup();
}
}
  1. Create file events.xml in Vendor\Module\etc folder. Add event in events.xml file
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_model_service_quote_submit_before">
    <observer name="[name_of_event]" instance="Vendor\Module\Observer\SaveToOrderObserver"/>
</event>
</config>
  1. Create SaveToOrderObserver.php file in Vendor\Module\Observer folder
<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SaveToOrderObserver implements ObserverInterface
{
/**
 * @var \Magento\Framework\ObjectManagerInterface
 */
protected $_objectManager;

/**
 * @param \Magento\Framework\ObjectManagerInterface $objectmanager
 */
public function __construct(\Magento\Framework\ObjectManagerInterface $objectmanager)
{
    $this->_objectManager = $objectmanager;
}

public function execute(EventObserver $observer)
{
    $order = $observer->getOrder();
    $quoteRepository = $this->_objectManager->create('Magento\Quote\Model\QuoteRepository');
    /** @var \Magento\Quote\Model\Quote $quote */
    $quote = $quoteRepository->get($order->getQuoteId());
    $order->setPakkeShipmentid( $quote->getPakkeShipmentid() );

    return $this;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top