Question

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

Was it helpful?

Solution

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);

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top