如何在运输时不需要电话字段,但在单页结账时需要付费?
我已经关注了许多指向以下方法的论坛,但这会在结算和发货时禁用所需的元素?
http://swarminglabs.com/magento-making-the-telephone-field-not-required-at-checkout/#comment-2687
答案 0 :(得分:0)
我不知道任何允许这样做的扩展。如果您想让它工作,您应该使用Mage_Customer_Model_Form
。在结帐过程中,magento调用此模型的validateData()
方法。此方法在Mage_Eav_Model_Form
中定义。您需要重写另一个模型,即Mage_Sales_Model_Quote_Address
,因为其父Mage_Customer_Model_Address_Abstract
具有valid()
方法,可检查电话是否不是emtpy。因此,假设您已删除此属性的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();
}
}
}