I have created two customer attribute one "abc" and second is "xyz" this two attribute display properly in customer admin. also attributes are created in database table "eav_attribute". But while save new customer with this field data are not save this into database. And also can not able to get this custom attribute value in frontend header side. plz check file.
app/code/Test/CustomerAttribute/Setup/InstallData.php
<?php
namespace Test\CustomerAttribute\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface {
private $customerSetupFactory;
public function __construct (
\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup'=> $setup]);
$setup->startSetup();
$customerSetup->addAttribute('customer', 'account_id', [
'label' => 'Account Id',
'type' => 'varchar',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 105,
"unique" => false,
'user_defined' => true,
'system' => 0,
'visible_on_front' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
]);
$loyaltyAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'account_id');
$loyaltyAttribute->setData('used_in_forms',['adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address','adminhtml_checkout']);
$loyaltyAttribute->save();
$customerSetup->addAttribute('customer', 'amount_spend', [
'label' => 'Amount Spend',
'type' => 'varchar',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 106,
"unique" => false,
'user_defined' => true,
'system' => 0,
'visible_on_front' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
]);
$amount_spend = $customerSetup->getEavConfig()->getAttribute('customer', 'amount_spend');
$amount_spend->setData('used_in_forms',['adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address','adminhtml_checkout']);
$amount_spend->save();
$setup->endSetup();
}
}
If you have any idea plz share it
thanks
-
show your script which you have used for create an attribute.Suresh Chikani– Suresh Chikani2018年06月15日 07:39:34 +00:00Commented Jun 15, 2018 at 7:39
-
@SureshChikani, plz check updated question..Sarfaraj Sipai– Sarfaraj Sipai2018年06月15日 10:22:25 +00:00Commented Jun 15, 2018 at 10:22
-
@SarfarajSipai You can refer the link, rakeshjesadiya.com/…Rakesh Jesadiya– Rakesh Jesadiya2020年08月06日 04:04:20 +00:00Commented Aug 6, 2020 at 4:04
2 Answers 2
looks like the issue was due to the fact your attributes are not assigned to the customer attribute set.
the following code is needed in your install: I added full code for you at https://bitbucket.org/magstaging/newcustomerattribute
$eavSetup->addAttributeToSet(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
null,
$attributeCode);
-
Thanks man, it took me sometime to figure this one out. Thanks for sharing!George Donev– George Donev2019年02月22日 22:27:26 +00:00Commented Feb 22, 2019 at 22:27
When adding new attribute in the database, you have to flush cache storage before data get persistently saved.
--> although you attributes are created, the cache is what triggers the data to be saved afterwards.
--> if data is still not saved, I'd look at php error log first
-
i have flushed all cache. attributes are display in magento database like "eav_attribute" table and after save data in "customer_entity_varchar" table data not save here.Sarfaraj Sipai– Sarfaraj Sipai2018年06月15日 10:18:37 +00:00Commented Jun 15, 2018 at 10:18
-
please check updated question..!Sarfaraj Sipai– Sarfaraj Sipai2018年06月15日 10:23:22 +00:00Commented Jun 15, 2018 at 10:23
Explore related questions
See similar questions with these tags.