Custom registration fields
<?php
function wooc_extra_register_fields() { ?>
<p class="form-row form-row-first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_company"><?php _e( 'Company name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php if ( ! empty( $_POST['billing_company'] ) ) esc_attr_e( $_POST['billing_company'] ); ?>" />
</p>
<p class="form-row form-row-first">
<label for="reg_vat_number"><?php _e( 'VAT number', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="vat_number" id="reg_vat_number" value="<?php if ( ! empty( $_POST['vat_number'] ) ) esc_attr_e( $_POST['vat_number'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="reg_commerce_number"><?php _e( 'Chamber of Commerce', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="commerce_number" id="reg_commerce_number" value="<?php if ( ! empty( $_POST['commerce_number'] ) ) esc_attr_e( $_POST['commerce_number'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?></label>
<input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action( 'woocommerce_register_form_start', __NAMESPACE__ . '\\wooc_extra_register_fields' );
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: Voornaam is verplicht', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Achternaam is verplicht', 'woocommerce' ) );
}
if ( isset( $_POST['billing_company'] ) && empty( $_POST['billing_company'] ) ) {
$validation_errors->add( 'billing_company_error', __( '<strong>Error</strong>: Bedrijfsnaam is verplicht', 'woocommerce' ) );
}
if ( isset( $_POST['vat_number'] ) && empty( $_POST['vat_number'] ) ) {
$validation_errors->add( 'vat_number_error', __( '<strong>Error</strong>: BTW nummer is verplicht', 'woocommerce' ) );
}
if ( isset( $_POST['commerce_number'] ) && empty( $_POST['commerce_number'] ) ) {
$validation_errors->add( 'commerce_number_error', __( '<strong>Error</strong>: Kvk nummer is verplicht', 'woocommerce' ) );
}
return $validation_errors;
}
add_action( 'woocommerce_register_post', __NAMESPACE__ . '\\wooc_validate_extra_register_fields', 10, 3 );
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
if ( isset( $_POST['billing_company'] ) ) {
update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
}
if ( isset( $_POST['vat_number'] ) ) {
update_user_meta( $customer_id, 'vat_number', sanitize_text_field( $_POST['vat_number'] ) );
}
if ( isset( $_POST['commerce_number'] ) ) {
update_user_meta( $customer_id, 'commerce_number', sanitize_text_field( $_POST['commerce_number'] ) );
}
if ( isset( $_POST['billing_phone'] ) ) {
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
}
add_action( 'woocommerce_created_customer', __NAMESPACE__ . '\\wooc_save_extra_register_fields' );