How can I rearrange form fields for customer address attributes in the admin?

I am developing a Magento extension that adds several custom attributes to the customer address. My goal is to rearrange the address input fields in the admin order form. When I change the sort order of the attributes, the arrangement changes, but only for the custom attributes. I.e. I cannot place input field for custom attribute before input field for a system attribute.

I've read the source code in Mage_Adminhtml_Block_Sales_Order_Create_Form_Address and Mage_Customer_Model_Form but still cannot figure out how to do it.

Is this possible at all? If so, how do I achieve it?

Edit In my mysql4-install-0.1.0.php script, I use:

$this->addAttribute('customer_address', 'carrier_office_name', array( 'type' => 'int', 'input' => 'text', 'label' => 'Carrier office name', 'global' => 1, 'visible' => 1, 'sort_order' => 247, // example 'required' => 0, 'user_defined' => 1, 'visible_on_front' => 1 ));  • What methods are you employing to try and adjust the sort order of the default attributes? And also, how are you adding your custom attributes to the form? – davidalger Jul 16 '13 at 3:12 • In my mysql4-install-0.1.0.php script, i use$this->addAttribute('customer_address', 'carrier_office_name', array( 'type' => 'int', 'input' => 'text', 'label' => 'Carrier office name', 'global' => 1, 'visible' => 1, 'sort_order'=>247(example) 'required' => 0, 'user_defined' => 1, 'visible_on_front' => 1 ));The attribute is added by magento (in the frontend i add the form field and it works ok).Then if i change the sort order directly in DB user defined attribute are rearranged, but the are place always after the system attributes. – Evgeni Ivanov Jul 16 '13 at 9:07

Take a look at the code found in Mage_Adminhtml_Block_Sales_Order_Create_Form_Address::_prepareForm. After it calls _addAttributesToForm to add all the attributes, it does extra processing on a handful of the default attributes. When it does this, it is removing them from the form, then adding them back in after '^' which will put them at the top of the form regardless of their original position.

It is certainly possible to alter the positioning of all the attributes in the form, but to do so you will need to extend the block in order to control the final positioning of the form elements.

• I managed to do it!Thank you very much, David! – Evgeni Ivanov Jul 17 '13 at 13:54

If somebody else is trying to achieve this:

$someFieldName =$this->_form->getElement('some_field_name');

//Remove the element from its default position
$fieldset->removeField($someFieldName->getId());

$someNewField =$fieldset->addField($someFieldName->getId(), 'text',$someFieldName->getData(),
**$this->_form->getElement('lastname')->getId()**);  The key piece of info: $this->_form->getElement('lastname')->getId() is the element AFTER which the custom input field has to be positioned.

The solution that I used was to define my custom attribute as a system attribute. The only thing required for that was to set the parameter 'user_defined' with the value 0.

\$this->addAttribute('customer_address', 'mobile', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Mobile',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'visible_on_front' => 1,
'position' => 130 ));


Dont know if the difference between a system or user defined attribute matters.

NB (magento 1.9.2.1) : I had to use the parameter 'position' instead of 'sort_order' for the method addAttribute()

• Just to make it clear for me: Changing user_defined to 0 changes the sort order of the attributes? – Fabian Blechschmidt Nov 12 '15 at 9:43