skip to Main Content

We have a custom registration page for our WooCommerce site which is based on a Business Bloomer shortcode and added an input field for the first name of the user. The code within our functions.php looks like this:

/**
 * @snippet       WooCommerce User Registration Shortcode
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.6.5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

// THIS WILL CREATE A NEW SHORTCODE: [wc_reg_form_bbloomer]
add_shortcode('wc_reg_form_bbloomer', 'bbloomer_separate_registration_form');

function bbloomer_separate_registration_form()
{
    if (is_admin()) return;
    if (is_user_logged_in()) return;
    ob_start();

    // NOTE: THE FOLLOWING <FORM></FORM> IS COPIED FROM woocommercetemplatesmyaccountform-login.php
    // IF WOOCOMMERCE RELEASES AN UPDATE TO THAT TEMPLATE, YOU MUST CHANGE THIS ACCORDINGLY

?>

      <form method="post" class="woocommerce-form woocommerce-form-register register" <?php do_action('woocommerce_register_form_tag'); ?> >

            <?php do_action('woocommerce_register_form_start'); ?>

            <?php if ('no' === get_option('woocommerce_registration_generate_username')): ?>

                <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
                    <label for="reg_username"><?php esc_html_e('Username', 'woocommerce'); ?>&nbsp;<span class="required">*</span></label>
                    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="reg_username" autocomplete="username" value="<?php echo (!empty($_POST['username'])) ? esc_attr(wp_unslash($_POST['username'])) : ''; ?>" /><?php // @codingStandardsIgnoreLine
         ?>
                </p>

            <?php
    endif; ?>

            <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
                <label for="reg_email"><?php esc_html_e('Email address', 'woocommerce'); ?>&nbsp;<span class="required">*</span></label>
                <input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="email" id="reg_email" autocomplete="email" value="<?php echo (!empty($_POST['email'])) ? esc_attr(wp_unslash($_POST['email'])) : ''; ?>" /><?php // @codingStandardsIgnoreLine
     ?>
            </p>

            <?php if ('no' === get_option('woocommerce_registration_generate_password')): ?>

                <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
                    <label for="reg_password"><?php esc_html_e('Password', 'woocommerce'); ?>&nbsp;<span class="required">*</span></label>
                    <input type="password" class="woocommerce-Input woocommerce-Input--text input-text" name="password" id="reg_password" autocomplete="new-password" />
                </p>

            <?php
    else: ?>

                <p><?php esc_html_e('A password will be sent to your email address.', 'woocommerce'); ?></p>

            <?php
    endif; ?>

            <?php do_action('woocommerce_register_form'); ?>

            <p class="woocommerce-FormRow form-row">
                <?php wp_nonce_field('woocommerce-register', 'woocommerce-register-nonce'); ?>
                <button type="submit" class="woocommerce-Button woocommerce-button button woocommerce-form-register__submit" name="register" value="<?php esc_attr_e('Register', 'woocommerce'); ?>"><?php esc_html_e('Register', 'woocommerce'); ?></button>
            </p>

            <?php do_action('woocommerce_register_form_end'); ?>

        </form>

   <?php
    return ob_get_clean();
}

/**
 * @snippet       Add First to Register Form - WooCommerce
 * @sourcecode    https://businessbloomer.com/?p=21974
 * @author        Rodolfo Melogli
 * @credits       Claudio SM Web
 * @compatible    WC 3.5.2
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

///////////////////////////////
// 1. ADD FIELDS

add_action( 'woocommerce_register_form_start', 'bbloomer_add_name_woo_account_registration' );

function bbloomer_add_name_woo_account_registration() {
    ?>

    <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <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>

    <div class="clear"></div>

    <?php
}

///////////////////////////////
// 2. VALIDATE FIELDS

add_filter( 'woocommerce_registration_errors', 'bbloomer_validate_name_fields', 10, 3 );

function bbloomer_validate_name_fields( $errors, $username, $email ) {
    if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
        $errors->add( 'billing_first_name_error', __( '<strong>Achtung</strong>: Vorname ist ein Pflichtfeld!', 'woocommerce' ) );
    }
    return $errors;
}

///////////////////////////////
// 3. SAVE FIELDS

add_action( 'woocommerce_created_customer', 'bbloomer_save_name_fields' );

function bbloomer_save_name_fields( $customer_id ) {
    if ( isset( $_POST['billing_first_name'] ) ) {
        update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
        update_user_meta( $customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']) );
    }

}

Since our site is based in Germany we use the WooCommerce Germanized plugin with the DOI (double opt-in) functionality. So a user receives an activation mail which they have to click in order to active the account. Within the activation mail we want to have a more personal feeling, thus include the first name of the user. There we added the original email template from WooCommerce Germanized to our child theme.

The problem is that the only value I can access (is displayed in the actually delivered email) is $user_login which returns the automatically created username. How can I access the first name of the user which is saved as first name and billing first name. The template within our child theme looks like this:

<?php
/**
 * Customer new account activation email.
 *
 * @see https://github.com/vendidero/woocommerce-germanized/wiki/Overriding-Germanized-Templates
 * @package Germanized/Templates
 * @version 1.6.4
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

?>

<?php do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

    <p><?php printf( __( 'Hi %s,', 'woocommerce' ), esc_html( $user_login ) ); ?></p>

    <p><?php printf( __( "Thanks for creating an account on %s. Please follow the activation link to activate your account:", 'woocommerce-germanized' ), esc_html( $blogname ) ); ?></p>

    <p><a class="wc-button button"
          href="<?php echo esc_url( $user_activation_url ); ?>"><?php _e( 'Activate your account', 'woocommerce-germanized' ); ?></a>
    </p>

<?php if ( get_option( 'woocommerce_registration_generate_password' ) == 'yes' && $password_generated ) : ?>

    <p><?php printf( __( "Your password has been automatically generated: <strong>%s</strong>", 'woocommerce-germanized' ), esc_html( $user_pass ) ); ?></p>

<?php endif; ?>

    <p style="font-size:75%;"><?php printf( __( "If you haven't created an account on %s please ignore this email.", "woocommerce-germanized" ), esc_html( $blogname ) ); ?></p>

    <p style="font-size:75%;"><?php printf( __( 'If you cannot follow the link above please copy this url and paste it to your browser bar: %s', 'woocommerce-germanized' ), esc_url( $user_activation_url ) ); ?></p>

<?php
/**
 * Show user-defined additional content - this is set in each email's settings.
 */
