Answers for "how to add multile attributes using upgradeSchema in magento 2"

PHP
0

how to add multile attributes using upgradeSchema in magento 2

<?php 

namespace MyCompany\Extendedcustomer\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{


protected $customerSetupFactory;

public function upgrade(
    ModuleDataSetupInterface $setup, 
    ModuleContextInterface $context
) {
    $setup->startSetup();

    if (version_compare($context->getVersion(), '1.0.1') < 0) {

        $setup->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'current_device',                    
                [
                    'type' => 'text',
                    'label' => 'Current Device',
                    'input' => 'text',
                    'required' => false,
                    'visible' => true,
                    'user_defined' => true,
                    'sort_order' => 300,
                    'position' => 400,
                    'default' => '',
                    'system' => false,
                    'is_used_in_grid' => 0,
                    'is_visible_in_grid' => 1,
                    'is_filterable_in_grid' => 1,
                    'is_searchable_in_grid' => 1,
                ]
            );
        $Attribute = $customerSetup->getEavConfig()->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'current_device');
        $Attribute->setData(
        'used_in_forms',
            [
                'adminhtml_customer',
                'customer_account_edit',
                'adminhtml_checkout',
                'adminhtml_customer_address',
                'customer_address_edit',
                'customer_register_address',
            ]
        );
        $Attribute->save();

        $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'current_scandevice',                    
                [
                    'type' => 'text',
                    'label' => 'Current RF Scan Device',
                    'input' => 'text',
                    'required' => false,
                    'visible' => true,
                    'user_defined' => true,
                    'sort_order' => 300,
                    'position' => 400,
                    'default' => '',
                    'system' => false,
                    'is_used_in_grid' => 0,
                    'is_visible_in_grid' => 1,
                    'is_filterable_in_grid' => 1,
                    'is_searchable_in_grid' => 1,
                ]
            );
        $Attribute = $customerSetup->getEavConfig()->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'current_scandevice');
        $Attribute->setData(
        'used_in_forms',
            [
                'adminhtml_customer',
                'customer_account_edit',
                'adminhtml_checkout',
                'adminhtml_customer_address',
                'customer_address_edit',
                'customer_register_address',
            ]
        );
        $Attribute->save();

        $setup->endSetup();

     }

   }
}
Posted by: Guest on March-12-2021

Code answers related to "how to add multile attributes using upgradeSchema in magento 2"

Browse Popular Code Answers by Language