【问题标题】:Remove telephone as required field on shipping but not billing Magento删除电话作为运输必填字段,但不向 Magento 计费
【发布时间】:2013-07-09 07:53:21
【问题描述】:

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

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

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

【问题讨论】:

    标签: magento


    【解决方案1】:

    我不知道任何允许这样做的扩展。如果你想让它工作,你应该使用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();
        }
      }
    }
    

    【讨论】:

    • 你能详细描述一下答案吗?即使我想这样做。请帮助我。
    • 您还需要什么详细信息?回复中提供了所有代码。我改进了格式以查看所有要添加的文件
    • 这个函数在magento validatePhoneForBilling()中的位置?
    • 无处,这是您需要创建的自定义模块的代码才能使其工作。 Magento 默认将两个地址视为相同,因此验证是相同的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 2018-01-30
    相关资源
    最近更新 更多