• Resolved KoolPal

    (@koolpal)


    Hi,

    I am a newbie trying to learn. I am on Woocommerce 3.0.6 with Flatsome Theme

    I have copied the woocommerce email templates into my child theme folder and I am editing them there.

    I am trying to insert Customer’s first name in all emails and have succeeded in order emails by using the below code: <p><?php printf( __( “Hello %s”, ‘woocommerce’ ), $order->billing_first_name ); ?></p>

    I even managed to modify the New Account Email to get the customer email rather than his account name using the code below:

    // load customer data and user email into email template
    $user_email = $user_login;
    $user       = get_user_by('login', $user_login);
    if ( $user ) {
        $user_email = $user->user_email;
    }
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }
    ?>
    
    <?php do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
    
            <p><?php printf( __( 'Hello %s,', 'woocommerce' ), esc_html($billing_first_name )); ?></p>
        
        <p><?php printf( __( 'Thanks for creating an account on %1$s. <br><br>
        Your username has been set as %2$s', 'woocommerce' ), esc_html( $blogname ), '<strong>' . esc_html( $user_email ) . '</strong>' ); ?></p>}

    However, this code is not working in New Account Email to populate the First Name.

    It would be great if anyone can guide me with the required code to get the Customer’s First name into the New Account Email.

    P.S. Please do not suggest to install any plugin. I would rather learn with your expert guidance.

    Thanks a lot!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter KoolPal

    (@koolpal)

    Hating to bump this but some guidance would be appreciated!

    Thanks

    Thread Starter KoolPal

    (@koolpal)

    OK! I found the solution.

    First I learnt that first name & last name is not stored in wp_users but in wp_usermeta

    Next I learnt (painfully – no decent WordPress documentation anywhere!) was that user metadata is not saved at the point when the “new user registration” email is sent!! (I am not sure of the logic to be in a hurry to shoot of this email without saving all data first!)

    Since I did not want to mess with WP Core, I used the below snippet to populate display_name with First Name (space) Last Name and then use this in the Welcome email.

    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;
    }
    

    In case you want to store only the first name in display_name, change the line above:
    $name = $firstname . ' ' . $lastname;
    to
    $name = $firstname;

    Next I added the below line in the customer-new-account.php

    <?php do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
    
    	<!-- Added by me-->
    	<p><?php printf( __( "Hello %s,", 'woocommerce' ), $user->display_name ); ?></p>
    

    And we finally have a decent welcome mail addressed to the customer!

    P.S. I am marking this as solved since this has solved the issue for me. The team monitoring this forum can take the credit for ‘solving’ another thread!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘New Account email – Insert First Name’ is closed to new replies.