0

如何使电话字段在运输时不需要,但在单页结帐时需要在账单上?

我关注了许多指向以下方法的论坛,但这会禁用计费和运输所需的元素?

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

4

1 回答 1

0

我不知道任何允许这样做的扩展。如果你想让它工作,你应该使用Mage_Customer_Model_Form. 在结帐过程中,magento 调用validateData()此模型的方法。此方法在 中定义Mage_Eav_Model_Form。您需要重写另一个模型,即Mage_Sales_Model_Quote_Address它的父模型Mage_Customer_Model_Address_Abstract有一个valid()检查电话是否不是空的方法。因此,假设您已删除此属性的 is_required 和 validation_rules

在一个模块中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>

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;

  }
}

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();
    }
  }
}
于 2013-07-09T21:38:53.963 回答