7

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

asked Jun 15, 2018 at 7:10
3
  • show your script which you have used for create an attribute. Commented Jun 15, 2018 at 7:39
  • @SureshChikani, plz check updated question.. Commented Jun 15, 2018 at 10:22
  • @SarfarajSipai You can refer the link, rakeshjesadiya.com/… Commented Aug 6, 2020 at 4:04

2 Answers 2

22

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);
answered Jun 18, 2018 at 11:44
1
  • Thanks man, it took me sometime to figure this one out. Thanks for sharing! Commented Feb 22, 2019 at 22:27
0

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

answered Jun 15, 2018 at 10:10
2
  • 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. Commented Jun 15, 2018 at 10:18
  • please check updated question..! Commented Jun 15, 2018 at 10:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.