1

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.

Your Answer

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

Browse other questions tagged or ask your own question.