在 Woocommerce 中的条款和条件下方添加自定义结帐字段

Max*_*oyd 5 php wordpress checkbox checkout woocommerce

I have built an e-commerce site using Woocommerce. I would like to add two more check boxes below the terms and conditions. I have searched everywhere for a working solution and the only thing that I found is a commercial plugin.

How to add custom checkout fields (2 checkboxes) below the terms and conditions programmatically?

Location of the terms and condition screenshot:

条款和条件下方的两个复选框

Loi*_*tec 6

  • 第一个挂钩函数显示 2 个额外的结帐字段
  • 第二个挂钩函数将检查两个复选框是否都被“选中”以允许结帐,如果没有,则显示自定义错误通知......

编码:

add_action('woocommerce_checkout_before_terms_and_conditions', 'checkout_additional_checkboxes');
function checkout_additional_checkboxes( ){
    $checkbox1_text = __( "My first checkbox text", "woocommerce" );
    $checkbox2_text = __( "My Second checkbox text", "woocommerce" );
    ?>
    <p class="form-row custom-checkboxes">
        <label class="woocommerce-form__label checkbox custom-one">
            <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="custom_one" > <span><?php echo  $checkbox1_text; ?></span> <span class="required">*</span>
        </label>
        <label class="woocommerce-form__label checkbox custom-two">
            <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="custom_two" > <span><?php echo  $checkbox2_text; ?></span> <span class="required">*</span>
        </label>
    </p>
    <?php
}

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['custom_one'] )
        wc_add_notice( __( 'You must accept "My first checkbox".' ), 'error' );
    if ( ! $_POST['custom_two'] )
        wc_add_notice( __( 'You must accept "My second checkbox".' ), 'error' );
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(活动主题)的 function.php 文件中。

测试和工作。

在此处输入图片说明

在此处输入图片说明