• Resolved massidrink1976

    (@massidrink1976)


    Hello!
    I need to add the “username” field in the order confirmation email.

    I added this code in the file “admin-new-order”:

    do_action( ‘woocommerce_email_header’, $email_heading, $email ); ?>

    <?php /* translators: %s: Customer billing full name */ ?>
    <?php $user_info = get_userdata(1);
    echo ‘Kundennummer: ‘ . $user_info->ID . “\n”;
    ?>

    but something is wrong.

    In the confirmation email, “1” appears instead of my username “132064”

    I tried also another version:

    do_action( ‘woocommerce_email_header’, $email_heading, $email ); ?>

    <?php /* translators: %s: Customer billing full name */ ?>
    <?php $user_info = get_userdata(1);
    echo ‘Username: ‘ . $user_info->user_login . “\n”;

    ?>

    but in this case, appear “admin” and also is wrong.

    How can I fix?

    Regards,
    Massimiliano

Viewing 5 replies - 1 through 5 (of 5 total)
  • If you need user’s name name (not username), it would be the combination of $user_info->first_name and $user_info->last_name.

    So you need:

    echo 'Username: '.$user_info->first_name.' '.$user_info->last_name.'\n';

    madeincosmos

    (@madeincosmos)

    Automattic Happiness Engineer

    Hi @massidrink1976,

    I understand you’re looking for a way to add the ID number or login of the customer in WooCommerce emails, is that correct?

    The get_userdata function returns information about the user passed to this function as a parameter. So if you call get_userdata(1) it will return information about the user with ID number 1, which is usually the site admin–this is why you’re getting admin data always showing up there.

    What I’d recommend instead is to hook into the woocommerce_email_customer_details_fields filter that add your custom fields directly in the customer details section. Here’s a quick (untested) example of how you could do that:

    https://gist.github.com/madeincosmos/d947066af056fd033a3c2ed01b1c9eb6

    This function has access to the $order object and so is able to get customer ID number from the order directly. Then it adds an extra field to the list displayed in customer data section of the email.

    If you need more coding help, you may want to reach out to our customization partners here:

    https://woocommerce.com/customizations/

    Cheers!

    Thread Starter massidrink1976

    (@massidrink1976)

    Yes, is perfect! Done!
    The code works, but I need only username’s field.
    How can I delete ID number?

    Cheers!

    madeincosmos

    (@madeincosmos)

    Automattic Happiness Engineer

    Awesome, I’m glad to hear it’s working! The part in my code snippet that is adding user ID number is this:

     
    $fields['user_id'] = array( 
        'label' => __( 'User ID', 'woocommerce' ),  
        'value' => $user_id
      );   
    

    If you remove it, only username will be added to emails.

    Thread Starter massidrink1976

    (@massidrink1976)

    Thank you very much!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Username field in the order confirmation email’ is closed to new replies.