Magento – Adding form field values into customer_address_entity_varchar Magento 1.7

attributescustomer-addresscustomer-attributemagento-1.7magento-1.8

I have added custom fields into the default registration page since it does not offer a user to create an account and add Company, Phone number, Website, etc. from the very first page. I have been following the guide here I have it adding the info into the database but I would like to add some of the info into the "customer_address_entity_varchar" table like the Phone, Company, Fax and the Address fields. I believe I would set this in AccountController.php since I am not creating a module.

This is the current code that the tutorial is telling me to put in I modified the parameter name to Company instead of Occupation.

 if($this->getRequest()->getParam('company')) {
     $customer->setCompany($this->getRequest()->getParam('company'));
 }

It will not add the field inside of the "customer_address_entity_varchar" it will add it to "customer_entity_varchar". I am sure it is just a simple change.

For clarification in Magento’s default installation when you register a new account it will ask you for “First and last name, Email, Password” After you login to your account it will let you add the default address for billing and shipping along with a phone number and other linformation. The Fax, Phone, City, State, Zip, Country are added to the “customer_address_entity_varchar” table after you add these under “My Account” I would like values of the custom fields I have added following the below tutorial to be added to the “customer_address_entity_varchar” table during registration. How do I do this?

Best Answer

As part of your install script if you add something like the following this should add the entires to the customer address tables.

$installer->addAttribute('customer_address', 'YOUR_ATTRIBUTE_CODE', array(
    'type'     => 'varchar',
    'input'    => 'text',
    'label'    => 'Your Attribute',
));

Here the 'customer_address' entity type is used and in combination the type 'varchar' this will make the attribute use the table customer_address_entity_varchar.

If you want to use these attributes on some forms in Magento you will then also need to set them as used on the forms via the install script.

$addressDefaultUsedInForms = array(
    'adminhtml_customer_address',
    'customer_register_address',
    'customer_address_edit'
);
// Add each attribute to the default forms
$installer->addAttributesToForm(array('YOUR_ATTRIBUTE_CODE', $addressDefaultUsedInForms, 'customer_address');

Once you have the address attributes created than you would need to use the following code to set and show the attributes in the template.

$customer->getAddress()->getYOURATTRIBUTECODE();
$customer->getAddress()->setYOURATTRIBUTECODE('value');