Up-to-Date Guide to Remove Telephone Required Field from Checkout in Magento

checkout

Please could some one post a guide on the removal of 'required' telephone field in checkout based on the magento layout Version 1.8.0.0, the majority of current googled resources are out of date and no longer work.
This would seem the perfect place to keep an updated how-to / guide.

It would be helpful if some one could confirm the working solution by downloading Magento 1.8.0.0 and providing proof of it's removal, these code snippets are sometimes helpful, but we have yet to find a working solution in an easy to read / documented fashion.

Best Answer

Before 1.8.1 I'm not aware of any ways to make this work, you have to either override the abstract core class in the local code pool or rewrite every single child class (ugly, I know).

From 1.8.1 on, they introduced the event customer_address_validation_after. I got rid of the validation by using reflexion, even if I'm not too proud of it. Might not solve your problem, but I thought I'd post it anyway.

/**
 * Validate address ignoring phone-related errors
 *
 * Internally uses reflexion. Maybe not the most
 * efficient or clean implementation, but allows
 * this functionality to be implemented without
 * the need to rewrite 3 core classes.
 *
 * Listens to:
 * - customer_address_validation_after
 *
 * @param Varien_Event_Observer $observer Event observer
 */
public function validateAddress(Varien_Event_Observer $observer)
{
    /* @var $address Mage_Customer_Model_Address_Abstract */
    $address = $observer->getAddress();
    if (!$address) {
        return;
    }

    $prop = new ReflectionProperty('Mage_Customer_Model_Address_Abstract', '_errors');
    if (!$prop) {
        return;
    }
    $prop->setAccessible(true);
    $errors = $prop->getValue($address);
    $prop->setValue($address, array());

    $errorMessage = $this->getErrorMessage();
    foreach ($errors as $error) {
        if ($error !== $errorMessage) {
            $address->addError($error);
        }
    }
}

/**
 * Get standard error message
 *
 * @return string
 */
protected function getErrorMessage()
{
    return Mage::helper('customer')->__('Please enter the telephone number.');
}

I also wrote an update script to define the filed as not required, and edited all forms to remove the frontend validation. Like I said, it might not be the best solution, but it's better than rewriting 3-4 classes IMHO.

UPDATE The core team wrapped the event customer_address_validation_after in a conditional starting from 1.9.0 through 1.9.1.1 (I really wonder why, makes no sense to me...) so my solution won't work for these versions, unfortunately. The conditional was luckily removed in 1.9.2.