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
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();
}
}
InstallScript
by which you have created customer attribute.