Changing & Fixing Required Customer Fields

Sometimes, a Magento owner may decide that certain customer fields which were optional should now be compulsory. This can break the OPC for customers who registered before this time who are trying to check out. Magento will validate their customer object during the billing section of the OPC, and throw an error (that in our case “Prefix is a required value”).

To fix this, we can copy the information from the customer’s default billing address.

The validation takes place in the Mage_Checkout_Model_Type_Onepage class, in the _validateCustomerData method:

<?php
$customerErrors = $customerForm->validateData($customerData);
        if ($customerErrors !== true) {
            return array(
                'error'     => -1,
                'message'   => implode(', ', $customerErrors)
            );
        }

config.xml

<checkout>
	<rewrite>
		<onepage_billing>Brandoutlet_Config_Block_Checkout_Onepage_Billing</onepage_billing>
	</rewrite>
</checkout>

Overriding the Onepage Model

<?php
class Brandoutlet_Config_Model_Checkout_Type_Onepage extends Mage_Checkout_Model_Type_Onepage{
	protected function  _validateCustomerData(array $data){
		// Copy over the prefix from the default billing address.
		// It looks like prefix was made compulsory at some point, so this makes
		// Accounts saved before this was done throw an error at the OPC.
		
		$customer = $this->getQuote()->getCustomer();
		
		if(!$customer->getPrefix()){
			if($billingDefault = $customer->getDefaultBillingAddress()){
				$customer->setPrefix($billingDefault->getPrefix());
			}
		}
		
		return parent::_validateCustomerData($data);
	}
}