答案 0 :(得分:8)
于2018年7月更新 - 增加了Woocommerce版本3.4 +的兼容性
如果尚未完成,首先需要在结帐页面启用条款和条件:
在注册表单上获取条款和条件复选框的代码:
// 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’ve read and accept the <a href="%s" target="_blank" class="woocommerce-terms-and-conditions-link">terms & 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文件中。经过测试和工作。
如果未选中复选框,则会显示错误消息。
此复选框只是一个验证步骤(它未保存在数据库中,因为我们不在任何地方使用该信息)。
要仅在注册页中允许使用条款和条件,请使用以下选项之一:
add_filter( 'woocommerce_checkout_show_terms', '__return_false' );
或者
add_filter( 'woocommerce_get_terms_and_conditions_checkbox_text', '__return_false' );
代码放在活动子主题(或活动主题)的function.php文件中。在WC 3.5+上测试并且有效。