Magento 2-将自定义字段添加到客户注册表单吗?

问题描述 投票:1回答:1

我需要在Magento 2的客户注册表单中添加一些自定义字段。

我需要添加的字段是:

公司注册号,业务类型(将是一个选择列表)和Sage帐号

我查看了一些资源,并尝试设置客户属性,但是运气不太好。

这是我到目前为止在InstallData.php中的代码:

public function __construct(CustomerSetupFactory $customerSetupFactory) {
    $this->customerSetupFactory = $customerSetupFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'company_reg_number',
        [
            'type'         => 'varchar',
            'label'        => 'Comapny Registration Number',
            'input'        => 'text',
            'required'     => true,
            'visible'      => true,
            'user_defined' => true,
            'position'     => 999,
            'system'       => 0,
        ]
    );

    $companyRegNo = $this->eavConfig->getAttribute(Customer::ENTITY, 'company_reg_number');
    // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
    $companyRegNo->setData(
        'used_in_forms',
        ['adminhtml_customer']
    );

    $companyRegNo->save();
}

我希望在此处的后端客户部分看到一个名为“公司注册号”的新字段:

客户帐户信息后端页面

enter image description here

不确定我是否缺少什么?

forms field magento2 registration custom-fields
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.