I have written the install script to insert the attribute on admin form. But when i click save it does not save the value. Below is the screen short. Prefer feel is the attribute.
-
have you added custom attribute to database?– Vish VaghelaCommented Mar 30, 2018 at 11:18
-
You mean EAV which i wrote in installData.php file?– Avesh NaikCommented Mar 30, 2018 at 11:33
-
your attribute present in account section? And you are able to save from admin ?– Shashank GuptaCommented Mar 30, 2018 at 12:12
-
No it's just displaying the field but not able to save it. It displays in form and not save.– Avesh NaikCommented Mar 30, 2018 at 12:21
Add a comment
|
2 Answers
To add an attribute to customer EAV table, we have to use 'used_in_form' proper EAV implementation of customer attribute. Please check InstallData.php code where I have created customer attribute to insert company name.
<?php
namespace [Package_Name]\[Module_Name]\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;
}
/**
* {@inheritdoc}
*/
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, 'customer_company', [
'type' => 'varchar',
'label' => 'Company',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_company')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer','customer_account_create','customer_account_edit'],
]);
$attribute->save();
}
}
-
-
This should work, which version of magento you are using? Commented Mar 30, 2018 at 12:13
-
-
This should work on version 2.2.2 as well. Now there is only one way I can check and i.e. to check your code. Is there a way you can show me your code? Commented Mar 30, 2018 at 12:23
-
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'prefer_feel', [
'type' => 'varchar',
'label' => 'Prefer Feel',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
'backend' => '',
'source' => '',
'default' => '',
'frontend' => '',
'unique' => false,
'note' => '',
]);
$prefer_feel = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "prefer_feel");
$child1_firstname = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'prefer_feel');
$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";
$child1_firstname->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", 100);
$child1_firstname->save();
$installer->endSetup();
$attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'prefer_feel')
->addData(['used_in_forms' => [
'adminhtml_customer',
'adminhtml_checkout',
'customer_account_create',
'customer_account_edit'
]]);
$attribute->save();
}