I have created a customer custom attribute with the above code:
namespace VendorCustomerSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
class InstallData implements InstallDataInterface
{
/**
* Customer setup factory
*
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
private $setup;
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
)
{
$this->setup = $setup;
$this->setup->startSetup();
$this->addCustomAttribute(
'customer_brand',
[
'label' => 'Marca',
'type' => 'varchar',
'input' => 'multiselect',
'source' => VendorCustomerModelAdminhtmlAttributeSourceBrand::class,
'position' => 1001,
'visible' => true,
'required' => false,
'system' => 0,
'default' => '0',
'user_defined' => true,
'adminhtml_only' => 1,
'validate_rules' => '[]'
]
);
}
private function addCustomAttribute($attrCode, $attrParams) {
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->setup]);
$customerSetup->removeAttribute(MagentoCustomerModelCustomer::ENTITY, $attrCode);
$customerSetup->addAttribute(
'customer',
$attrCode,
$attrParams
);
$eavConfig = $customerSetup->getEavConfig()->getAttribute('customer', $attrCode);
$eavConfig->setData('used_in_forms', ['adminhtml_customer']);
$eavConfig->save();
$this->setup->endSetup();
return $this->setup;
}
}
And with this code the customer admin form now shows my custom attribute:
But now i want to retrieve this attribute in a view with:
$customer->getCustomAttribute('customer_brand');
But it’s aways returning null.
When i debug the code i cannot see the custom attribute in the customer repository:
How can i get this custom attribute?