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

3

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);
1
  • Thanks man, it took me sometime to figure this one out. Thanks for sharing! 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.