if ( $additional_content ) {
    echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}
?>

<?php do_action( 'woocommerce_email_footer', $email ); ?>

I already tried to populate the first name as described here (https://wordpress.org/support/topic/new-account-email-insert-first-name/) and to access it via $user->display_name but this did not work.

I also tried the solution provided here (Get user meta data from woocommerce_created_customer hook in Woocommerce) to access first name via Customer object. The code would then look like below. I didn’t work either.

<p><?php $customer = new WC_Customer( $customer_id ); printf( __( 'Hi %s,', 'woocommerce' ), esc_html( $customer->get_first_name() ) ); ?></p>

Any help would be highly appreciated!

3

Answers


  1. First you need to find the user. I assume you don’t have access to the user ID, if you do you can skip the first line, and insert the user_id at the appropriate point.

    Then you need to get the first name from the meta data. According to your code you store it in the metadata field 'billing_first_name'. And then it’s a matter of inserting it in your email.

    <?php 
    
    $the_user = get_user_by('login', $user_login);
    $first_name = get_user_meta($the_user->ID, 'billing_first_name', true);
    
    ?>
    <p><?php printf( __( 'Hi %s,', 'woocommerce' ), esc_html( $first_name ) ); ?></p>
    
    Login or Signup to reply.
  2. Actually WooCommerce has that template on default, which includes username, email, myaccount url and much more, I dont think there will be difference between woc eng or grmn, except language.

    defined('ABSPATH') || exit;
    
    do_action('woocommerce_email_header', $email_heading, $email); ?>
    
    //To get user_name on registration you need to call get_user_by('id',$user_login); OR get_user_by('login',$user_login); OR get_user_by('user_name',$user_login);
    
    $user = get_user_by('id',$user_login);
    $first_name = get_user_meta($user,'first_name',true);
    
    //and call like this
    
    <p><?php printf(esc_html__('Hi %s,', 'woocommerce'), esc_html($first_name)); ?></p>
    //translators: %s: Customer username 
    <p><?php printf(esc_html__('Hi %s,', 'woocommerce'), esc_html( $user_login)); ?>
    //translators: %1$s: Site title, %2$s: Username, %3$s: My account link
    
    <p><?php printf(esc_html__('Thanks for creating an account on %1$s. Your username is %2$s. You can access your account area to view orders, change your password, and more at: %3$s', 'woocommerce'), esc_html($blogname), '<strong>' . esc_html( $user_login) . '</strong>', make_clickable(esc_url( wc_get_page_permalink('myaccount')))); 
    ?></p>
    
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
    
    <?php if ('yes' === get_option('woocommerce_registration_generate_password') && $password_generated) : ?>
    
    //translators: %s: Auto generated password
    
        <p><?php printf(esc_html__('Your password has been automatically generated: %s', 'woocommerce'), '<strong>' . esc_html($user_pass) .'</strong>'); ?></p>
    <?php endif; ?>
    
    <?php
    //Show user-defined additional content - this is set in each email's settings 
    
    if ($additional_content) {
        echo wp_kses_post(wpautop( wptexturize( $additional_content)));
    }
    
    do_action('woocommerce_email_footer', $email);
    ?>
    

    Here is the template https://github.com/woocommerce/woocommerce/blob/master/templates/emails/customer-new-account.php

    Login or Signup to reply.
  3. get the user information by login

    $customer = get_user_by('login', $user_login);

    and get the name using the user id

    $firstName = get_user_meta($customer->ID,'billing_first_name',true);

    if the user is not registered, so, you can use this

    esc_html( $firstName ) );

    instead to

    esc_html( $customer->get_first_name() ) );

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search