0

I've succesfully managed to add custom fields to the customer. However I need these fields to show up in onepage checkout.

I've overridden Mage_Customer_Block_Widget_Name and created my own customer/widget/name.phtml, added the attributes in the sql/xxx_setup/installer-x.y.z.php (added them to adminhtml_customer, customer_account_edit, checkout_register and customer_account_create) and they work fine in the admin site, however they just wont work on the checkout form. The field shows up, but it has the wrong value and no label.

I'm clueless why does it work in the customer registration form but doesn't in the checkout.

The installer code to add the attribute is:

$attributes = array(
    'lastname2' =>  array(
        'frontend_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
    ));


foreach($attributes as $attribute_code=>$definition)
    {
        $installer->addAttribute('customer', $attribute_code,  $definition); 

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

The code in name.phtml is

<div class="<?php echo $this->getContainerClassName()?>">
    <?php if ($this->showPrefix()): ?>
        <div class="field name-prefix">
            <label for="<?php echo $this->getFieldId('prefix')?>"<?php if ($this->isPrefixRequired()) echo ' class="required"' ?>><?php if ($this->isPrefixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('prefix') ?></label>
            <div class="input-box">
                <?php if ($this->getPrefixOptions() === false): ?>
                    <input type="text" id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getPrefix()) ?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?> />
                    <?php else: ?>
                    <select id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?>>
                        <?php foreach ($this->getPrefixOptions() as $_option): ?>
                            <option value="<?php echo $_option?>"<?php if ($this->getObject()->getPrefix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
                        <?php endforeach; ?>
                </select>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>
    <div class="field name-firstname">
        <label for="<?php echo $this->getFieldId('firstname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('firstname') ?></label>
        <div class="input-box">
            <input type="text" id="<?php echo $this->getFieldId('firstname')?>" name="<?php echo $this->getFieldName('firstname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>" title="<?php echo $this->getStoreLabel('firstname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>" <?php echo $this->getFieldParams() ?> />
        </div>
    </div>
    <?php if ($this->showMiddlename()): ?>
        <?php $isMiddlenameRequired = $this->isMiddlenameRequired(); ?>
        <div class="field name-middlename">
            <label for="<?php echo $this->getFieldId('middlename')?>"<?php echo $isMiddlenameRequired ? ' class="required"' : '' ?>><?php echo $isMiddlenameRequired ? '<em>*</em>' : '' ?><?php echo $this->getStoreLabel('middlename') ?></label>
            <div class="input-box">
                <input type="text" id="<?php echo $this->getFieldId('middlename')?>" name="<?php echo $this->getFieldName('middlename')?>" value="<?php echo $this->escapeHtml($this->getObject()->getMiddlename()) ?>" title="<?php echo $this->getStoreLabel('middlename') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('middlename') ?>" <?php echo $this->getFieldParams() ?> />
            </div>
        </div>
        <?php endif; ?>
    <div class="field name-lastname">
        <label for="<?php echo $this->getFieldId('lastname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('lastname') ?></label>
        <div class="input-box">
            <input type="text" id="<?php echo $this->getFieldId('lastname')?>" name="<?php echo $this->getFieldName('lastname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname()) ?>" title="<?php echo $this->getStoreLabel('lastname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname') ?>" <?php echo $this->getFieldParams() ?> />
        </div>
    </div>

    <div class="field name-lastname">
        <label for="<?php echo $this->getFieldId('lastname2')?>"><?php echo $this->getStoreLabel('lastname2') ?></label>
        <div class="input-box">
            <input type="text" id="<?php echo $this->getFieldId('lastname2')?>" name="<?php echo $this->getFieldName('lastname2')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname2()) ?>" title="<?php echo $this->getStoreLabel('lastname2') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname2') ?>" <?php echo $this->getFieldParams() ?> />
        </div>
    </div>

    <?php if ($this->showSuffix()): ?>
        <div class="field name-suffix">
            <label for="<?php echo $this->getFieldId('suffix')?>"<?php if ($this->isSuffixRequired()) echo ' class="required"' ?>><?php if ($this->isSuffixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('suffix') ?></label>
            <div class="input-box">
                <?php if ($this->getSuffixOptions() === false): ?>
                    <input type="text" id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getSuffix()) ?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?> />
                    <?php else: ?>
                    <select id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?>>
                        <?php foreach ($this->getSuffixOptions() as $_option): ?>
                            <option value="<?php echo $_option?>"<?php if ($this->getObject()->getSuffix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
                        <?php endforeach; ?>
                </select>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>
</div>
2
  • 1
    You don't need 'frontend_label'=>'Apellido Materno' it is the same as label. What do you mean by "the wrong value"? Change the attribute system to 0 and check in the backend, what the label is on the customer page. And check the eav_attribute and customer_eav_attribute for the attribute. Does it exist? Theoretically the label should be used, if you don't define a storeView label, but in my experience, sometimes it doesn't. So fix this error or define a storeview label. Commented Nov 5, 2013 at 8:05
  • Ur right about frontend_label. By wrong value I mean it shows me the value of lastname, not lastname2. The attribute is set to system=>yes so it wont show automatically on the forms wherever it wants to, but rather where I manually put it (yes I tried visible=>no, but then it doesn't get processed). The attribute does exist, it works fine in the customer registration and in the customer admin panel. It just wont work in the onepage checkout. My gut feeling is that I need to add that attribute to another entity as well (I tried to add it to customer/address but it didnt work either)
    – dorphalsig
    Commented Nov 5, 2013 at 13:26

1 Answer 1

1

You have a bug here:

<input type="text" id="<?php echo $this->getFieldId('lastname2')?>" 
name="<?php echo $this->getFieldName('lastname2')?>" 
value="<?php echo $this->escapeHtml($this->getObject()->getLastname()) ?>"
title="<?php echo $this->getStoreLabel('lastname2') ?>" maxlength="255"...

Have a look on $this->getObject()->getLastname() should be $this->getObject()->getLastname2()

6
  • Fabian. yes a friend pointed that out for me this morning. That explains the wrong value. Once that got fixed, the wrong value is gone, and nothing shows up :(
    – dorphalsig
    Commented Nov 6, 2013 at 5:24
  • What do you mean? Nothing? No value? Is the value in the database? And is it loaded? Commented Nov 6, 2013 at 12:42
  • The vealue is in the DB. And the customer has the value. I'm starting to think I should create the lastname2 attribute in customer/address??
    – dorphalsig
    Commented Nov 6, 2013 at 19:57
  • My guess is, that the collection doesn't load the value or the customer doesn't Commented Nov 6, 2013 at 21:05
  • A Collection there? Now I'm lost. Can you explain, please? (the customer is loading the second last name, because I can get it everywhere else)
    – dorphalsig
    Commented Nov 6, 2013 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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