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.

Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155
Naim Malek
  • 133
  • 1
  • 5
  • Refer this link to fulfill your requirement : http://magento.stackexchange.com/questions/88245/magento2-create-a-customer-custom-attribute –  Jul 22 '16 at 09:53
  • How to add more than one attributes? – Jarnail S Apr 07 '17 at 11:44

3 Answers3

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.

Rushvi
  • 2,843
  • 2
  • 14
  • 31
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.

Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155
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

Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
Nagaraju Kasa
  • 5,856
  • 6
  • 53
  • 113