0

Hello Everyone, I have created a custom attributes for a customers it's working fine in admin also save data and get data while attributes have any certain value in textbox But if attribute has not value save in textfield it will not aslo dispaly attribute name while print_r() data. so i want to get this attribute while custom attribute value have or have not save in database.

please check image for more details.

thanks

enter image description here

InstallData.php

  use Magento\Customer\Api\CustomerMetadataInterface;

 use Magento\Eav\Setup\EavSetupFactory;

 use Magento\Framework\Setup\InstallDataInterface;

 use Magento\Framework\Setup\ModuleContextInterface;

 use Magento\Framework\Setup\ModuleDataSetupInterface;

 class InstallData implements InstallDataInterface

 {

/**
 * @var EavSetupFactory
 */

private $eavSetupFactory;

/**
 * @var \Magento\Eav\Model\Config
 */

private $eavConfig;

public function __construct(
    EavSetupFactory $eavSetupFactory,
    \Magento\Eav\Model\Config $eavConfig
) {
    $this->eavSetupFactory = $eavSetupFactory;
    $this->eavConfig = $eavConfig;
}

/**
 * Installs data for a module
 *
 * @param ModuleDataSetupInterface $setup
 * @param ModuleContextInterface $context
 * @return void
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    /** @var \Magento\Eav\Setup\EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    $setup->startSetup();

    $attributeCode = 'account_id';
    $eavSetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'account_id', [
        'label' => 'Account Id',
        'required' => false,
        'user_defined' => 1,
        'system' => 0,
        'position' => 100,
        'default' => 0,
        'input' => 'text'
    ]);

    $eavSetup->addAttributeToSet(
        CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
        CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
        null,
        $attributeCode);

    $amountId = $this->eavConfig->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode);
    $amountId->setData('used_in_forms', [
        'adminhtml_customer',
        'customer_account_create',
        'customer_account_edit'
    ]);
    $amountId->getResource()->save($amountId);

    /***** Amount Spend **********/
    $attributeCode = 'amount_spend';
    $eavSetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode, [
        'label' => 'Amount Spend',
        'required' => false,
        'user_defined' => 1,
        'visible_on_front' => 1,
        'system' => 0,
        'default' => 0,
        'position' => 110
    ]);
    $eavSetup->addAttributeToSet(
        CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
        CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
        null,
        $attributeCode);

    $amountSpend = $this->eavConfig->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode);
    $amountSpend->setData('used_in_forms', [
        'adminhtml_customer',
        'customer_account_create',
        'customer_account_edit'
    ]);
    $amountSpend->getResource()->save($amountSpend);
    /******* Customer Type **********/
    $attributeCode = 'customer_type';
    $eavSetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode, [
        'label' => 'Customer Type',
        'required' => false,
        'user_defined' => 1,
        'visible_on_front' => 1,
        'system' => 0,          
        'position' => 120,
    ]);

    $eavSetup->addAttributeToSet(
        CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
        CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
        null,
        $attributeCode);

    $customer_type = $this->eavConfig->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode);
    $customer_type->setData('used_in_forms', [
        'adminhtml_customer',
        'customer_account_create',
        'customer_account_edit'
    ]);
    $customer_type->getResource()->save($customer_type);

    $attributeCode = 'tax_exempt_id';
        $eavSetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode, [
            'label' => 'Tax Exempt ID',
            'required' => false,                
            'user_defined' => 1,
            'system' => 0,
            'position' => 130,          
            'input' => 'text'
        ]);

        $eavSetup->addAttributeToSet(
            CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
            CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
            null,
            $attributeCode);

        $tax_exempt_id = $this->eavConfig->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode);
        $tax_exempt_id->setData('used_in_forms', [
            'adminhtml_customer',
            'customer_account_create',
            'customer_account_edit'
        ]);
        $tax_exempt_id->getResource()->save($tax_exempt_id);

        /****** Yes No Button *********/

        $attributeCode = 'rebate_allowed';
        $eavSetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode, [
            'label' => 'Rebate Allowed',
            'type' => 'text',
            'required' => false,                
            'input' => 'select',                
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'global' => '\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL',
            'user_defined' => 1,
            'system' => 0,
            'default' => '0',
            'position' => 140
        ]);

        $eavSetup->addAttributeToSet(
            CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
            CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
            null,
            $attributeCode);

        $rebate_allowed = $this->eavConfig->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode);
        $rebate_allowed->setData('used_in_forms', [
            'adminhtml_customer',
            'customer_account_create',
            'customer_account_edit'
        ]);
        $rebate_allowed->getResource()->save($rebate_allowed);

    $setup->endSetup();
}

}

