添加相互依赖的签出字段

Adding co-dependent checkout fields

本文关键字:字段 依赖 添加      更新时间:2023-09-26

我想在结帐表单中添加两个字段。我经营一个卖越野配件的网站。我想有一个文本框,客户可以输入他们的车辆年/制造/型号。如果他们不希望我们验证该部件是否与他们的车辆兼容,我希望他们必须在一个方框中勾选同样的内容。

http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/—第3课非常适合添加....字段但我不知道如何使复选框是必需的,只有当文本框为空。

TIA

我不太了解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>

我猜昨晚很晚。答案很简单。以下是我在我最初的问题中发布的链接。

/**
 * 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.') );