在 Woocommerce 注册表中添加条款和条件复选框

Add a terms and conditions checkbox in Woocommerce registration form

在 woocommerce 注册表中,注册按钮前没有 "terms and conditions"。

有没有办法让它出现在表格中?

这里是the link of my theme layout.

2018 年 7 月更新 - 添加了对 Woocommerce 版本 3.4+ 的兼容性

如果还没有完成,首先您需要在结帐页面启用条款和条件:

  1. 根据您的条款和条件在 Wordpress 中创建新页面
  2. 在 Woocommerce > 设置 > 结帐 > 结帐页面中启用该页面 (部分)
  3. 然后保存……大功告成。

在注册表单上获取条款和条件复选框的代码:

// Add term and conditions check box on registration form
add_action( 'woocommerce_register_form', 'add_terms_and_conditions_to_registration', 20 );
function add_terms_and_conditions_to_registration() {

    if ( wc_get_page_id( 'terms' ) > 0 && is_account_page() ) {
        ?>
        <p class="form-row terms wc-terms-and-conditions">
            <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
                <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="terms" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms'] ) ), true ); ?> id="terms" /> <span><?php printf( __( 'I&rsquo;ve read and accept the <a href="%s" target="_blank" class="woocommerce-terms-and-conditions-link">terms &amp; conditions</a>', 'woocommerce' ), esc_url( wc_get_page_permalink( 'terms' ) ) ); ?></span> <span class="required">*</span>
            </label>
            <input type="hidden" name="terms-field" value="1" />
        </p>
    <?php
    }
}

// Validate required term and conditions check box
add_action( 'woocommerce_register_post', 'terms_and_conditions_validation', 20, 3 );
function terms_and_conditions_validation( $username, $email, $validation_errors ) {
    if ( ! isset( $_POST['terms'] ) )
        $validation_errors->add( 'terms_error', __( 'Terms and condition are not checked!', 'woocommerce' ) );

    return $validation_errors;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

未勾选复选框时,会显示错误消息。

This checkbox is only a validation step (It is not saved in database, as we don't use that information anywhere).


加法:

要允许条款和条件仅在注册页面,请使用以下选项之一:

add_filter( 'woocommerce_checkout_show_terms', '__return_false' );

或者

add_filter( 'woocommerce_get_terms_and_conditions_checkbox_text', '__return_false' );

代码进入您的活动子主题(或活动主题)的 function.php 文件。在 WC 3.5+ 上测试并有效。