douci6541 2018-05-16 18:20
浏览 113
已采纳

通过Woocommerce“新帐户”电子邮件通知模板获取用户电子邮件

In Woocommerce I am trying to customize the "New account" email notification template changing the texts.

Currently the default template shows the username but intead I want it to show the user email.

I thought I needed to use something like $user_email but that shows up blank (or empty).

How to get the user email in Woocommerce "New account" email notification template?

That is the customized template that I got where I need to get the user email:

<?php
/**
 * Customer new account email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-new-account.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/
 * @author      WooThemes
 * @package     WooCommerce/Templates/Emails
 * @version     1.6.4
 */

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

?>

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

    <p><?php printf( __( 'Thanks for creating an account on %1$s. Your registered email is %2$s', 'woocommerce' ), esc_html( $blogname ), '<strong>' . esc_html( $user_email ) . '</strong>' ); ?></p>

<?php if ( 'yes' === get_option( 'woocommerce_registration_generate_password' ) && $password_generated ) : ?>

    <p><?php printf( __( 'Your password has been automatically generated: %s', 'woocommerce' ), '<strong>' . esc_html( $user_pass ) . '</strong>' ); ?></p>

<?php endif; ?>

    <p><?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>

<?php do_action( 'woocommerce_email_footer', $email );
  • 写回答

1条回答 默认 最新

  • doujiang2643 2018-05-16 23:54
    关注

    In this template, the $user_login is defined and available. What you can do is to use the WordPress function get_user_by() with it to get the user email.

    So in the template you will have just to replace this line:

        <p><?php printf( __( 'Thanks for creating an account on %1$s. Your registered email is %2$s', 'woocommerce' ), esc_html( $blogname ), '<strong>' . esc_html( $user->user_email ) . '</strong>' ); ?></p>
    

    By this instead:

    <?php $user = get_user_by('login', $user_login );
    
    if( $user && isset($user->user_email) ): ?>
    
        <p><?php printf( __( 'Thanks for creating an account on %1$s. Your registered email is %2$s', 'woocommerce' ), esc_html( $blogname ), '<strong>' . esc_html( $user->user_email ) . '</strong>' ); ?></p>
    
    <?php else: ?>
    
        <p><?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 endif; ?>
    

    This way you will be sure to don't get any error and you will have the default woocommerce alternative.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集