TransWikia.com

Magento 2.3 adding column to inventory_source

Magento Asked by Khaled Omara on October 2, 2021

I’ve added a new column to inventory_source successfully and it’s working, here is the tutorial I’ve followed.

The issue is the data saved to the database is not showed in the form, the field appear empty but the data is in the database.

The Steps I’ve done are:

  1. Created extension_attributes

  2. Created event to save the data

  3. Created inventory_source_form.xml to view the field

    <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
        <fieldset name="general" sortOrder="10">
            <field name="branch_erp_id" formElement="input" sortOrder="25">
                <settings>
                    <validation>
                        <rule name="validate-number" xsi:type="boolean">true</rule>
                    </validation>
                    <dataType>number</dataType>
                    <label translate="true">Branch ERP Id</label>
                </settings>
            </field>
        </fieldset>
    </form>```
    
    

The di.xml file:

<type name="MagentoInventoryApiApiSourceRepositoryInterface">
    <plugin name="plugin_source_save" type="vendormodulePluginInventorySourceSource"/>
</type>

The plugin file Source.php:

<?php

namespace vendormodulePluginInventorySource;

use MagentoInventoryApiApiSourceRepositoryInterface;
use MagentoInventoryApiApiDataSourceInterface;
use MagentoInventoryApiApiDataSourceSearchResultsInterface;
use MagentoInventoryApiApiDataSourceExtensionFactory;
use MagentoInventoryApiApiDataSourceExtensionInterfaceFactory;


class Source
{
    const FIELD_NAME = 'branch_erp_id';

    protected $extensionFactory;
    protected $sourceFactory;

    public function __construct(SourceExtensionInterfaceFactory $extensionFactory)
    {
        $this->extensionFactory = $extensionFactory;
    }


    public function afterGet(SourceRepositoryInterface $subject, SourceInterface $source)
    {
        $sourceBranchId = $source->getData(self::FIELD_NAME);
        $extensionAttributes = $source->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
        $extensionAttributes->setBranchErpId($sourceBranchId);
        $source->setExtensionAttributes($extensionAttributes);

        return $source;
    }

    public function afterGetList(SourceRepositoryInterface $subject, SourceSearchResultsInterface $result)
    {
        $products = [];
        $sources = $result->getItems();

        foreach ($sources as $source) {
            $sourceBranchId = $source->getData(self::FIELD_NAME);
            $extensionAttributes = $source->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
            $extensionAttributes->setBranchErpId($sourceBranchId);
            $source->setExtensionAttributes($extensionAttributes);
            $products[] = $source;
        }
        $result->setItems($products);
        return $result;
    }
}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP