Displaying a Welcome Message to a Woocommerce User

In the registration fields, I’ve introduced an two additional fields that take the First name and last name of the registering user. In my woocommerce-functions.php I define (user_firstname/lastname) as the following:

function woocommerce_process_registration() {
global $woocommerce, $current_user;

if ( ! empty( $_POST['register'] ) ) {

    $woocommerce->verify_nonce( 'register' );

    // Get fields
    $user_email = isset( $_POST['email'] ) ? trim( $_POST['email'] ) : '';
    $password   = isset( $_POST['password'] ) ? trim( $_POST['password'] ) : '';
    $password2  = isset( $_POST['password2'] ) ? trim( $_POST['password2'] ) : '';
    $user_email = apply_filters( 'user_registration_email', $user_email );
    $user_firstname = isset( $_POST['firstname'] ) ? trim( $_POST['firstname'] ) : '';
    $user_lastname = isset( $_POST['lastname'] ) ? trim( $_POST['lastname'] ) : '';

That being said, how would I add a welcome message to my header.php that uses the format,

Read More
<?php global $user_identity;
get_currentuserinfo();

if ($user_identity == '') {
echo('Welcome Guest');
} else {
echo('Welcome ' . $user_identity);
}
?>

Thanks in advance!

Related posts

1 comment

  1. This is not a WooCommerce question. Once the user is registered you use WordPress functions to verify whether the user is logged in and to retrieve the user’s info. Assuming you are saving the user info correctly, then this should show a user’s first name to the user, and a generic message to a non-logged-in user.

    if ( is_user_logged_in() ) {
        $current_user = wp_get_current_user();
        echo 'Welcome, ' . $current_user->user_firstname . '!';
    } else {
        echo 'Welcome, visitor!';
    }
    

    See the Codex for references:

    is_user_logged_in()
    wp_get_current_user()

Comments are closed.