I've been searching around the forums recently and have tried many different solutions that either don't work at all or only partially work.
So the issue i am having is I need 3 new fields in the customer registration page. I have added the 3 fields on the front-end but only the "customer number" one actually works as expected & shows up in the admin panel
Customer registration page:
Admin backend:
So I got the customer number field working as i want it (shows up as 12345 at the top of the admin backend picture), but I can't get the company name or account type fields to show up in the backend. Also, the customer number field is duplicated for some reason.
Here are the files i have in my module:
Setup/InstallData.php
namespace sahib\test\Setup;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface {
/**
* Customer setup factory
*
* @var \Magento\Customer\Setup\CustomerSetupFactory
*/
private $customerSetupFactory;
public function __construct(CustomerSetupFactory $customerSetupFactory) {
$this->customerSetupFactory = $customerSetupFactory;
}
public function install(ModuleDataSetupInterface $setup,
ModuleContextInterface $context) {
$setup->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(Customer::ENTITY, 'companyname', [
'label' => 'Company Name',
'input' => 'text',
'required' => false,
'sort_order' => 40,
'visible' => true,
'system' => true,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true]
);
// add attribute to form
/** @var $attribute */
$attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'companyname');
$attribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create', ]);
$attribute->save();
$customerSetup->addAttribute(Customer::ENTITY, 'customernumber', [
'label' => 'Customer Number',
'input' => 'text',
'required' => false,
'sort_order' => 40,
'visible' => true,
'system' => false,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true]
);
$attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'customernumber');
$attribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create']);
$attribute->save();
$customerSetup->addAttribute(Customer::ENTITY, 'accounttype', [
'type' => 'text',
'label' => 'Account Type',
'input' => 'select',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 101,
'position' => 101,
'system' => 0,
'option' =>
array (
'values' =>
array (
0 => 'Retail',
1 => 'Cash Account',
2 => 'Credit Account',
),
),
]); $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'accounttype');
$attribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create']);
$attribute->save();
$setup->endSetup();
}
}
view/frontend/layout/customer_account_create.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Framework\View\Element\Template"
name="additional_info_customer_companyname"
template="sahib_test::additionalinfocustomer.phtml"/>
</referenceContainer>
view/frontend/templates/additionalinfocustomer.phtml
<fieldset class="fieldset create account" data-hasrequired="<?php /*
@escapeNotVerified */ echo __('* Required Fields') ?>">
<legend class="legend"><span><?php /* @escapeNotVerified */ echo
__('Additional Information') ?></span></legend>
<p>
<div class="field regulation">
<label for="regulation" class="label"><span><?php /* @escapeNotVerified
*/
echo __('Company Name') ?></span></label>
<div class="control">
<input type="text" name="companyname" id="companyname" title="<?php
/* @escapeNotVerified */ echo __('Company Name') ?>" class="input-text"
data-validate="{required:false}">
</div>
</div>
<div class="field regulation">
<label for="regulation" class="label"><span><?php /* @escapeNotVerified
*/
echo __('Customer Number') ?></span></label>
<div class="control">
<input type="text" name="customernumber" id="customernumber" title="
<?php /* @escapeNotVerified */ echo __('Customer Number') ?>" class="input-
text"
data-validate="{required:false}">
</div>
</div>
<div class="field regulation">
<label for="regulation" class="label"><span><?php /* @escapeNotVerified
*/
echo __('Account Type') ?></span></label>
<div class="control">
<input type="dropdown" name="accounttype" id="accounttype" title="<?
php /* @escapeNotVerified */ echo __('Account Type') ?>" class="input-
select" data-validate="{required:false}">
</div>
</div>
</p>
</fieldset>
Thanks!