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?

  • please confirm the magento version? and name of the attribute and is it a text field? – Nagaraju Kasa Jun 1 '17 at 8:45
  • magento 2.1.2 version am using. new custom attribute field anniversary_year (Anniversary Year) date field – jeni Jun 1 '17 at 8:49
  • Kindly anyone can help me, am frustrated by this changes. please help – jeni Jun 1 '17 at 9:28
  • hi @jeni follow the steps which i have posted and please ask u r doubt if any. thanks – Nagaraju Kasa Jun 1 '17 at 12:42
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();
    }
}
  • 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 '17 at 10:28
  • @jeni, I updated my code. It's work on my Magento 2.1.7, please try again. – Quan Le Jun 1 '17 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

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

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.