0
votes

How do I make the telephone field not required on shipping but have it required on billing in onepage checkout?

I have followed many forums that point to the below method but this disables the required element on both billing and shipping?

http://swarminglabs.com/magento-making-the-telephone-field-not-required-at-checkout/#comment-2687

1

1 Answers

0
votes

I do not know any extension that would allow this. If you want to make it work you should work with Mage_Customer_Model_Form. During checkout process magento calls validateData() method of this model. This method is defined in Mage_Eav_Model_Form. You need to rewrite another model, namely Mage_Sales_Model_Quote_Address as its parent Mage_Customer_Model_Address_Abstract has a valid() method that checks if telephone is not emtpy. So, assuming you have removed is_required and validation_rules for this attribute

in a module etc/config.xml

<config>
  <global>
    <models>
      <customer>
        <rewrite>
          <form>YourNamespace_YourModule_Model_Customer_Form</form>
        </rewrite>
      </customer>
      <sales>
        <rewrite>
          <quote_address>YourNamespace_YourModule_Model_Quote_Address</quote_address>
        </rewrite>
      </sales>
    </models>
  </global>
</config>

in YourNamespace/YourModule/Model/Customer/Form.php

class YourNamespace_YourModule_Model_Customer_Form extends Mage_Customer_Model_Form {

  public function validateData(array $data) {
    //perform parent validation
    $result = parent::validateData($data);

    //checking billing address; perform additional validation
    if ($this->getEntity()->getAddressType() == Mage_Sales_Model_Quote_Address::TYPE_BILLING) {
      $valid = $this->_validatePhoneForBilling($data);          
      if ($result !== true && $valid !== true) {
        $result[] = $valid;
      }
      elseif ($result === true && $valid !== true) {
        $result = $valid;
      }
    }

    return $result;
  }

  protected function _validatePhoneForBilling($data) {
    $errors     = array();
    if (empty($data['telephone'])) {
      $attribute  = $this->getAttribute('telephone');
      $label      = Mage::helper('eav')->__($attribute->getStoreLabel());
      $errors[] = Mage::helper('eav')->__('"%s" is a required value.', $label);
    }
    if (empty($errors)) {
      return true;
    }
    return $errors;

  }
}

in YourNamespace/YourModule/Model/Quote/Address.php

class YourNamespace_YourModule_Model_Quote_Address extends Mage_Sales_Model_Quote_Address {
  public function validate() {
    if ($this->getAddressType() == self::TYPE_SHIPPING) {
      $result = parent::validate();
      $errorMsg = Mage::helper('customer')->__('Please enter the telephone number.');
      if (is_array($result) && in_array($errorMsg, $result)) {
        $result = array_diff($result, array($errorMsg));
      }          
      if (empty($result)) {
        return true;
      }
      return $result;
    }
    else {
      return parent::validate();
    }
  }
}