1

I need to add a custom attribute (contact_number) for customer in Magento2 via an existing module, so I am using UpgradeData.php. Can anyone please tell what I have missed out.

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Customer\Model\Customer;

class UpgradeData implements UpgradeDataInterface {

private $customerSetupFactory;
private $attributeSetFactory;

public function __construct(
\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory
) {
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
}

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
    //if (version_compare($context->getVersion(), "1.0.1", "<")) 
    {
        $setup->startSetup();
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'contact_number', array(
            [
                'type' => 'varchar',
                'label' => 'Contact Number',
                'input' => 'text',
                'required' => true,
                'visible' => true,
                'user_defined' => true,
                'position' => 999,
                'system' => 0,
            ]
        ));

        $Attribute = $customerSetup->getEavConfig()->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'contact_number');
        $Attribute->setData(
        'used_in_forms',
            [
             'adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'
            ]
    );
        $Attribute->save();
        $setup->endSetup();
}
}

}

Not getting what I am doing wrong. Any help, please.

[edit] : Not getting the label in the backend as well: Please find the screen-shot attached.

enter image description here

5
  • have You use 'php bin/magento setup:upgrade' ? Jun 21, 2018 at 8:50
  • yes i did, but i think that their is some another issue as when in upgrade function for debug i did echo 'hi'; exit;. Their came no error in setup:upgrade Jun 21, 2018 at 8:58
  • I am a bit confused that what i should use installdata or upgradedata Jun 21, 2018 at 8:59
  • 1
    I got the point. Actually we use upgradedata when module is already installed but module version should also change but i was not changing the module version so the changes were not reflecting. Thanks by the way Jun 21, 2018 at 9:03
  • Please check my updated answer. Jun 21, 2018 at 9:31

3 Answers 3

5

Check if you have updated module version in module.xml and composer.json file before running php bin/magento setup:upgrade.

1
  • Can you please have a look at my updated question and help me in not getting labels in backend. Please ? Jun 21, 2018 at 9:53
5

Unless you haven't copy/pasted your whole file, I think you may be missing a namespace. Without it, the UpgradeData script will not run.

namespace YourVendor\YourModule\Setup; before your use statements.

1

You can create custom attribute using InstallData.php file on below path :

/app/code/CompanyName/ModuleName/Setup/ <?php /** * Add new attribute in Customer account section. */ namespace CompanyName\ModuleName\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
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;
use Magento\Framework\App\ResourceConnection;
use Magento\Eav\Model\Entity\Attribute\Source\Boolean;

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

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

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;
    /**
     * @var \Magento\Indexer\Model\IndexerFactory
     */
    protected $indexerFactory;
    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory,
        ResourceConnection $resource,
        \Magento\Framework\Indexer\IndexerInterfaceFactory $indexerFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
        $this->resource = $resource;
        $this->indexerFactory = $indexerFactory;
    }

    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);

        $customerSetup->addAttribute(Customer::ENTITY, 'is_allow', [
            'type' => 'int',
            'label' => 'Allow',
            'input' => 'select',
            'source' => Boolean::class,
            'required' => true,
            'default' => '1',
            'sort_order' => 300,
            'user_defined' => true,
            'system' => false,
            'position' => 400,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true,
            'is_filterable_in_grid' => true,
        ]);
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_allow')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer'], //you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
            ]);

        $attribute->save();
    }
}

Then, php bin/magento setup:upgrade execute this command and cache clean.

edit:

if changes not reflected then delete module from setup_module table and run upgrade command.

3
  • Hi, actually the script that I mentioned above in my question worked for me after changing module version. But dont know why I am not getting lables in backend .. I am getting only field box and that also on the top. Can you please help me in this Jun 21, 2018 at 9:49
  • 1
    It's better and proper way for add attribute at install time using InstallData. It's magento proper way. Jun 21, 2018 at 10:19
  • Set latest version in module.xml file and upgrade module. Jun 21, 2018 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.