0

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.

2 Answers 2

1

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.

8
  • Thanks for the help, I tried this code and while it lets me finish the checkout the "Job Title" attribute isn't updating. Just to confirm the "attribute_code" in the "eav_attribute" table in the database for this is "job_title". Do you have any other suggestions to check?
    – HannaMan
    Jul 25, 2014 at 11:52
  • if its a customer attribute, I will assume it static. Is the quote and / or sales table was altered to include job_title as a column?
    – MTM
    Jul 25, 2014 at 12:02
  • Usually magneto customer attributes are stored in the EAV structure, tables customer_entity_*
    – Amasty
    Jul 25, 2014 at 13:14
  • Which tables should I specifically check? When I done the tutorial it only mentioned about them being stored in "eav_attribute". The table "customer_eav_attribute" does have a reference to this attribute's id.
    – HannaMan
    Jul 25, 2014 at 13:23
  • can you please updated your question by posting the code that was used to create this attribute?
    – MTM
    Jul 25, 2014 at 13:38
0

You can use this module https://www.magecheckout.com/magento-customer-attributes.html to add new customer attributes

Your Answer

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

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