Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding first name and last name to new account email notification in Woocommerce

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]?

like image 260
bobfelstead Avatar asked Mar 06 '23 13:03

bobfelstead


1 Answers

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>
like image 69
LoicTheAztec Avatar answered Mar 08 '23 21:03

LoicTheAztec