2

I have programatically created a customer attribute and set it to be displayed in adminhtml_customer. All is good, the attribute is being displayed only in the admin customer edit section as desired.

$attributeCode = 'attribute_code';

$eavSetup->addAttribute(Customer::ENTITY, $attributeCode,
    [
        'type'          => 'varchar',
        'label'         => 'Attribute Lable',
        'input'         => 'multiselect',
        'backend'       => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
        'global'        => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
        'source'        => \Magento\Eav\Model\Entity\Attribute\Source\Table::class,
        'visible'       => true,
        'user_defined'  => true,
        'system'        => false,
        'required'      => false,
        'unique'        => false,
        'position'      => 200
    ]
);

$attribute = $this->eavConfig->getAttribute(Customer::ENTITY, $attributeCode);

$attribute
    ->setData('used_in_forms', ['adminhtml_customer'])
    ->save();

However, when I try to edit the attribute in the M2 admin (Store/Attributes/Customer) - let's say I want to make it show in another area, the adminhtml_customer option is not available for selection in the Forms to Use In list (adminhtml_customer_address is not available either).

enter image description here

Looking through the code I found the following:

\Magento\CustomerCustomAttributes\Helper\Customer::getAttributeFormOptions

/**
 * Return available customer attribute form as select options
 *
 * @return array
 */
public function getAttributeFormOptions()
{
    return [
        ['label' => __('Customer Registration'), 'value' => 'customer_account_create'],
        ['label' => __('Customer Account Edit'), 'value' => 'customer_account_edit'],
        ['label' => __('Admin Checkout'), 'value' => 'adminhtml_checkout']
    ];
}

Since adminhtml_customer and adminhtml_customer_edit are M2 out of the box options you would expect to find those in the list, right?

Any thoughts?

Later edit: Customer Attributes are only available on M2 Enterprise Edition.

1
  • Just checked M2 Commerce 2.2.9 (Enterprise), the \Magento\CustomerCustomAttributes\Helper\Customer::getAttributeFormOptions method is the same. Also it looks like adding/removing adminhtml_customer to used_in_forms has no effect, the field is displayed anyway in the customer edit form in the backend. Also 'visible' (which is actually transformed into is_visible by the mapper) has no effect on the backend customer edit form, field is visible regardless. Jul 16 '19 at 7:39
0

If you want to show your attribute in adminhtml_customer_address

you have to create a customer address attribute. In this case your install data becomes like this

    $attributeCode = 'attribute_code';

$eavSetup->addAttribute('customer_address', $attributeCode,
    [
        'type'          => 'varchar',
        'label'         => 'Attribute Lable',
        'input'         => 'multiselect',
        'backend'       => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
        'global'        => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
        'source'        => \Magento\Eav\Model\Entity\Attribute\Source\Table::class,
        'visible'       => true,
        'user_defined'  => true,
        'system'        => false,
        'required'      => false,
        'unique'        => false,
        'position'      => 200
    ]
);

$attribute = $this->eavConfig->getAttribute('customer_address', $attributeCode);

$attribute
    ->setData('used_in_forms', ['adminhtml_customer_address'])
    ->save();

In this way you will see your attribute in adminhtml customer address form but not in adminhtml customer form, because customer and customer_address are separated entities.

5
  • the question is about adminhtml_customer
    – Radu
    Sep 24 '18 at 22:06
  • I explained why adminhtml_customer_address is not available Sep 24 '18 at 22:09
  • the question is also about why the two adminhtml options are not being displayed in the Forms to Use In list - thanks for your answer, but it is not on topic
    – Radu
    Sep 24 '18 at 22:25
  • They are not displayed because you created a customer attribute. Those options are displayed only for customer address attributes. Anyway if you think this is not the point maybe I have not understood what it is! :) Sep 25 '18 at 6:54
  • I have tried to create an address attribute. The only two options in the [Forms to Use In] when adding a Customer Address attribute are [Customer address registration] and [Customer Account Registration]
    – Radu
    Sep 25 '18 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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