Magento – Add a custom attribute to company in magento 2.2.2 for B2B

b2bmagento2magento2.2magento2.2.2

I am using B2B extensions and I want to add custom attributes to company entity, As a text field and create 6-digit number randomly for that i'm using

$customcompanyattribute     = mt_rand(100000, 999999);

Attribute should be read-only and should generate value when a new company form is submitted.

I found that we don't have a mechanism like Magento has for adding attributes to customer CustomerSetupFactory, and using

Here is the work i did.

app/code/Custom/OverrideCompany/view/base/ui_component/company_form.xml

 <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="general">
        <field name="account_number" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                <item name="disabled" xsi:type="boolean">true</item>

                    <item name="source" xsi:type="string">Account Number</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
                <dataType>text</dataType>
                <label translate="true">Account Number</label>
                <dataScope>account_number</dataScope>
            </settings>
        </field>

    </fieldset>
</form>

app/code/Custom/OverrideCompany/Setup/InstallSchema.php

   <?php
namespace Custom\OverrideCompany\Setup;

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

class InstallSchema implements InstallSchemaInterface {
    const COMPANY_TABLE_NAME = 'company';

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context){
        $setup->startSetup();
        $table = $setup->getTable(self::COMPANY_TABLE_NAME);

        $columns = [
            'account_number',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 11,
                    'nullable' => true,
                    'comment' => 'Account Number'
            ],
        ];
        $connection = $setup->getConnection();
        foreach ($columns as $col_name => $col_array){
            $connection->addColumn($table, $col_name, $col_array);
        }
        $setup->endSetup();
    }
}

app/code/Custom/OverrideCompany/Setup/UpgradeSchema.php

<?php

namespace Custom\OverrideCompany\Setup;

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

class UpgradeSchema implements UpgradeSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function upgrade(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $installer = $setup;

        $installer->startSetup();
        if (version_compare($context->getVersion(), '1.0.0', '<')) {
                $installer->getConnection()->addColumn(
                $installer->getTable('company'),
                'account_number',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 11,
                    'nullable' => true,
                    'comment' => 'Account Number'
                ]
            );
        }
        $installer->endSetup();
    }
}

enter image description here
How to achieve it.

Best Answer

I am assuming that you have your custom column added in the database and custom field present on the admin company edit page in the General section.

  1. For reading the column value from the database into the field on the admin page:

Add in di.xml:

<!-- override core class in order to have the custom column show on the admin company edit page -->
<preference for="Magento\Company\Model\Company\DataProvider" type="MyVendor\MyModule\Model\Company\DataProvider" />

And in your DataProvider.php:

namespace MyVendor\MyModule\Model\Company;

class DataProvider extends \Magento\Company\Model\Company\DataProvider {
    
    public function getGeneralData(\Magento\Company\Api\Data\CompanyInterface $company) {
        
        $result = parent::getGeneralData($company);
        
        # add custom column value to the General data section so that the value populates the custom field on the admin company edit page
        $result['my_custom_column'] = $company->setMyCustomColumn();
        
        return $result;
    }
}

  1. For saving the field into the database custom column:

Add in di.xml:

<!-- override core class by plugin in order to save value from the admin company edit page into the custom column my_custom_column -->
<type name="Magento\Company\Model\CompanyRepository">
    <plugin name="myvendor_mymodule_after_save_company" type="MyVendor\MyModule\Plugin\Model\CompanyRepository" />
</type>

And in your CompanyRepository.php:

namespace MyVendor\MyModule\Plugin\Model;

class CompanyRepository {
    
    public function afterSave(\Magento\Company\Model\CompanyRepository $companyRepository, \Magento\Company\Api\Data\CompanyInterface $company) {
        
        $my_custom_column = 'some value here';
        
        $company->setMyCustomColumn($my_custom_column);
        
        $company->save();
        
        return $company;
    }
}
Related Topic