8
  • Can you verify that value is saved in DB ?
    – anonymous
    Jun 28, 2018 at 5:15
  • no while first time created attribute has not any value so it's not display in array Jun 28, 2018 at 5:21
  • Please post your code for InstallScript by which you have created customer attribute.
    – anonymous
    Jun 28, 2018 at 5:24
  • Did it work for you ?
    – anonymous
    Jun 28, 2018 at 6:46
  • This file is work for me it's create attribute and save data into database while click on save on from admin. Jun 28, 2018 at 11:32

1 Answer 1

1

In order to get Customer Attribute You can try below code:

1). Load customer by Id.

$customer = $this->_customerRepositoryInterface->getById(1);

//Use Class  \Magento\Customer\Api\CustomerRepositoryInterface

2). Get Attribute

$customer->getCustomAttribute('your_custom_attribute');

// For File type Attribute

$customer->getCustomAttribute('your_custom_attribute')->getvalue();

UPDATE

File Name is incorrect It should be InstallData.php

I didn't check your code. But I am providing you sample code that will surely resolve your issue. Modify it as per you need.

namespace Vendor\Module\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
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;


class InstallData implements InstallDataInterface
{
protected $groupFactory;

protected $customerSetupFactory;

private $attributeSetFactory;

private $eavSetupFactory;




public function __construct(GroupFactory $groupFactory, EavSetupFactory $eavSetupFactory, CustomerSetupFactory $customerSetupFactory,
                            AttributeSetFactory $attributeSetFactory)
{

    $this->groupFactory = $groupFactory;
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
    $this->eavSetupFactory = $eavSetupFactory;
}

public function install(
    ModuleDataSetupInterface $setup,
    ModuleContextInterface $context
)
{


    $setup->startSetup();

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $setup->startSetup();
    $attributesInfo = [
        'customer_avatar' => [
            'label' => 'Customer Avatar',
            'type' => 'varchar',
            'input' => 'file',
            'position' => 1000,
            'visible' => true,
            'required' => false,
            'system' => 0,
            'user_defined' => true,
            'position' => 1000,
        ]
    ];
    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    foreach ($attributesInfo as $attributeCode => $attributeParams) {
        $customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
    }
    $magentoUsernameAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_avatar');
    $magentoUsernameAttribute->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => ['adminhtml_customer', 'customer_account_edit', 'adminhtml_checkout', 'adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'],
    ]);
    $magentoUsernameAttribute->save();

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $setup->startSetup();
    $attributesInfo = [
        'social_status' => [
            'label' => 'Social Status',
            'type' => 'varchar',
            'input' => 'text',
            'position' => 333,
            'visible' => true,
            'required' => false,
            'system' => 0,
            'user_defined' => true,
            'position' => 333,
        ]
    ];
    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    foreach ($attributesInfo as $attributeCode => $attributeParams) {
        $customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
    }
    $magentoUsernameAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'social_status');
    $magentoUsernameAttribute->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => ['adminhtml_customer', 'customer_account_edit', 'adminhtml_checkout', 'adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'],
    ]);
    $magentoUsernameAttribute->save();

    $setup->endSetup();
}

}

PS: Don't forget to remove your module's entry from setup_module table if you are modifying existing module which is already registred.

After remove entry from setup_module table execute

php bin/magento setup:upgrade from your root directory.

6
  • you are right using this code i will get custom attribute value but this "your_custom_attribute" is not display so i am not able to get value of that attribute. plz see image i have posted. Jun 28, 2018 at 5:31
  • As i said Please post you code for customer attribute so that i can check what's issue with this.
    – anonymous
    Jun 28, 2018 at 5:34
  • please check update question. Jun 28, 2018 at 5:50
  • only this file created to create customer custom attributes. Jun 28, 2018 at 5:51
  • Check I've posted InstallData.php sample code Which is working for me You can change it as per you need Let me know if you have any problems with this.
    – anonymous
    Jun 28, 2018 at 6:16

Your Answer

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

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