3

I was trying to add new custom attribute in customer registration. Field's value do not save in database.

Please can anyone help me?

4
  • please confirm the magento version? and name of the attribute and is it a text field? Jun 1, 2017 at 8:45
  • magento 2.1.2 version am using. new custom attribute field anniversary_year (Anniversary Year) date field
    – jeni
    Jun 1, 2017 at 8:49
  • Kindly anyone can help me, am frustrated by this changes. please help
    – jeni
    Jun 1, 2017 at 9:28
  • hi @jeni follow the steps which i have posted and please ask u r doubt if any. thanks Jun 1, 2017 at 12:42

3 Answers 3

2

Customer Registration Custom attribute value create in admin and save

1. Text Field

2. Drop Down Field

3. Date Field

Using UpgradeSchema.php

<?php

namespace {CompanyName}\{ModuleName}\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
/* irrelevant */
#use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
/* irrelevant */
#use Magento\Framework\Setup\SchemaSetupInterface;
/* add this */
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements  UpgradeDataInterface
{
    private $customerSetupFactory;

    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.1', '<')) 
        {
            // For Text field
            $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    'attribute_title',                    
                    [
                        'type' => 'text',
                        'input' => 'text',
                        'label' => 'Attribute Title',
                        'required' => false,
                        'visible' => true,
                        'user_defined' => false,
                        'sort_order' => 1000,
                        'position' => 1000,
                        'system' => 0,
                    ]
                );
            $attribute_title = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'attribute_title')
                ->addData(
                    ['used_in_forms' => ['adminhtml_customer']
                ]);

            $attribute_title->save();

            //Add field Drop Down for Yes/No
            $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    'is_attribute',                    
                    [
                        'type' => 'int',
                        'input' => 'select',
                        'label' => 'Is Attribute',
                        'frontend' => '',
                        'default' => '1',
                        'class' => '',
                        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                        'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                        'required' => false,
                        'visible' => true,
                        'user_defined' => false,
                        'sort_order' => 1000,
                        'position' => 1000,
                        'system' => 0,
                    ]
                );
            $is_attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_attribute')
                ->addData(
                    ['used_in_forms' => ['adminhtml_customer']
                ]);

            $is_attribute->save();

            // For Date And Time field
            $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'custom_date',                    
                [
                    'label' => 'Custom Date',
                    'type' => 'datetime',
                    'input' => 'date',
                    'frontend' => 'Magento\Eav\Model\Entity\Attribute\Frontend\Datetime',
                    'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\Datetime',
                    'validate_rules' => '{"input_validation":"date"}',
                    'user_defined' => false,
                    'required' => false,
                    'visible' => true,
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'sort_order' => 1000,
                    'position' => 1000,
                    'system' => 0,
                ]
            );
            $custom_date = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_date')
                ->addData(
                    ['used_in_forms' => ['adminhtml_customer']
                ]);
                // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
            
            $custom_date->save();
        }
    }
}
0

You can try to add the new attribute to an attribute set and attribute group when install or upgrade data:

<?php

namespace Custom\Attribute\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Eav\Model\AttributeRepository;

class InstallData implements InstallDataInterface
{

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

    /**
     * @var GroupFactory
     */
    protected $groupFactory;

    /**
     * @var AttributeRepository
     */
    protected $attributeRepository;

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

    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeRepository $attributeRepository,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeRepository = $attributeRepository;
        $this->attributeSetFactory = $attributeSetFactory;
    }

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

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

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

        /** @var \Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(Customer::ENTITY, 'anniversary_year', [
            'label' => 'Anniversary Year',
            'input' => 'text',
            'type' => 'varchar',
            'required' => false,
            'visible' => true,
            'system' => false,
            'is_used_in_grid' => false,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false,
            'is_searchable_in_grid' => false
        ]);

        $newAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'anniversary_year')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['customer_account_create', 'checkout_register' , 'adminhtml_customer'],
            ]);
        $newAttribute->save();
        $this->attributeRepository->save($newAttribute);

        $setup->endSetup();
    }
}
2
  • Thanks Walish, I put your code in Customer module setup Install data but field is not created in customer entity table. and i upgrade setupn reindex done also. will it be working?
    – jeni
    Jun 1, 2017 at 10:28
  • @jeni, I updated my code. It's work on my Magento 2.1.7, please try again.
    – Quan Le
    Jun 1, 2017 at 11:59
0

Note: Here I am showing you how to create a customer custom attribute using simple steps.

Please follow the below steps using a tool called "Silk Software module creator" http://www.silksoftware.com/magento-module-creator/magento2x.php

Screen shot:

enter image description here

Step 1: Create a Custom module by the below screen shot

enter image description here

Step 2: Next select the 'Need to Add Customer Attribute' option and mention the details as shown in the below screen shot

enter image description here

Step 3: Now Download the module

Step 4: Follow the install module steps Clear flush cache and check the same in the admin end

Output:

enter image description here

2
  • Thank you so much but its not display in frontend . please help me
    – jeni
    Jun 1, 2017 at 13:07
  • you are welcome let me check and update you Jun 1, 2017 at 13:12

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.