【问题标题】:Adding co-dependent checkout fields添加相互依赖的结帐字段
【发布时间】:2013-03-04 22:47:23
【问题描述】:

我想在结帐表单中添加两个字段。我经营一个销售越野零件的网站。我想要一个文本框,客户可以在其中输入他们的车辆年份/品牌/型号。如果他们不希望我们验证该零件是否与他们的车辆兼容,我希望他们必须选中一个框,上面写着同样的内容。

http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ - 第 3 课非常适合添加字段....但我不知道如何仅在文本框为空时才需要复选框。

TIA

【问题讨论】:

    标签: php javascript wordpress woocommerce


    【解决方案1】:

    我对 woothemes 了解不多,但这里有一段基本的“如果填写表格,复选框将被禁用”。你没有用 PHP 标记你的查询,所以这都是基本的 javascript

    <!DOCTYPE html>
    <meta charset="UTF-8">
    <title>test</title>
    
    <input type="checkbox" id="myCheckBox"></input>
    <input type="text" name="myTextBox" id="checking" onblur="myTextBox(this);" onmousedown="myTextBoxActive(this);" />
    
    <script>  
    
    
    function myTextBox(field) {
        if (field.value == '') {
            document.getElementById('myCheckBox').disabled=false;
        }
    }
    
    
    function myTextBoxActive(field) {
        if (field.value == '') {
            document.getElementById('myCheckBox').disabled=true;
        }
    }
    
    
    
    
    </script>
    

    【讨论】:

    • 这可以工作,但我正在寻找更多的服务器端答案(尽管我知道验证通常是通过 javascript 在客户端完成的)。这可能无法通过 php,但我将 php 标签添加到我的查询中以防万一:-) 谢谢!
    【解决方案2】:

    我猜是昨晚很晚了。答案很简单。以下来自我在原始问题中发布的链接。

    /**
     * Process the checkout
     **/
    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
    
    function my_custom_checkout_field_process() {
        global $woocommerce;
    
        // Check if set, if its not set add an error.
        if (!$_POST['my_field_name'])
            $woocommerce->add_error( __('Please enter something into this new shiny field.') );
    }
    

    所有要做的就是:

    if (!$_POST['my_field_name'])
            $woocommerce->add_error( __('Please enter something into this new shiny field.') );
    

    修改如下:

    if (!($_POST['my_field_name'] || $_POST['my_checkbox']))
            $woocommerce->add_error( __('Please enter something into this new shiny field or check the box.') );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-17
      • 2016-01-10
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多