添加电子邮件和名字以重置密码电子邮件woocommerce

时间:2019-10-17 06:58:40

标签: php wordpress woocommerce

根据文档和我发现以下代码的一些帖子,尝试将名称和电子邮件添加到重置密码电子邮件中应该可以,但不能。

<?php 
    do_action( 'woocommerce_email_header', $email_heading, $email );
    $user = get_user_by('login', $user_login);

    printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $user->user_firstname ) );
    printf( esc_html__( 'User email: %s', 'woocommerce' ), esc_html( $user->user_email) );

?>

电子邮件工作正常,但是我找不到正确的语法来获取名字和姓氏。

我在做什么错?我想念什么

谢谢

1 个答案:

答案 0 :(得分:1)

您的代码工作正常,我已经测试过。您已经拥有了用户对象:

$user = get_user_by('login', $user_login);

因此,您只需要添加:

//get first and last name
$user_firstname = $user->user_firstname;
$user_lastname = $user->user_lastname;

//display first and last name
echo $user_firstname;
echo $user_lastname;

//to securely output the first and last name with standard e-mail format of Woocommerce
<?php printf("Customer first name: %s", $user_firstname); ?>
<?php printf("Customer last name: %s", $user_lastname); ?>
//or use esc_html() with html tags to output in your own style

这是WooCommerce的标准重置密码电子邮件内容,仅供参考:

<?php
/**
 * Customer Reset Password email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-reset-password.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates/Emails
 * @version 3.5.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

?>

<?php do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<?php /* translators: %s: Customer first name */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $user_login ) ); ?>
<?php /* translators: %s: Store name */ ?>
<p><?php printf( esc_html__( 'Someone has requested a new password for the following account on %s:', 'woocommerce' ), esc_html( wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) ) ); ?></p>
<?php /* translators: %s Customer username */ ?>
<p><?php printf( esc_html__( 'Username: %s', 'woocommerce' ), esc_html( $user_login ) ); ?></p>
<p><?php esc_html_e( 'If you didn\'t make this request, just ignore this email. If you\'d like to proceed:', 'woocommerce' ); ?></p>
<p>
    <a class="link" href="<?php echo esc_url( add_query_arg( array( 'key' => $reset_key, 'id' => $user_id ), wc_get_endpoint_url( 'lost-password', '', wc_get_page_permalink( 'myaccount' ) ) ) ); ?>"><?php // phpcs:ignore ?>
        <?php esc_html_e( 'Click here to reset your password', 'woocommerce' ); ?>
    </a>
</p>
<p><?php esc_html_e( 'Thanks for reading.', 'woocommerce' ); ?></p>

<?php do_action( 'woocommerce_email_footer', $email ); ?>