Magento – customer attribute taxvat value not saving to database (from frontend custom registration form)

customer-attributeformsmagento-1.9

We setup 2 website/store. When we tried to create customer from a store (i.e. not main store). By using customer/form_register type form submission. customer created successfully but taxvat field not saving with customer information. All other required customer attributes firstname, lastname, email saving correctly but only taxvat attribute not saving.

Code for Load layout with customer/form_register block type :

<reference name = "content">
   <block type = "customer/form_register" name = "customer_form_register" template = "seller/seller.phtml">
      <action method = "setShowAddressFields"><value>true</value></action>
        <block type = "page/html_wrapper" name = "customer.form.register.fields.before" as = "form_fields_before" translate = "label">
           <label>Form Fields Before</label>
        </block>
   </block>
</reference>

Template File :

<form name="test-form1" id="my-custom-form1" action="<?php echo $this->getPostActionUrl(); ?>" method="post">
    <input type="hidden" name="success_url" value="<?php echo $this->getSuccessUrl(); ?>" />
    <input type="hidden" name="error_url" value="<?php echo $this->getErrorUrl(); ?>" />
    <div>
        <div>
            <label>Email <span class="required">*</span></label>
            <input type="text" name="email"/>
        </div>
    </div>

    <div>
        <div>
            <label>First Name <span class="required">*</span></label>
            <input type="text" name="firstname"/>
        </div>
    </div>

    <div>
        <div>
            <label>Middle Name <span class="required">*</span></label>
            <input type="text" name="middlename"/>
        </div>
    </div>

    <div>
        <div>
            <label>Last Name <span class="required">*</span></label>
            <input type="text" name="lastname"/>
        </div>
    </div>

    <div>
        <div>
            <label>Tax/Vat Number <span class="required">*</span></label>
            <input type="text" name="taxvat"/>
        </div>
    </div>

    <div>
        <div>
            <input type="submit" name="sub" value="Submit">
        </div>
    </div>
</form>

This problem also exist for middlename, prefix, suffix attribute.

All these attributes 'taxvat', 'middlename', 'prefix', 'suffix' are exist in default magento setup same like 'firstname', 'lastname', 'email' exist. the only difference is they are not required.

Can anyone please tell me why this is happened and how to resolve this problem?

Best Answer

This happens because those attributes you mentioned have is_visible set to 0 in the customer_eav_attribute table. They are saved with this value when they are created by the setup.

Before saving the customer data, in the controller (Mage_Customer_AccountController) the _getCustomerErrors function is called:

protected function _getCustomerErrors($customer)
{
    $errors = array();
    $request = $this->getRequest();
    if ($request->getPost('create_address')) {
        $errors = $this->_getErrorsOnCustomerAddress($customer);
    }
    $customerForm = $this->_getCustomerForm($customer);
    $customerData = $customerForm->extractData($request);
    $customerErrors = $customerForm->validateData($customerData);
    if ($customerErrors !== true) {
        $errors = array_merge($customerErrors, $errors);
    } else {
        $customerForm->compactData($customerData);
        $customer->setPassword($request->getPost('password'));
        $customer->setConfirmation($request->getPost('confirmation'));
        $customerErrors = $customer->validate();
        if (is_array($customerErrors)) {
            $errors = array_merge($customerErrors, $errors);
        }
    }
    return $errors;
}

Not all the data you submit from the form gets set on the customer model in order to be saved. The data is first extracted in Mage_Customer_Model_Form class, which extends Mage_Eav_Model_Form. The first condition for the attribute data to be set on the customer model is that the specific attribute should be associated to submited form (you can check these in the customer_form_attribute table by filtering by form_code customer_account_create). This condition should be met for the attributes you mentioned since they are added to this form when they are created by the setup. The second condition for the attribute data to be set on the customer model is to pass the validation in the Mage_Eav_Model_Form::extractData function:

public function extractData(Zend_Controller_Request_Http $request, $scope = null, $scopeOnly = true)
{
    $data = array();
    foreach ($this->getAttributes() as $attribute) {
        if ($this->_isAttributeOmitted($attribute)) {
            continue;
        }
        $dataModel = $this->_getAttributeDataModel($attribute);
        $dataModel->setRequestScope($scope);
        $dataModel->setRequestScopeOnly($scopeOnly);
        $data[$attribute->getAttributeCode()] = $dataModel->extractValue($request);
    }
    return $data;
}

The _isAttributeOmitted function will check the is_visible value:

protected function _isAttributeOmitted($attribute)
{
    if ($this->_ignoreInvisible && !$attribute->getIsVisible()) {
        return true;
    }
    return false;
}

You can check the is_visible values for those attribute by running this query on your database:

SELECT * FROM customer_eav_attribute c WHERE attribute_id IN (
    SELECT attribute_id FROM eav_attribute
    WHERE attribute_code IN ('taxvat', 'middlename', 'prefix', 'suffix') AND entity_type_id=1
);

If the value for is_visible is 0 then you can change it to 1.

If you are using Magento Enterprise then you can edit the customer attributes in Customers -> Attributes -> Manage Customer Attributes. If you set Show on Frontend to Yes then the customer attribute values should be saved correctly into the database.

Related Topic