Modificare la pagina di registrazione di WooCommerce

Mattepuffo's logo
Modificare la pagina di registrazione di WooCommerce

Modificare la pagina di registrazione di WooCommerce

WooCommerce è probabilmene il plugin e-commerce più usato di WordPress.

Tra le varie cose, gestisce anche la registrazione degli utenti; ma di default richiede solo email e password.

Vediamo come modificarlo per richiedere anche nome e cognome.

Andate Aspetto -> Editor, e aprite il file functions.php.

Andate alla fine e aggiungete queste righe:

/**
 * Add new register fields for WooCommerce registration.
 *
 * @return string Register fields HTML.
 */
function wooc_extra_register_fields() {
    ?>
    <p class="form-row form-row-wide form-group">
        <input type="text" class="input-text form-control" name="billing_first_name" id="reg_billing_first_name" required value="<?php if (!empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>" placeholder="Nome / Name *" />
    </p>
    <p class="form-row form-row-wide form-group">
        <input type="text" class="input-text form-control" name="billing_last_name" id="reg_billing_last_name" required value="<?php if (!empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" placeholder="Cognome / Surname *" />
    </p>
    <?php
}

add_action('woocommerce_register_form_start', 'wooc_extra_register_fields');

Così abbiamo aggiunto dei campi al form di registrazione; rinfrescate la pagina per vedere il risultato.

A questo punto, subito sotto aggiungete queste righe:

/**
* Validate the extra register fields.
*
* @param string $username           Current username.
* @param string $email              Current email.
* @param object $validation_errors  WP_Error object.
*
* @return void
*/
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>: First name is required!', 'woocommerce' ) );
       }
       if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
              $validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
       }
}

add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );

Così abbiamo impostato la validazione, lato server, dei campi.

Infine, ancora sotto, aggiungete questo:

/**
* Save the extra register fields.
*
* @paramint $customer_id Current customer ID.
*
* @return void
*/
function wooc_save_extra_register_fields( $customer_id ) {
       if ( isset( $_POST['billing_first_name'] ) ) {
              // WordPress default first name field.
              update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
              // WooCommerce billing first name.
              update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
       }

       if ( isset( $_POST['billing_last_name'] ) ) {
              // WordPress default last name field.
              update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
              // WooCommerce billing last name.
              update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
       }

}

add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

In questo modo i campi riempiti verranno salvati nel database.

Se infatti le omettete, la registrazione andrà a buona fine, ma nome e congome non verranno salvati.

Le classi CSS usate dipendo, ovviamente, anche dal vostro template.

E, se non siete sicuri, fate una copia di backup del file functions.php per ogni evenienza.

Enjoy!


Condividi

Commentami!