Aggiungere campi alla Registrazione Woocommerce

Woocommerce è uno dei plugin più potenti che WordPress mette a disposizione a chi vuole una piattaforma e-commerce potente e completamente personalizzabile.
All’interno della pagina registrazione però permette solo di inserire Username, Password, Email.
Con questa breve guida saremo ingrado di mettere tutti i campi che vogliamo.
Prendiamo dal nostro sito il file functions.php alla fine del file dopo “?>” inseriamo la prima parte di codice (vi ricordo che il file functions.php si trova dentro la cartella \wp-content\themes\”tema utilizzato”):

[showhide type=”codici” more_text=”Apri per vedere il codice” less_text=”chiudi” hidden=”yes”]

<?php function wooc_extra_register_fields() {?>
<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'] ); ?>" />
<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'] ); ?>" />
<label for="reg_billing_address_1">
<?php _e( 'Address', 'woocommerce' ); ?>
</label>
<input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php if ( ! empty( $_POST['billing_address_1'] ) ) esc_attr_e( $_POST['billing_address_1'] ); ?>" />
<label for="reg_billing_state">
<?php _e( 'Provincia', 'woocommerce' ); ?>
</label>
<input type="text" class="input-text" name="billing_state" id="reg_billing_state" value="<?php if ( ! empty( $_POST['billing_state'] ) ) esc_attr_e( $_POST['billing_state'] ); ?>" />
<label for="reg_billing_city">
<?php _e( 'Città', 'woocommerce' ); ?>
</label>
<input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php if ( ! empty( $_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" />
<label for="reg_billing_postcode">
<?php _e( 'C.A.P.', 'woocommerce' ); ?>
</label><input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php if ( ! empty( $_POST['billing_postcode'] ) ) esc_attr_e( $_POST['billing_postcode'] ); ?>" />
<label for="reg_billing_phone">
<?php _e( 'Phone', 'woocommerce' ); ?>
<span class="required">*</span>
</label>
<input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] )) esc_attr_e( $_POST['billing_phone'] ); ?>" />
<div class="clear">
</div>
<?php
 }
 add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );

[/showhide]
[hr]
Controllate bene il codice eventuali impurità vi potrebbero far apparire una schermata registrazione non allineata bene.
Con questa parte di codice che abbiamo inserito andremo a visualizzare nella pagina registrazione i campi Nome, Cognome, Indirizzo, Provincia, Città, C.A.P., Telefono
[EasyGallery key=’2′]

[hr]


Ora dobbiamo inserire la parte di codice che riguarda i campi obbligatori Nome, Cognome e Telefono
[showhide type=”codice2″ more_text=”Apri per vedere il codice” less_text=”chiudi” hidden=”yes”]

/**
* Validate the extra register fields.
*
* @param WP_Error $validation_errors Errors.
* @param string $username Current username.
* @param string $email Current email.
*
* @return WP_Error
*/
function wooc_validate_extra_register_fields( $errors, $username, $email ) {
if ( isset( $_POST['billing_first_name'] ) &amp;&amp; empty( $_POST['billing_first_name'] ) ) {
$errors-&gt;add( 'billing_first_name_error', __( 'Inserisci il Nome', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) &amp;&amp; empty( $_POST['billing_last_name'] ) ) {
$errors-&gt;add( 'billing_last_name_error', __( 'Inserisci il Cognome', 'woocommerce' ) );
}
if ( isset( $_POST['billing_phone'] ) &amp;&amp; empty( $_POST['billing_phone'] ) ) {
$errors-&gt;add( 'billing_phone_error', __( 'Inserisci il numero di telefono', 'woocommerce' ) );
}
return $errors;
}
add_filter( 'woocommerce_registration_errors', 'wooc_validate_extra_register_fields', 10, 3 );

[/showhide]

[hr]
Eseguito il controllo dei campi obbligatori (ovviamente possiamo mettere tutti i campi che vogliamo, il codice è abbastanza intuitivo) passiamo all’inserimento dei campi controllati nel nostro database
[showhide type=”codice3″ more_text=”Apri per vedere il codice” less_text=”chiudi” hidden=”yes”]

 /**
* Below code save extra fields.
*/

function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_phone'] ) ) {
                 // Phone input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
          }
		  
    if ( isset( $_POST['billing_address_1'] ) ) {
                 // Address input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
          }
		      if ( isset( $_POST['billing_state'] ) ) {
                 // Address input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_state', sanitize_text_field( $_POST['billing_state'] ) );
          }
		  if ( isset( $_POST['billing_city'] ) ) {
                 // Address input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
          }
		  if ( isset( $_POST['billing_postcode'] ) ) {
                 // Address input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
          }
		  
      if ( isset( $_POST['billing_first_name'] ) ) {
             //First name field which is by default
             update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
             // First name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
      }
      if ( isset( $_POST['billing_last_name'] ) ) {
             // Last name field which is by default
             update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
             // Last name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
      }

}

function my_woocommerce_before_checkout_process() {
    remove_filter( 'woocommerce_registration_errors', array('WC_Ncr_Registration_Captcha', 'validate_captcha_wc_registration'), 10 );
}
add_action('woocommerce_before_checkout_process', 'my_woocommerce_before_checkout_process');

?>

[/showhide]

[hr]
Ora i vostri clienti si possono registrare al sito direttamente dalla pagina di Woocommerce!

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *