This happens because those attributes you mentioned have is_visible
set to 0
in the customer_eav_attribute
table. They are saved with this value when they are created by the setup.
Before saving the customer data, in the controller (Mage_Customer_AccountController
) the _getCustomerErrors
function is called:
protected function _getCustomerErrors($customer)
{
$errors = array();
$request = $this->getRequest();
if ($request->getPost('create_address')) {
$errors = $this->_getErrorsOnCustomerAddress($customer);
}
$customerForm = $this->_getCustomerForm($customer);
$customerData = $customerForm->extractData($request);
$customerErrors = $customerForm->validateData($customerData);
if ($customerErrors !== true) {
$errors = array_merge($customerErrors, $errors);
} else {
$customerForm->compactData($customerData);
$customer->setPassword($request->getPost('password'));
$customer->setConfirmation($request->getPost('confirmation'));
$customerErrors = $customer->validate();
if (is_array($customerErrors)) {
$errors = array_merge($customerErrors, $errors);
}
}
return $errors;
}
Not all the data you submit from the form gets set on the customer model in order to be saved. The data is first extracted in Mage_Customer_Model_Form
class, which extends Mage_Eav_Model_Form
.
The first condition for the attribute data to be set on the customer model is that the specific attribute should be associated to submited form (you can check these in the customer_form_attribute
table by filtering by form_code
customer_account_create). This condition should be met for the attributes you mentioned since they are added to this form when they are created by the setup.
The second condition for the attribute data to be set on the customer model is to pass the validation in the Mage_Eav_Model_Form::extractData
function:
public function extractData(Zend_Controller_Request_Http $request, $scope = null, $scopeOnly = true)
{
$data = array();
foreach ($this->getAttributes() as $attribute) {
if ($this->_isAttributeOmitted($attribute)) {
continue;
}
$dataModel = $this->_getAttributeDataModel($attribute);
$dataModel->setRequestScope($scope);
$dataModel->setRequestScopeOnly($scopeOnly);
$data[$attribute->getAttributeCode()] = $dataModel->extractValue($request);
}
return $data;
}
The _isAttributeOmitted
function will check the is_visible
value:
protected function _isAttributeOmitted($attribute)
{
if ($this->_ignoreInvisible && !$attribute->getIsVisible()) {
return true;
}
return false;
}
You can check the is_visible
values for those attribute by running this query on your database:
SELECT * FROM customer_eav_attribute c WHERE attribute_id IN (
SELECT attribute_id FROM eav_attribute
WHERE attribute_code IN ('taxvat', 'middlename', 'prefix', 'suffix') AND entity_type_id=1
);
If the value for is_visible
is 0
then you can change it to 1
.
If you are using Magento Enterprise then you can edit the customer attributes in Customers -> Attributes -> Manage Customer Attributes. If you set Show on Frontend to Yes then the customer attribute values should be saved correctly into the database.
For creating attribute create InstallData.php in you custom module
and upgrade you module
namespace Compony\Custom\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
class InstallData implements InstallDataInterface
{
private $customerSetupFactory;
/**
* Constructor
*
* @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* {@inheritdoc}
*/
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_field', [
'type' => 'varchar',
'label' => 'Custom Field',
'input' => 'text',
'source' => '',
'required' => false,
'visible' => true,
'position' => 333,
'system' => false,
'backend' => ''
]);
$attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'custom_field')
->addData(['used_in_forms' => [
'adminhtml_customer',
'adminhtml_checkout',
'customer_account_create',
'customer_account_edit'
]
]);
$attribute->save();
}
}
For Adding field in Dashboard edit page
Using Object in phtml :
../vendor/magento/module-customer/view/frontend/templates/form/edit.phtml
<?php $objm = \Magento\Framework\App\ObjectManager::getInstance(); ?>
<?php $customerSession = $objm->get('Magento\Customer\Model\Session'); ?>
<input type="text" class="input-text" value="<?php echo $customerSession->getCustomer()->getCustomField();?>" name="custom_field" id="custom_field" data-input="custom_field" />
You can also do by overriding Magento\Customer\Block\Form\Edit if you don't want to use Object in phtml file
Using Overriding Block :
../Compony/Custom/Block/Magento/Customer/Form/Edit.php
<?php
namespace Compony\Custom\Block\Magento\Customer\Form;
class Edit extends \Magento\Customer\Block\Form\Edit
{
public function getCustomField()
{
return $this->customerSession->getCustomer()->getCustomField();
}
}
../Compony/Custom/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Block\Form\Edit" type="Compony\Custom\Block\Magento\Customer\Form\Edit" />
</config>
../vendor/magento/module-customer/view/frontend/templates/form/edit.phtml
add below code where you want your field
<input type="text" class="input-text" value="<?php echo $block->getCustomField();?>" name="custom_field" id="custom_field" data-input="custom_field" />
Best Answer
You can add your attributes through a layout update. There is a call for a block inside the customer registration template:
You can use this to add your block:
Your content will be visible under the password field and above the register button.
If the position in the template doesn't fit your needs, then there are only ugly options like: