[英]Add a second checkbox in WooCommerce registration form
How could I add a second checkbox in the registration form for WooCommerce users?如何在 WooCommerce 用户的注册表中添加第二个复选框?
On the 'my account' page of WooCommerce it gives you the option to log in or register (that's where I need help).在 WooCommerce 的“我的帐户”页面上,您可以选择登录或注册(这是我需要帮助的地方)。
I already have a checkbox established to accept the privacy policy (just below the user, e-mail and password boxes).我已经建立了一个复选框来接受隐私政策(就在用户、电子邮件和密码框下方)。
This is the hook that I have achieved thanks to this article这是我通过这篇文章实现的钩子
add_action( 'woocommerce_register_form', function () {
woocommerce_form_field( 'politica_privacidad_registro', array(
'type' => 'checkbox',
'class' => array('form-row rgpd'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true,
'label' => 'He leído y acepto la política de privacidad',
));
}, 10);
add_filter( 'woocommerce_registration_errors', function ( $errores, $usuario, $email ) {
if ( ! (int) isset( $_POST['politica_privacidad_registro'] ) ) {
$errores->add( 'politica_privacidad_registro_error', 'Tienes que aceptar nuestra política de privacidad' );
}
return $errores;
}, 10, 3);
But I need another checkbox so that they accept another necessary requirement on the web (age verification).但我需要另一个复选框,以便他们接受 web(年龄验证)的另一个必要要求。 That mandatory age verification checkbox to register, of course, would be accompanied by a small phrase.当然,用于注册的强制性年龄验证复选框会附有一个小短语。
I have tried with some hook in the functions.php
, but it happens that if you accept the privacy checkbox you could already register even if you do not accept the second mandatory checkbox.我已经尝试在functions.php
中使用一些挂钩,但碰巧如果您接受隐私复选框,即使您不接受第二个强制复选框,您也可以注册。
This is the code that best suited what I needed to this post这是最适合我这篇文章所需的代码
// To add the form
function add_age_verification() {
?>
<p class="form-row form-row-first">
<label for="reg_age_verification" class="woocommerce-form__label woocommerce-form__label-for-checkbox"><?php _e( 'age verification', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox" name="age_verification" id="reg_age_verification" value="<?php if ( ! empty( $_POST['age_verification'] ) ) esc_attr_e( $_POST['age_verification'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
// to validate the form
function validate_age_verification( $errors, $username, $email ) {
if ( isset( $_POST['age_verification'] ) && empty( $_POST['age_verification'] ) ) {
$errors->add( 'age_verification_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
}
return $errors;
}
// to save the form into user meta age_verification
function age_verification_save( $customer_id ) {
if ( isset( $_POST['age_verification'] ) ) {
update_user_meta( $customer_id, 'age_verification', sanitize_text_field( $_POST['age_verification'] ) );
}
}
add_action( 'woocommerce_register_form', 'add_age_verification' );
add_filter( 'woocommerce_registration_errors', 'validate_age_verification', 10, 3 );
add_action( 'woocommerce_created_customer', 'age_verification_save' );
Aside from the incompatibility of hooks, also this second checkbox with the tests that I have carried out appears the phrase in a line and the check box below.除了钩子不兼容之外,我执行的测试的第二个复选框也出现了一行中的短语和下面的复选框。 It must all go together on one line.它必须将所有 go 都放在一条线上。
How could I do it and have both hooks work without creating incompatibilities?我该怎么做,并且在不造成不兼容的情况下让两个钩子都工作?
Your code contains some minor mistakes您的代码包含一些小错误
woocommerce_created_customer
missing from the privacy policy隐私政策中缺少woocommerce_created_customer
reg_age_verification
& age_verification
are used interchangeably reg_age_verification
和age_verification
可互换使用form-row-first
, form-row-last
& form-row-wide
.关于字段的显示,这可以通过添加/修改 CSS 选择器来完成: form-row-first
, form-row-last
& form-row-wide
。 Furthermore, it is a matter of CSS adjustments, depending on the theme you use另外就是CSS调整的事情,看你用的主题So you get:所以你得到:
// Add fields to register page
function action_woocommerce_register_form() {
// First checkbox
woocommerce_form_field( 'politica_privacidad_registro', array(
'type' => 'checkbox',
'class' => array( 'woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide' ),
'label_class' => array( 'woocommerce-form__label woocommerce-form__label-for-checkbox checkbox' ),
'input_class' => array( 'woocommerce-form__input woocommerce-form__input-checkbox input-checkbox' ),
'required' => true,
'label' => __( 'He leído y acepto la política de privacidad', 'woocommerce' ),
));
// Second checkbox
woocommerce_form_field( 'age_verification', array(
'type' => 'checkbox',
'class' => array( 'woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide' ),
'label_class' => array( 'woocommerce-form__label woocommerce-form__label-for-checkbox checkbox' ),
'input_class' => array( 'woocommerce-form__input woocommerce-form__input-checkbox input-checkbox' ),
'required' => true,
'label' => __( 'Age verification', 'woocommerce'),
));
}
add_action( 'woocommerce_register_form', 'action_woocommerce_register_form' );
// Validation
function filter_woocommerce_registration_errors( $errors, $username, $email ) {
// Privacy NOT isset
if ( ! (int) isset( $_POST['politica_privacidad_registro'] ) ) {
$errors->add( 'politica_privacidad_registro_error', __( 'Tienes que aceptar nuestra política de privacidad.', 'woocommerce' ) );
}
// Age NOT isset
if ( ! (int) isset( $_POST['age_verification'] ) ) {
$errors->add( 'age_verification_error', __( 'Age is required.', 'woocommerce' ) );
}
return $errors;
}
add_filter( 'woocommerce_registration_errors', 'filter_woocommerce_registration_errors', 10, 3 );
// Save fields
function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
// Privacy isset
if ( isset( $_POST['politica_privacidad_registro'] ) ) {
update_user_meta( $customer_id, 'politica_privacidad_registro', sanitize_text_field( $_POST['politica_privacidad_registro'] ) );
}
// Age isset
if ( isset( $_POST['age_verification'] ) ) {
update_user_meta( $customer_id, 'age_verification', sanitize_text_field( $_POST['age_verification'] ) );
}
}
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10 , 3 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.