3

I have added custom fields in sales_shipment_item table. I just want to save a value in that custom fields on a particular event.
How can I do it?

5
  • in which particular event would you like to add the value to custom fields?
    – fmsthird
    Mar 16, 2019 at 9:55
  • on ready for dispatch . I call a funtction in which i just want to save value in custom fields. Mar 16, 2019 at 9:58
  • can you post your function? let's see how we can make it work
    – fmsthird
    Mar 16, 2019 at 10:00
  • 2
    Magento\Sales\Model\Order\Shipment\Item is the class which is responsible for your requirement based on entity_id you can set value @kimnana Mar 16, 2019 at 10:15
  • I was little bit confuse.on ready for dispatch I want to create shipment and while creating the shipment also have save custom fields value . I dont have entity_id. Mar 16, 2019 at 12:11

1 Answer 1

2

Hi You Can do this by creating an event "sales_order_shipment_save_before"

Create a sample module Stackoverflow_Answer and follow below instruction.

step1: Create custom fields in sales_shipment_item table as Below

Create InstallSchema.php under "app/code/Stackoverflow/Answer/Setup/InstallSchema.php

<?php

namespace Stackoverflow\Answer\Setup;

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

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();


        $setup->getConnection()->addColumn(
            $setup->getTable('sales_shipment_item'),
            'custom_field',
            [
                'type' => Table::TYPE_TEXT,
                'nullable' => true,
                'default' => NULL,
                'length' => 255,
                'comment' => 'Custom Field Text'
            ] 
        );

    $setup->endSetup();    
    }
}
?>    

step2: create an event "sales_order_shipment_save_before" Create events.xml under app/code/Stackoverflow/Answer/etc/adminhtml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_shipment_save_before">
        <observer name="stackoverflow_beforeshipment" instance="Stackoverflow\Answer\Observer\BeforeShipment" />
    </event>

</config>

step3: create observer file BeforeShipment.php under app/code/Stackoverflow/Answer/Observer

<?php

namespace Stackoverflow\Answer\Observer;

use Magento\Framework\Event\ObserverInterface;    

class BeforeShipment implements ObserverInterface
{    
    protected $_logger;
    public function __construct(\Psr\Log\LoggerInterface $logger)
    {
        $this->_logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $shipment = $observer->getEvent()->getShipment();
        foreach ($shipment->getItemsCollection() as $item) {    
                $item->setCustomField('test info');
        }
        return $this;
    }
}

enter image description here

I hope it helps. Also, I am quite confused about to do this with extension_attribute and plugin.

7
  • it doesnt save custm attribute value. Mar 19, 2019 at 9:09
  • okay, how can i share my package so you can check. Did you faced any issue? Mar 19, 2019 at 17:18
  • Its resolved using $item->setData('custom_field','test info2'); . now its workin perfectly . Thank you. Mar 20, 2019 at 6:29
  • can you help me passed dynamic data to observer and save it Mar 20, 2019 at 13:26
  • Hi your solution is working perfectly . But I have lot stuffs to do , Can I call this observer after shipment Create . Thanks Mar 27, 2019 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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