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

  • have You use 'php bin/magento setup:upgrade' ? – Konrad Siamro Jun 21 '18 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 – Narendra Vyas Jun 21 '18 at 8:58
  • I am a bit confused that what i should use installdata or upgradedata – Narendra Vyas Jun 21 '18 at 8:59
  • 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 – Narendra Vyas Jun 21 '18 at 9:03
  • Please check my updated answer. – Rohan Hapani Jun 21 '18 at 9:31
2

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

  • Can you please have a look at my updated question and help me in not getting labels in backend. Please ? – Narendra Vyas Jun 21 '18 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/

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 s:up execute this command and cache clean.

  • 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 – Narendra Vyas Jun 21 '18 at 9:49
  • 1
    It's better and proper way for add attribute at install time using InstallData. It's magento proper way. – Rohan Hapani Jun 21 '18 at 10:19
  • Set latest version in module.xml file and upgrade module. – Rohan Hapani Jun 21 '18 at 10:24

Your Answer

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

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