0

I have created a module with setup/installdata.php and layout & template files, but the custom attribute is showing up only on the frontside on customer account create form. Am i missing something?

 <?php namespace NameSpace\NameSpace\Setup; use Magento\Framework\Module\Setup\Migration; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; class InstallData implements InstallDataInterface { /** * Customer setup factory * * @var \Magento\Customer\Setup\CustomerSetupFactory */ private $customerSetupFactory; /** * Init * * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory */ public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory) { $this->customerSetupFactory = $customerSetupFactory; } /** * Installs DB schema for a module * * @param ModuleDataSetupInterface $setup * @param ModuleContextInterface $context * @return void */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY); $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "reg_code"); $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "reg_code", array( "type" => "varchar", "backend" => "", "label" => "Company Reg. code", "input" => "text", "source" => "", "visible" => true, "required" => false, "default" => "", "frontend" => "", "unique" => false, "note" => "" )); $reg_code = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "reg_code"); $reg_code = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'reg_code'); $used_in_forms[]="adminhtml_customer"; $used_in_forms[]="checkout_register"; $used_in_forms[]="customer_account_create"; $used_in_forms[]="customer_account_edit"; $used_in_forms[]="adminhtml_checkout"; $used_in_forms[]="customer_address"; $used_in_forms[]="customer_address_edit"; $reg_code->setData("used_in_forms", $used_in_forms) ->setData("is_used_for_customer_segment", true) ->setData("is_system", 0) ->setData("is_user_defined", 1) ->setData("is_visible", 1) ->setData("sort_order", 121); $reg_code->save(); $installer->endSetup(); } } 
2
  • could you add your installData.php file code and also where do you want to show that attribute ? ( on which area ) Commented Apr 16, 2018 at 5:11
  • if you created extension_attributes you cant find in database. Commented Apr 16, 2018 at 5:23

1 Answer 1

2

It doesn't show up in customer_entity table.

It will show up at Eav Attributetable in your DB.

If you can find your attribute at customer edit form at back end than you have done correct things no issue with it.

I don't know what's your mistake. Could you please try with below code. Its working for me.

<?php namespace Vendor\Module\Setup; use Magento\Framework\Module\Setup\Migration; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Customer\Model\GroupFactory; use Magento\Eav\Setup\EavSetupFactory; 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; class InstallData implements InstallDataInterface { protected $groupFactory; protected $customerSetupFactory; private $attributeSetFactory; private $eavSetupFactory; public function __construct(GroupFactory $groupFactory,EavSetupFactory $eavSetupFactory,CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory) { $this->groupFactory = $groupFactory; $this->customerSetupFactory = $customerSetupFactory; $this->attributeSetFactory = $attributeSetFactory; $this->eavSetupFactory = $eavSetupFactory; } public function install( ModuleDataSetupInterface $setup, ModuleContextInterface $context ) { $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); $setup->startSetup(); $attributesInfo = [ 'customer_avatar' => [ 'label' => 'Company Reg. code', 'type' => 'varchar', 'input' => 'text', 'position' => 1000, 'visible' => true, 'required' => false, 'system' => 0, 'user_defined' => true, 'position' => 1000, ] ]; $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer'); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); /** @var $attributeSet AttributeSet */ $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); foreach ($attributesInfo as $attributeCode => $attributeParams) { $customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams); } $magentoUsernameAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'company_reg_code'); $magentoUsernameAttribute->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => ['adminhtml_customer','customer_account_edit','adminhtml_checkout','adminhtml_customer_address','customer_address_edit','customer_register_address'], ]); $magentoUsernameAttribute->save(); $setup->endSetup(); } } ?> 

Note : After update code remember that you need to remove your module entry from setup_module table than run below commands from your Magento root.

setup:upgrade setup:static-content:deploy (If you are on Magento 2.2.x than you need to add -f at end of command) 

Hope it will resolve your issue.!!

8
  • its not showing up even in eav_attribute table and customer edit form at back end. its showing only in the frontend. Commented Apr 16, 2018 at 5:22
  • @kapsid, Please update your question with code, So that i can guide you. Commented Apr 16, 2018 at 5:28
  • i have added the code Commented Apr 16, 2018 at 5:56
  • @kapsid, Check my updated code. Commented Apr 16, 2018 at 6:13
  • @kapsid, if it works for you than give up vote to help community. !! Commented Apr 16, 2018 at 6:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.