Reputation: 33
I've followed the tutorial for adding an additional field to registration exactly, but I can't get it to validate. It doesn't need to save, it just needs to be a one-time question to confirm the user is doing the right thing.
I've added the below into functions.php but it's not working... can anyone see why please?
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' );
Any help, much appreciated...
Thanks, Matt
Upvotes: 0
Views: 4427
Reputation: 26319
Completely untested, because I can't check it right now. But it seems like you have no value for the checkbox. Then you are validating if ( isset( $_POST['instructor'] ) && empty( $_POST['instructor'] ) )
but if a checkbox is set, then it is never empty
, so you're condition would never be met. Lastly you don't need to sanitize_text_field()
since it isn't a text field.
Instead, I've set the checkbox value to 1
and test if the posted value equals 1. WooCommerce actually uses a lot of yes
versus no
for boolean checkboxes and that might even be easier so you can try that if you need to. But for just testing if the box is checked and equal to something specific it almost doesn't matter.
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- my code might have adverse effects on registrants who are not instructors, should you may need to elaborate on the conditional logic for the validation, probably checking again for the $_GET['register']
variable.
EDIT
Here's my best guess at how to add the user meta to the WooCommerce fields. However, there's no control over what the value displays, so 1. you could either adjust how you save the instructor
meta... save it as 'yes' or 'instructor' or something. OR, add your own 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' );
Upvotes: 2