4

I have defined a lastname2 attribute for customer and customer_address, it shows up correctly in all forms, but when I'm creating a new address, its does not get 'prefilled' with the customer's value.

Any clues on how to do it?

The definition of my fields:

$entityAttributes = array(
        'customer'=>array(
            'lastname2' =>  array(
                'store_label'=>'Apellido Materno',
                'label' => 'Apellido Materno',
                'input' => 'text',
                'type'  => 'varchar',
                //System =  False and visible true = Show in 'customer_account_create', 'customer_account_edit', 'checkout_register'
                'system'=>true,
                'visible'=>true, //Watch out!! Only visible fields get processed by the form controllers!!!
                'user_defined'=>false,
                'used_in_forms' => array('adminhtml_customer', 'customer_account_edit', 'checkout_register','customer_account_create'),
                'required' => 0,
                'position' =>69
            ),),
'customer_address'=> array(
            'lastname2' =>  array(
                'store_label'=>'Apellido Materno',
                'label' => 'Apellido Materno',
                'input' => 'text',
                'type'  => 'varchar',
                //System =  False and visible true = Show in 'customer_account_create', 'customer_account_edit', 'checkout_register'
                'system'=>false,
                'visible'=>true, //Watch out!! Only visible fields get processed by the form controllers!!!
                'user_defined'=>false,
                'used_in_forms' => array('customer_account_create','customer_register_address','customer_address_edit','adminhtml_customer_address','adminhtml_customer'),
                'required' => 0,
                'position' =>49
            ),
        ),
    );
# Install Attributes
    foreach($entityAttributes as $entity=>$attributes)
    {
        foreach($attributes as $attribute_code=>$definition)
        {
            $installer->addAttribute($entity, $attribute_code,  $definition); 

            /**
            * @var Mage_Eav_Model_Config
            */
            Mage::getSingleton('eav/config')
            ->getAttribute($entity, $attribute_code)
            ->setData('used_in_forms',$definition['used_in_forms'])
            ->save();
        }
    }

My config.xml (relevant extracts, the full version is here)

<global>
<!-- [...] -->
        <fieldsets>
            <!-- @see http://www.excellencemagentoblog.com/customer-registration-fields-magento1-6 -->
            <customer_account>
                <lastname2>
                    <create>1</create>
                    <update>1</update>
                    <name>1</name>
                    <to_quote>customer_lastname2</to_quote>
                    <to_customer_address>lastname2</to_customer_address>
                    <to_customer_address>customer_lastname2</to_customer_address>
                </lastname2>
            </customer_account>      
            <customer_address>
                <lastname2>
                    <to_quote_address>*</to_quote_address>
                </lastname2>
            </customer_address>
        </fieldsets> 
<!-- [...] -->
</global>

Edit: Its not the browser's autofill, I want to replicate the behaviour of the firstname and lastname fields, that show the customer's first and lastname (respectively) by default

  • You mean there is no autofill for the input field ? What happens to other fields in the same form ? – Niloct Nov 28 '13 at 15:51
  • I am a bit confused here. When are these fields being used? – David Manners Jul 24 '14 at 13:21
2

Have a look at the block Mage_Customer_Block_Address_Edit this is the block that controls the address editing. In here you will notice the block of code in the _prepareLayout function that sets the defaults if the address does not exist, so for new addresses.

$this->_address = Mage::getModel('customer/address');

// Init address object
if ($id = $this->getRequest()->getParam('id')) {
    $this->_address->load($id);
    if ($this->_address->getCustomerId() != Mage::getSingleton('customer/session')->getCustomerId()) {
        $this->_address->setData(array());
    }
}

if (!$this->_address->getId()) {
    $this->_address->setPrefix($this->getCustomer()->getPrefix())
        ->setFirstname($this->getCustomer()->getFirstname())
        ->setMiddlename($this->getCustomer()->getMiddlename())
        ->setLastname($this->getCustomer()->getLastname())
        ->setSuffix($this->getCustomer()->getSuffix());
}

Then later on when getting the name html this address object is set against the widget to be used for getting data in the template.

public function getNameBlockHtml()
{
    $nameBlock = $this->getLayout()
        ->createBlock('customer/widget_name')
        ->setObject($this->getAddress());

    return $nameBlock->toHtml();
}

And then finally these values are being used in the template itself app/design/frontend/base/default/template/customer/widget/name.phtml.

<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>

So you will need to either hock onto the before html event and make your changes to the address object or rewrite this block. Either way you will also need to change the template to include your new attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.