3

Anyone Can help me, I want to add customer Attribute and i have put my code as below.I have installed it, not getting any error but noting happened.

Customer/Attrib/Setup/InstallData.php

<?php namespace Customer\Attrib\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; } 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, 'my_measurements', [ 'type' => 'varchar', 'label' => 'Mymeasurements', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'sort_order' => 1000, 'position' => 1000, 'system' => 0, ]); $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'my_measurements') ->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => ['customer_address_edit'], ]); $attribute->save(); } } 

Customer/Attrib/registration.php

<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Customer_Attrib', __DIR__ ); 

Customer/Attrib/etc/module.xml

<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Customer_Attrib" setup_version="1.0.0"> <sequence> <module name="Customer_Attrib"/> </sequence> </module> </config> 

Thanks in Advance.

2
  • Refer this link to fulfill your requirement : magento.stackexchange.com/questions/88245/… Commented Jul 22, 2016 at 9:53
  • How to add more than one attributes? Commented Apr 7, 2017 at 11:44

3 Answers 3

7

Please make below change in module.xml,

<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Customer_Attrib" setup_version="1.0.0"> <sequence> <!--<module name="Customer_Attrib"/>--> <module name="Magento_Customer"/> </sequence> </module> </config> 

InstallData.php

<?php namespace Customer\Attrib\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; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); /** @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, 'my_measurements', [ 'type' => 'varchar', 'label' => 'Mymeasurements', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'sort_order' => 1000, 'position' => 1000, 'system' => 0, ]); $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'my_measurements') ->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => ['customer_address_edit',customer_form], ]); $attribute->save(); $setup->endSetup(); } } 

Now run commands,

php bin/magento setup:upgrade rm -rf var/view_preprocessed rm -rf pub/static/frontend/ rm -rf var/cache/rm -rf var/page_cache/ rm -rf var/generation/ php bin/magento setup:static-content:deploy php bin/magento cache:flush php bin/magento cache:clean 

Now Login to amdin your attribute will be displayed in customer section as screen shot.

enter image description here

[UPDATE]
If still you don't get it, please make sure to add below code to app/code/<vendor>/<module>/view/base/ui_component/customer_form.xml

<?xml version="1.0" encoding="UTF-8"?> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <fieldset name="customer"> <field name="my_measurements" formElement="input"> <settings> <visible>true</visible> </settings> </field> </fieldset> </form> 

After this run commands as above.

5
  • It's working fine. Thank you for your help @RJ07. It's display on admin side but i want that on customer side in profile edit mode. Commented Jul 25, 2016 at 7:39
  • how would we add multiple attributes to a user instead of just one? Can we repeat the code from addAttribute(Customer::ENTITY,... to $attribute->save(); ? Commented Dec 15, 2016 at 17:16
  • How to add more than one attributes? Commented Apr 7, 2017 at 11:41
  • But it is not rendering on checkout page. Commented Jun 2, 2017 at 8:37
  • it's working fine but it's value not save while save data and also this attribute not working in frontend while get it's value like $block->getMymeasurements(); Commented Jun 15, 2018 at 7:06
0

Your module xml should be:

Customer/Attrib/etc/module.xml

<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Customer_Attrib" setup_version="1.0.0"> <sequence> <!--<module name="Customer_Attrib"/>--> <module name="Magento_Customer"/> </sequence> </module> </config> 

Customer/Attrib/Setup/InstallData.php

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); //Your add attribute code here $setup->endSetup(); } 

Remember to run setup upgrade command.

4
  • Nope Still not working Commented Jul 22, 2016 at 9:54
  • Try to remove your module in setup_module table. Try to run setup upgrade command again. Commented Jul 22, 2016 at 10:00
  • I can't see my module in setup_module table Commented Jul 22, 2016 at 10:06
  • Try to clear your Magento Cache and run setup upgrade command again. Commented Jul 22, 2016 at 13:35
0

We do have an option to create a customer attribute/s with the magento2 silk software tool which is very easy. please follow the steps below might be useful.

Please find the screenshot:

Step1: Create a module with the selection of customer attribute from using the module creator tool.

Magento 2 silk software tool

Step 2: Download the zip and install it in your magento2 using module installed steps

Step3: Clear flush cache and page cache and login into your admin check it.

Output:

admin end customers section

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.