Magento – Custom customer attribute not processing on onepage checkout registration form

attributescustomeronepage-checkoutregister

I have followed a tutorial and created a custom customer attribute which I have added to the registration form from the file: persistent/customer/form/register.phtml

I have this code to process the new attribute:

<li>
    <label for="job_title" class="required"><em>*</em><?php echo $this->__('Job Title') ?></label>
    <div class="input-box">
        <input type="text" name="job_title" id="job_title" value="<?php echo $this->escapeHtml($this->getFormData()->getJobTitle()) ?>" title="<?php echo $this->__('Job Title') ?>" class="input-text required-entry" />
    </div>
</li>

This works fine and updates the attribute when the form is submitted. I am however trying to do the same on the onepage checkout registration form. I have edited the file: onepage/billing.phtml

and added the same code, however this breaks the checkout. I believe the error to be caused by this line:

<?php echo $this->escapeHtml($this->getFormData()->getJobTitle()) ?>

I am unsure how to check the right functions for this to work. I have looked at the sql file for the module which created the modules and checked that this attribute can be used in the relevant forms:

$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
    $attribute->setData("used_in_forms", $used_in_forms)
    ->setData("is_used_for_customer_segment", true)
    ->setData("is_system", 0)
    ->setData("is_user_defined", 1)
    ->setData("is_visible", 1)
    ->setData("sort_order", 100);

$attribute->save();

How can I get this custom customer attribute to display on the onepage registration form? This is a Customer and not Customer Address attribute. Any help is much appreciated.

UPDATE

Below is the code which I have used in the module.

This is the script which is in the sql directory of the module (here there are other custom attributes which I created but this is the code for the "job_title" attribute:

$installer = $this;
$installer->startSetup();


$installer->addAttribute("customer", "job_title",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Job Title",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""
));

$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "job_title");

$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";   

$attribute->setData("used_in_forms", $used_in_forms)
    ->setData("is_used_for_customer_segment", true)
    ->setData("is_system", 0)
    ->setData("is_user_defined", 1)
    ->setData("is_visible", 1)
    ->setData("sort_order", 100);

$attribute->save();

The "Helper" directory has this code in a file called "Data.php":

<?php
class Mymodule_Customerattributes_Helper_Data extends Mage_Core_Helper_Abstract
{
}

The "config.xml" file has this:

<?xml version="1.0"?>
<config>
  <modules>
    <Mymodule_Customerattributes>
      <version>0.1.0</version>
    </Mymodule_Customerattributes>
  </modules>
  <global>
    <helpers>
      <customerattributes>
        <class>Mymodule_Customerattributes_Helper</class>
      </customerattributes>
    </helpers>
    <models>
      <customerattributes>
        <class>Mymodule_Customerattributes_Model</class>
        <resourceModel>customerattributes_mysql4</resourceModel>
      </customerattributes>
    </models>
    <resources>
      <mymodule_customerattributes_setup>
        <setup>
          <module>Mymodule_Customerattributes</module>
          <class>Mage_Customer_Model_Entity_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </mymodule_customerattributes_setup>
      <mymodule_customerattributes_write>
        <connection>
          <use>core_write</use>
        </connection>
      </mymodule_customerattributes_write>
      <mymodule_customerattributes_read>
        <connection>
          <use>core_read</use>
        </connection>
      </mymodule_customerattributes_read>
    </resources>
  </global>
</config> 

Some names such as the module name has been changed as it contained my employer's company name. The structure is the same though.

Thank you.

Best Answer

When you say this is Customer attribute, then this will get the values of logged in customer only, if this what you want, you should use the following code in onepage checkout files:

$this->getQuote()->getCustomer()->getJobTitle();

This code will first get the customer from the quote then will get the related value for Job title. Use the name of the input field as:

<input type="text" name="billing[job_title]" id="job_title" value="<?php echo $this->escapeHtml($this->getQuote()->getCustomer()->getJobTitle()) ?>" title="<?php echo $this->__('Job Title') ?>" class="input-text required-entry" />

and in the config.xml, add the following code in global tag:

<fieldsets>
    <checkout_onepage_billing>
       <job_title>
          <to_customer>*</to_customer>
       </job_title>
    </checkout_onepage_billing>
 </fieldsets>

not tested but the fieldsets part should do the job.