[英]Add a terms and conditions checkbox in Woocommerce registration form
In woocommerce registration form, there's no "terms and conditions" before sign up button. 在woocommerce注册表中,注册按钮之前没有“条款和条件”。
Is there a way to make it appears in the form? 有没有办法让它出现在表格中?
Here is the link of my theme layout . 这是我的主题布局的链接 。
Updated on july 2018 - Added compatibility for Woocommerce version 3.4+ 更新于2018年7月 - 增加了Woocommerce版本3.4+的兼容性
If not done yet, first you need enable terms and conditions on checkout page: 如果尚未完成,首先您需要在结帐页面上启用条款和条件:
The code to get the term and conditions check box on registration form: 在注册表单上获取条款和条件复选框的代码:
// 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;
}
Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。
When checkbox is not ticked, an error message is displayed. 如果未勾选复选框,则会显示错误消息。
This checkbox is only a validation step (It is not saved in database, as we don't use that information anywhere). 此复选框只是一个验证步骤(它不保存在数据库中,因为我们不在任何地方使用该信息)。
To allow terms and conditions only in Registration page use one of the following: 要仅在“注册”页面中允许使用条款和条件,请使用以下其中一项:
add_filter( 'woocommerce_checkout_show_terms', '__return_false' );
Or 要么
add_filter( 'woocommerce_get_terms_and_conditions_checkbox_text', '__return_false' );
Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 Tested on WC 3.5+ and works. 在WC 3.5+上测试并且有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.