Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jasperf/fa980ef3063dcd959553b069e2eaccee to your computer and use it in GitHub Desktop.
Save jasperf/fa980ef3063dcd959553b069e2eaccee to your computer and use it in GitHub Desktop.
Add Custom Fields to WooCommerce Registration
add_action( 'woocommerce_register_form', 'wc_extra_registation_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
}
// Validate WooCommerce registration form custom fields.
add_action( 'woocommerce_register_post', 'wc_validate_reg_form_fields', 10, 3 );
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment