Magento – Magento Customer Attribute Not Showing

customercustomer-attributefrontendmagento-1.9

I have created a customer attribute to capture the company name on registration, not as part of the address field.

The field is created in eav_attribute

  //add Company Name to customer attributes
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute("customer", "company_name",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Company Name",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => true,
    "default" => "",
    "frontend" => "",
    "unique"     => false

        ));         

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

$setup->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'tamper_company_name',
    '3'  //sort_order
);

$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", 3)
                ;
$attribute->save();
$installer->endSetup();

The field displays and populates properly from the account creation page:
\app\design\frontend\base\default\template\MYMODULE\customer\widget\name.phtml
Which I overwrote using config.xml

<div class="field name-companyname">
    <label for="<?php echo $this->getFieldId('company_name')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('company_name') ?></label>
    <div class="input-box">
        <input type="text" id="<?php echo $this->getFieldId('company_name')?>" name="<?php echo $this->getFieldName('company_name')?>" value="<?php echo $this->escapeHtml($this->getObject()->getTamper_company_name()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->getStoreLabel('company_name')) ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('company_name') ?>" <?php echo $this->getFieldParams() ?> />
    </div>
</div>

I have two issues:

  1. In the customer dashboard, create address does not return anything for the above code, even through it uses the same template. Name and surname work, but Company Name does not – it does not return any value or labels.
    $this->getObject()->getFirstname() returns the name $this->getObject()->getCompany_name() returns nothing. getAttributeValidationClass also does not work.
  2. In the address templates, using {{depend company_name}}{{var company_name}}{{/depend}} does not return anything.

If i Mage::Log $this->getObject() from the address entry screen, I get a data array including name, pre/post fix, initial but no company_name.

Do I need to override when of the helpers to add this field in?

Best Answer

I have a solution which is acceptable for the business logic although it does not fix the underlying issue of not being able to retrieve the custom value from the customer and use it as part of the address.

This approach will pre-populate addresses "Company" field where the custom field "company_name" has been set. It then returns the value "Company" - if it has been amended or not.

Edit \app\design\frontend\base\default\template\MYMODULE\customer\address\edit.phtml

   <li class="wide">
        <label for="company"><?php echo $this->__('Company') ?></label>
        <div class="input-box">

        <?php if ($this->getAddress()->getCompany()) {
            $companyName = $this->escapeHtml($this->getAddress()->getCompany());//Company name has been filled
        }else{
            $companyName =$this->escapeHtml($this->getCustomer()->getCompany_name());
        }?>
            <input type="text" name="company" id="company" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Company')) ?>" value="<?php echo $companyName ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('company') ?>" />
        </div>
    </li>

And edit app\design\frontend\base\default\template\MYMODULE\customer\widget\name.phtml

<?php if ($this->getStoreLabel('tamper_company_name')):?>
<div class="field name-companyname">

    <label for="<?php echo $this->getFieldId('tamper_company_name')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('company_name') ?></label>
    <div class="input-box">
        <input type="text" id="<?php echo $this->getFieldId('company_name')?>" name="<?php echo $this->getFieldName('company_name')?>" value="<?php echo $this->escapeHtml($this->getObject()->getCompany_name()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->getStoreLabel('company_name')) ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('company_name') ?>" <?php echo $this->getFieldParams() ?> />
    </div>
</div>
<?php endif;?>

This means that the custom field "company_name" is asked for when creating an account, but not when entering an address. The default field "company" is populated by "company_name", which the user can update as required.

As it is using the default fields, it populates in all the correct places for address handling without the need to override the default behaviour.

I would still be interested in getting/setting the values in a better way if anyone has some thoughts.

Related Topic