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

标签 php wordpress templates woocommerce customer

我的特殊问题是将名字和姓氏添加到客户的新帐户电子邮件中,为此我尝试了各种方法。最终,我最终查看了 Codex,因为 woocommerce 注册详细信息已保存为用户帐户。问题似乎是 Woocommerce 在用户登录之前发送了新客户电子邮件 - 尽管我可能在这里遗漏了一些东西?这是最后尝试的方法,但它在用户 ID 中产生 0,基本上用户未登录,因此我之前的评论。这是我正在使用的代码合并到默认的 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 );

结果 - '嗨,欢迎来到..' 缺少名字。我在这里遗漏了什么,还是我需要捕获 POSTed 变量 - POST[billing_first_name]?

非常感谢帮助。

鲍勃

最佳答案

您无法获取当前用户,因为它不是前端 Hook 。而是作为变量 $user_login是在这个模板中定义的,你可以用它来获取WP_User对象替换:

$current_user = wp_get_current_user();

经过:
$current_user = get_user_by('login', $user_login ); 

已测试并适用于 emails/customer-new-account.php模板文件。所以现在您将能够显示名字和姓氏,如果它们作为用户元数据存在 ,使用以下代码插入此模板:
<?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>

关于php - 将名字和姓氏添加到 Woocommerce 中的新帐户电子邮件通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52079041/

相关文章:

使用 "Undefined variable:post"时出现 Php 错误 "get_post_thumbnail_id($post->ID)"

javascript - 如何使用 jQuery 循环访问 WordPress WP_Query 对象?

c++ - Visual Studio 中的模板化函数指针数组

c++ - 无法推导函数参数的非类型模板参数

php - Laravel 5 Session_driver 数据库 token 不匹配

php - 用于搜索的 Jquery 或 javascript

php - 如何通过文件夹名称或文件夹 ID 获取 Google 驱动器文件?

php - 拉拉维尔 5 : PHP variable shared by all methods of controller

django - 如何使用wordpress + django搭建网站

C++11模板别名作为模板模板参数导致不同的类型?