4
votes

My particular problem is adding first and last names to the Customer new account email, and I have tried various methods to do so. Eventually, I ended up looking at the Codex, since woocommerce registration details are saved as user accounts. The problem appears to be that Woocommerce, sends the New Customer email before the user is logged in - although I could be missing something here? This is the last method tried, but it yields a 0 in User ID, essentially user not logged in, hence my previous comment. Here's the code that I'm using merged into the default woocommerce template...

        if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }

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

    <?php
        $current_user = wp_get_current_user();
        /**
         * Get current logged in user detail from codex
         */
        echo 'Username: ' . $current_user->user_login . '<br />';
        echo 'User email: ' . $current_user->user_email . '<br />';
        echo 'User first name: ' . $current_user->user_firstname . '<br />';
        echo 'User last name: ' . $current_user->user_lastname . '<br />';
        echo 'User display name: ' . $current_user->display_name . '<br />';
        echo 'User ID: ' . $current_user->ID . '<br />';
    ?>

        <!--<img src = "image-name" style="margin-right:0px;">-->
        <h3 style="text-align:center;" ><?php echo 'Hi '. $current_user->user_firstname . ', welcome to.'; ?></h3>
        <p style="text-align:center;" >Thanks for signing up to our online service.</p>
        <h4 style="text-align:center; color:#289b38; ">Getting in touch  with us.</h4>
        <p style="text-align:center;"><?php printf( __( 'Contact us by phone on: %1$s, or by email to %2$s'), '<strong style="color:#289b38;">0999 999 999</strong>', '<strong><a href="mailto:[email protected]">[email protected]</a></strong>' ); ?></h4>
        <h4 style="text-align:center; color:#2f51a8;">Your account details.</h4>

        <p style="text-align:center;" ><?php printf( __( 'Thanks for creating an account on %1$s. Your username is %2$s', 'woocommerce' ), esc_html( $blogname ), '<strong>' . esc_html( $user_login ) . '</strong>' ); ?></p>

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

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

    <?php endif; ?>

        <p style="text-align:center;" ><?php printf( __( 'You can access your account area to view your orders and change your password here: %s.', 'woocommerce' ), make_clickable( esc_url( wc_get_page_permalink( 'myaccount' ) ) ) ); ?></p>

        <p style="text-align:center;" >Best wishes<p>
        <p style="text-align:center;" >Company name</p>
        <br>
    <?php do_action( 'woocommerce_email_footer', $email );

The result - 'Hi welcome to..' missing the first name. Is there something I've missed here, or do i need to capture the POSTed variable - POST[billing_first_name]?

Help would be very much appreciated.

Bob

3

3 Answers

7
votes

You can't get the current user as it's not a frontend hook. Instead as the variable $user_login is defined in this template, you can use it to get the WP_User object replacing:

$current_user = wp_get_current_user();

by:

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

Tested and works on emails/customer-new-account.php template file. So now you will be able to display the first name and the last name, if they exist as user meta data, with the following code to insert in this template:

<?php $user = get_user_by('login', $user_login ); ?>
<p><?php 
    _e('First name'); ?>: <?php echo $user->first_name; ?><br /><?php 
    _e('First name'); ?>: <?php echo $user->last_name; 
?></p>
0
votes

below method works for me

In theme functions.php add below code.

add_filter('pre_user_display_name','default_display_name');
function default_display_name($name) {
if ( isset( $_POST['billing_first_name'] ) ) {
$firstname = sanitize_text_field( $_POST['billing_first_name'] );
}
if ( isset( $_POST['billing_last_name'] ) ) {
$lastname = sanitize_text_field( $_POST['billing_last_name'] );
}
$name = $firstname . ' ' . $lastname;
return $name;
}

Next add the below code in the customer-new-account.php

$user       = get_user_by('login', $user_login);
<p><?php printf( esc_html__( '%s, Welcome to website!', 'woocommerce' ), esc_html( $user->display_name ) ); ?></p>
0
votes

I just added this in the customer-new-account.php (under the line "do_action(...)") and it worked for me :

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

// if you need the email address or something else
$email = $user->user_email;

// if you need an extra field from the user metas
$firstname = get_user_meta( $user->ID, 'billing_first_name' , true );
$lastname = get_user_meta( $user->ID, 'billing_last_name', true);

Of course, you need to make sure those fields are saved in the right meta field when the account is created.