Aggiungere campi nel form di registrazione WOOCOMMERCE

Se devi aggiungere dei campi allo scarno menu di registrazione di woocommerce e in generale di wordpress bisogna modificare il file “functions.php” come di seguito indicato. Il fatto di essere riuscito ad inserire extra campi in registration form ha molto facilitato la registrazione.

“Se serve aggiungere campi (tipo p.iva, sdi) lo puoi fare con il plugin che si chiama: Checkout Field Editor for WooCommerce

Nel caso sotto riportato è stato utilizzato il plugin Checkout Field Editor for WooCommerce per aggiungere i campi P.Iva e Società oltre a quelli già presenti.

////////////////////////////////////////////////////////////////////
// REGISTRAZIONE UTENTE
////////////////////////////////////////////////////////////////////
function woocommerce_add_registration_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-first">
        <label for="billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_phone" id="billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
    </p>
    <p class="form-row form-row-last">
        <label for="reg_billing_company"><?php _e( 'Società', '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-last">
    <label for="reg_billing_piva"><?php _e( 'Partita Iva', 'woocommerce' ); ?><span class="required">*</span></label>
    <input type="text" class="input-text" name="billing_piva" id="reg_billing_piva" value="<?php if ( ! empty( $_POST['billing_piva'] ) ) esc_attr_e( $_POST['billing_piva'] ); ?>" />
    </p>
    <div class="clear"></div>
    <?php
}
add_action( 'woocommerce_register_form_start', 'woocommerce_add_registration_fields' );
function woocommerce_validate_extra_registration_fields( $username, $email, $validation_errors ) {
 
    if ( empty( $_POST['billing_first_name'] ) ) {
        $validation_errors->add( 'billing_first_name_error', __( 'Nome è richiesto!', 'woocommerce' ) );
    }
 
    if ( empty( $_POST['billing_last_name'] ) ) {
        $validation_errors->add( 'billing_last_name_error', __( 'Cognome è richiesto!', 'woocommerce' ) );
    }
 
    if( empty($_POST['billing_phone']) ) {
        $validation_errors->add( 'billing_phone_error', __( 'Telefono è richiesto!', 'woocommerce' ) );
    }
 
    if( empty($_POST['billing_company']) ) {
        $validation_errors->add( 'billing_company', __( 'Data di nascita è richiesta!', 'woocommerce' ) );
    }
	  if( empty($_POST['billing_piva']) ) {
        $validation_errors->add( 'billing_piva', __( 'La Partita Iva è richiesta!', 'woocommerce' ) );
    }
  
    return $validation_errors;
}
 
add_action( 'woocommerce_register_post', 'woocommerce_validate_extra_registration_fields', 10, 3 );
function woocommerce_save_extra_registration_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_phone'] ) ) {
        update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
    }
 
    if( isset($_POST['billing_company']) ) {
        update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
    }
	if( isset($_POST['billing_piva']) ) {
        update_user_meta( $customer_id, 'billing_piva', sanitize_text_field( $_POST['billing_piva'] ) );
    }
 }
add_action( 'woocommerce_created_customer', 'woocommerce_save_extra_registration_fields' );

Il RISULTATO:

Me lo merito un caffè? Dai clicca qui per offrirmelo.