Pergunta

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
));
Foi útil?

Solução

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.

Outras dicas

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());

//Add new field
$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()

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top