skip to Main Content

Please specify how to add multiple attributes in single InstallData script

2

Answers


  1. Chosen as BEST ANSWER

    thanks for the above solution using patches. It's working and I used the same methodology using InstallData/UpgradeData.php according to my requirement.

    Please check my answer This will save the data in the database in table customer_entity_varchar and attributes in eav_attribute. Check the code:

    <?php
    namespace CustomB2BRFQModuleSetup;
    
    use MagentoCustomerModelCustomer;
    use MagentoFrameworkSetupModuleContextInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoFrameworkSetupUpgradeDataInterface;
    use MagentoCustomerSetupCustomerSetupFactory;
    use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
    
    class UpgradeData implements MagentoFrameworkSetupUpgradeDataInterface
    {
    private $eavSetupFactory;
    
    private $eavConfig;
    
    private $attributeResource;
    
    private $customerSetupFactory;
    
    /**
    * @var AttributeSetFactory
    */
    protected $attributeSetFactory;
    
    protected $moduleDataSetup;
    
    
    public function __construct(
    MagentoEavSetupEavSetupFactory $eavSetupFactory,
    MagentoEavModelConfig $eavConfig,
    MagentoCustomerModelResourceModelAttribute $attributeResource,
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory,
    ModuleDataSetupInterface $moduleDataSetup
    ) {
    $this->eavSetupFactory = $eavSetupFactory;
    $this->eavConfig = $eavConfig;
    $this->attributeResource = $attributeResource;
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
    $this->moduleDataSetup = $moduleDataSetup;
    
    }
    
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
    
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    //$customerSetup->removeAttribute(Customer::ENTITY, "phonenumber");
    
    $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    
    
    /** attribute_1 */
    $customerSetup->addAttribute(
    Customer::ENTITY,
    'phonenumber',
    [
    'type' => 'varchar',
    'label' => 'Phone Number',
    'input' => 'text',
    'required' => true,
    'visible' => true,
    'user_defined' => true,
    'sort_order' => 991,
    'position' => 991,
    'system' => 0,
    
    ]
    );
    
    $attribute = $customerSetup->getEavConfig()->getAttribute(
    Customer::ENTITY,
    'phonenumber'
    );
    
    $attribute->addData(
    [
    'attribute_set_id' => $attributeSetId,
    'attribute_group_id' => $attributeGroupId,
    'used_in_forms' => ['adminhtml_customer',
    'customer_account_create',
    'customer_account_edit']
    ]
    );
    
    $attribute->save();
    
    /** attribute_2 */
    $customerSetup->addAttribute(
    Customer::ENTITY,
    'gstnumber',
    [
    'type' => 'varchar',
    'label' => 'GST Number',
    'input' => 'text',
    'required' => true,
    'visible' => true,
    'user_defined' => true,
    'sort_order' => 992,
    'position' => 992,
    'system' => 0,
    ]
    );
    
    $attribute = $customerSetup->getEavConfig()->getAttribute(
    Customer::ENTITY,
    'gstnumber'
    );
    
    $attribute->addData(
    [
    'attribute_set_id' => $attributeSetId,
    'attribute_group_id' => $attributeGroupId,
    'used_in_forms' => ['adminhtml_customer',
    'customer_account_create',
    'customer_account_edit']
    ]
    );
    
    $attribute->save();
    
    
    }
    }
    ?>
    

  2. Magento 2 uses Data scripts to add attributes.
    In folder Vendor/Module/Setup/Patch/Data add a .php file (eg: AddCustomerAttributes)

    The following will add a few customer attributes.
    After adding this bin/magento setup:upgrade command is required.

    There will be an entry added to patch_list datatable, if the script file was executed correctly and also the attributes in the eav attribute table of course.

    <?php
    namespace VendorModuleSetupPatchData;
    
    use MagentoCustomerModelCustomer;
    use MagentoCustomerSetupCustomerSetupFactory;
    use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoFrameworkSetupPatchDataPatchInterface;
    
    class AddCustomerAttributes implements DataPatchInterface
    {
        /**
         * @var ModuleDataSetupInterface
         */
        protected $moduleDataSetup;
    
        /**
         * @var CustomerSetupFactory
         */
        protected $customerSetupFactory;
    
        /**
         * @var AttributeSetFactory
         */
        protected $attributeSetFactory;
    
        /**
         * AddCustomerPhoneNumberAttribute constructor.
         * @param ModuleDataSetupInterface $moduleDataSetup
         * @param CustomerSetupFactory $customerSetupFactory
         * @param AttributeSetFactory $attributeSetFactory
         */
        public function __construct(
            ModuleDataSetupInterface $moduleDataSetup,
            CustomerSetupFactory $customerSetupFactory,
            AttributeSetFactory $attributeSetFactory
        ) {
            $this->moduleDataSetup = $moduleDataSetup;
            $this->customerSetupFactory = $customerSetupFactory;
            $this->attributeSetFactory = $attributeSetFactory;
        }
    
        /**
         * {@inheritdoc}
         */
        public function apply()
        {
            $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
    
            $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
            $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    
            $attributeSet = $this->attributeSetFactory->create();
            $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    
            /** attribute_1 */
            $customerSetup->addAttribute(
                Customer::ENTITY,
                'attribute_1',
                [
                    'type' => 'text',
                    'label' => 'Attribute One',
                    'input' => 'text',
                    'required' => false,
                    'user_defined' => true,
                    'sort_order' => 1000,
                    'position' => 1000,
                    'default' => 0,
                    'system' => 0
                ]
            );
    
            $attribute = $customerSetup->getEavConfig()->getAttribute(
                Customer::ENTITY,
                'attribute_1'
            );
    
            $attribute->addData(
                [
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer']
                ]
            );
    
            $attribute->save();
    
            /** attribute_2 */
            $customerSetup->addAttribute(
                Customer::ENTITY,
                'attribute_2',
                [
                    'type' => 'int',
                    'label' => 'Attribute Two',
                    'input' => 'select',
                    'source' => 'VendorModuleModelOptions',
                    'required' => false,
                    'user_defined' => true,
                    'sort_order' => 1000,
                    'position' => 1000,
                    'default' => 0,
                    'system' => 0
                ]
            );
    
            $attribute = $customerSetup->getEavConfig()->getAttribute(
                Customer::ENTITY,
                'attribute_2'
            );
    
            $attribute->addData(
                [
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer']
                ]
            );
    
            $attribute->save();
        }
    
        /**
         * {@inheritdoc}
         */
        public static function getDependencies()
        {
            return [];
        }
    
        /**
         * {@inheritdoc}
         */
        public function getAliases()
        {
            return [];
        }
    }
    

    Please let me know if you need help with anything on this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search