2

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!

1 Answer 1

6
  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;
}
}
4
  • Thanks! it works, but now how can I get the custom field? can I use getPakkeShipmentId() or I would need to create this method? greetings! Commented Jan 2, 2020 at 16:09
  • You can directly get using $quote->getPakkeShipmentid() Commented Jan 2, 2020 at 16:14
  • Thanks, it worked! Commented Jan 2, 2020 at 16:37
  • @OscarVazquez Welcome :-) Commented Jan 3, 2020 at 3:18

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.