1

I am new to Magento 2. We have created a custom module in Magento 2. We have successfully created a custom attribute in that module and created installdata.php file in the custom module.

My problem is that I want add or edit a custom attribute. I have created upgradedata file but custom attribute is not updated.

installData.php

                    <?php
        /**
         * @author      Sashas
         * @category    Sashas
         * @package     Sashas_CustomerAttribute
         * @copyright   Copyright (c) 2015 Sashas IT Support Inc. (http://www.extensions.sashas.org) 
         */
        namespace test\CustomerAttribute\Setup;
        use Magento\Customer\Setup\CustomerSetupFactory;
        use Magento\Customer\Model\Customer;
        use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
        use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
        use Magento\Framework\Setup\InstallDataInterface;
        use Magento\Framework\Setup\ModuleContextInterface;
        use Magento\Framework\Setup\ModuleDataSetupInterface;

        /**
         * @codeCoverageIgnore
         */
        class InstallData implements InstallDataInterface
        {

            /**
             * @var CustomerSetupFactory
             */
            protected $customerSetupFactory;

            /**
             * @var AttributeSetFactory
             */
            private $attributeSetFactory;

            /**
             * @param CustomerSetupFactory $customerSetupFactory
             * @param AttributeSetFactory $attributeSetFactory
             */
            public function __construct(
                CustomerSetupFactory $customerSetupFactory,
                AttributeSetFactory $attributeSetFactory
            ) {
                $this->customerSetupFactory = $customerSetupFactory;
                $this->attributeSetFactory = $attributeSetFactory;
            }


            /**
             * {@inheritdoc}
             */
            public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
            {

                /** @var CustomerSetup $customerSetup */
                $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

                $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
                $attributeSetId = $customerEntity->getDefaultAttributeSetId();

                /** @var $attributeSet AttributeSet */
                $attributeSet = $this->attributeSetFactory->create();
                $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

                //Vendor number
                $customerSetup->addAttribute(Customer::ENTITY, 'vendor_number', [
                    'type' => 'varchar',
                    'label' => 'Vendor Number',
                    'input' => 'text',
                    'required' => false,
                    'visible' => true,
                    'user_defined' => true,
                    'sort_order' => 1000,
                    'position' => 1000,
                    'system' => 0,
                ]);

                $attribute_vendor_number = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'vendor_number')
                ->addData([
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer'],
                ]);
                        $attribute_vendor_number->save();


                //FTP Username
                $customerSetup->addAttribute(Customer::ENTITY, 'ftp_username', [
                    'type' => 'varchar',
                    'label' => 'FTP Username',
                    'input' => 'text',
                    'required' => false,
                    'visible' => true,
                    'user_defined' => true,
                    'sort_order' => 1000,
                    'position' => 1000,
                    'system' => 0,
                ]);

                $attribute_ftp_username = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'ftp_username')
                ->addData([
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer'],
                ]);
                        $attribute_ftp_username->save();  

            }
        }

UpgradeData.php - I want to add following attribute in customer module.

                <?php
                namespace Kraus\Attribute\Setup;

                use Magento\Eav\Setup\EavSetup;
                use Magento\Eav\Setup\EavSetupFactory;
                use Magento\Framework\Setup\ModuleContextInterface;
                use Magento\Framework\Setup\ModuleDataSetupInterface;
                use Magento\Framework\Setup\UpgradeDataInterface;
                use Magento\Eav\Model\Config;

                class UpgradeData implements UpgradeDataInterface
                {
                    private $eavSetupFactory;

                    public function __construct(EavSetupFactory $eavSetupFactory)
                    {
                        $this->eavSetupFactory = $eavSetupFactory;
                        $this->eavConfig = $eavConfig;
                    }

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

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

                            $setup->startSetup();


                        //    if (version_compare($context->getVersion(), '2.0.0') < 0) {
                            /** @var CustomerSetup $customerSetup */
                                                     $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
                                $eavSetup->addAttribute(
                                        \Magento\Customer\Model\Customer::ENTITY,
                                        'sample_attribute',
                                        [
                                                'type' => 'int',
                                                'label' => 'Sample Attribute',
                                                'input' => 'select',
                                                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                                                'required' => true,
                                                'default' => '0',
                                                'sort_order' => 100,
                                                'system' => false,
                                                'position' => 100
                                        ]
                                );
                                $sampleAttribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'sample_attribute');
                                $sampleAttribute->setData(
                                        'used_in_forms',
                                        ['adminhtml_customer_address',]
                                );
                                $sampleAttribute->save();

                     //   }  
                        }

                        $setup->endSetup();
                    }
                }

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.