简体   繁体   English

在 Woocommerce 中将名字和姓氏添加到新帐户电子邮件通知

[英]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.最终,我最终查看了 Codex,因为 woocommerce 注册详细信息已保存为用户帐户。 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?问题似乎是 Woocommerce 在用户登录之前发送了新客户电子邮件 - 尽管我可能在这里遗漏了什么? This is the last method tried, but it yields a 0 in User ID, essentially user not logged in , hence my previous comment.这是最后尝试的方法,但它在用户 ID 中产生 0,本质上是用户未登录,因此是我之前的评论。 Here's the code that I'm using merged into the default woocommerce template...这是我正在使用的合并到默认 woocommerce 模板中的代码...

        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:someone@something.com">someone@something.com</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.结果 - 'Hi Welcome to..' 缺少名字。 Is there something I've missed here, or do i need to capture the POSTed variable - POST[billing_first_name]?我在这里错过了什么,还是我需要捕获 POSTed 变量 - POST [billing_first_name]?

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:相反,由于在此模板中定义了变量$user_login ,您可以使用它来获取WP_User对象替换:

$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.emails/customer-new-account.php模板文件上测试和工作。 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>

I just added this in the customer-new-account.php (under the line "do_action(...)") and it worked for me :我刚刚在 customer-new-account.php 中添加了这个(在“do_action(...)”行下),它对我有用:

$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.当然,您需要确保在创建帐户时将这些字段保存在正确的元字段中。

below method works for me下面的方法对我有用

In theme functions.php add below code.在主题functions.php 中添加以下代码。

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接下来在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM