Woocommerce 附加注册字段验证不起作用

Woocommerce additional registration field validation not working

我已经按照教程准确地向注册添加了一个附加字段,但我无法对其进行验证。它不需要保存,它只需要是一个一次性问题来确认用户正在做正确的事情。

我已将以下内容添加到 functions.php,但它不起作用...请问有人知道为什么吗?

function wooc_extra_register_fields() {
?>
<?php if (($_GET['register'] == 'instructor')) : ?>
<p class="form-row form-row-wide">
<label for="reg_instructor"><?php _e( 'Please confirm you\'re trying to register as an instructor', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="checkbox" class="checkbox" name="instructor" id="reg_instructor" value="<?php if ( ! empty( $_POST['instructor'] ) ) echo esc_attr( $_POST['instructor'] ); ?>" />
</p>
<?php endif; ?>
<?php
}


add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['instructor'] ) && empty( $_POST['instructor'] ) ) {
$validation_errors->add( 'instructor_error', __( 'You must be an instructor!', 'woocommerce' ) );
}
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );


add_action('user_profile_update_errors','instructor_validation',10,1);
function instructor_validation($args)
{
if ( isset( $_POST['instructor'] ) && empty( $_POST['instructor'] ) ) {
$args->add( 'instructor_error', __( 'You must be an instructor!', 'woocommerce' ),'');
}
}


function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['instructor'] ) ) {
update_user_meta( $customer_id, 'instructor', sanitize_text_field( $_POST['instructor'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

任何帮助,非常感谢...

谢谢, 马特

完全未经测试,因为我现在无法检查。但似乎您对复选框没有任何价值。然后你正在验证 if ( isset( $_POST['instructor'] ) && empty( $_POST['instructor'] ) ) 但如果设置了一个复选框,那么它永远不会 empty,所以你的条件永远不会被满足。最后,您不需要 sanitize_text_field(),因为它不是文本字段。

相反,我将复选框值设置为 1 并测试发布的值是否等于 1。WooCommerce 实际上使用了很多 yesno 用于布尔复选框和这甚至可能更容易,因此您可以在需要时尝试。但是,如果只是测试该框是否被选中并且等于某个特定的东西,这几乎无关紧要。

function wooc_extra_register_fields() { ?>
    <?php if ( isset($_GET['register']) && ($_GET['register'] == 'instructor')) : ?>
    <p class="form-row form-row-wide">
    <label for="reg_instructor"><?php _e( 'Please confirm you\'re trying to register as an instructor', 'woocommerce' ); ?> <span class="required">*</span></label>
    <input type="checkbox" class="checkbox" name="instructor" id="reg_instructor" value="1" <?php checked ( isset($_POST['instructor']) && 1 == $_POST['instructor'], true ); ?>/>
    </p>
    <?php endif; ?>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );


function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
    if ( ! isset( $_POST['instructor'] ) || 1 != $_POST['instructor'] ) {
        $validation_errors->add( 'instructor_error', __( 'You must be an instructor!', 'woocommerce' ) );
    }
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );



function instructor_validation($args){
    if ( ! isset( $_POST['instructor'] ) || 1 != $_POST['instructor'] ) {
        $args->add( 'instructor_error', __( 'You must be an instructor!', 'woocommerce' ),'');
    }
}
add_action('user_profile_update_errors','instructor_validation',10,1);


function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['instructor'] ) && 1 == $_POST['instructor'] ) {
        update_user_meta( $customer_id, 'instructor', 1 );
    }
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

PS- 我的代码可能会对 不是 讲师的注册人产生不利影响,如果您可能需要详细说明验证的条件逻辑,可能会检查再次为 $_GET['register'] 变量。

编辑

这是我对如何将用户元数据添加到 WooCommerce 字段的最佳猜测。但是,无法控制值显示的内容,因此 1. 您可以调整保存 instructor 元的方式...将其保存为 'yes' 或 'instructor' 或其他内容。或者,添加您自己的 custom user meta section

function wooc_show_customer_meta_fields( $fields ){
    $fields['instructor'] = array(
                'title' => __( 'Instructors', 'your-plugin' ),
                'fields' => array(
                    'instructor' => array(
                        'label'       => __( 'Instructor Status', 'your-plugin' ),
                        'description' => ''
                    )
                );
    return $fields;
}
add_filter( 'woocommerce_customer_meta_fields', 'wooc_show_customer_meta_fields' );