.
A user role selection is needed in WooCommerce registration form. Users should have a pull down menu to select between "customer" and "reseller" (IDs) wordpress role. Unfortunately my attempt to assemble code fails.
The following code does not assign the selected role and I'm stuck. Registered users still get default role "customer" although "reseller" was selected.
I have changed the code from this answer (I dont know whether this code was working anyway)
What I am doing wrong?
The code I am using:
/* To add WooCommerce registration form custom fields. */
function WC_extra_registation_fields() {?>
<p class="form-row form-row-first">
<label for="reg_role"><?php _e( 'Privat or commercial?', 'woocommerce' ); ?></label>
<select class="input-text" name="role" id="reg_role">
<option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'customer') esc_attr_e( 'selected' ); ?> value="customer">private</option>
<option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'reseller') esc_attr_e( 'selected' ); ?> value="reseller">commercial</option>
</select>
</p>
<?php
}
add_action( 'woocommerce_register_form', 'WC_extra_registation_fields');
/* To validate WooCommerce registration form custom fields. */
function WC_validate_reg_form_fields($username, $email, $validation_errors) {
if (isset($_POST['role']) && empty($_POST['role']) ) {
$validation_errors->add('role_error', __('Role required!', 'woocommerce'));
}
return $validation_errors;
}
add_action('woocommerce_register_post', 'WC_validate_reg_form_fields', 10, 3);
/* To save WooCommerce registration form custom fields. */
function WC_save_registration_form_fields($customer_id) {
//Role field
if (isset($_POST['role'])) {
update_user_meta($customer_id, 'role', sanitize_text_field($_POST['role']));
}
}
add_action('woocommerce_created_customer', 'WC_save_registration_form_fields');