如何在WooCommerce‘新账户’邮件通知模板中获取用户邮箱?
解决WooCommerce新账户邮件模板中获取用户邮箱的问题
我明白你遇到的困扰了——在自定义customer-new-account.php模板时,直接用$user_email会显示空值,这是因为这个变量默认并没有被WooCommerce作为独立变量传递到模板里。不过别担心,有两种可靠的方法可以获取到用户邮箱:
方法1:通过$user对象提取邮箱
在这个邮件模板中,WooCommerce会自动传递一个$user变量(这是WP_User类的实例,包含了用户的所有核心数据),你可以直接从这个对象里获取邮箱地址:
<?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->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 ); ?>
方法2:通过$email对象的收件人属性获取
模板里的$email变量是WooCommerce邮件类的实例,它的recipient属性就是当前邮件的收件邮箱(也就是用户的注册邮箱),直接调用这个属性也能拿到正确的邮箱:
<?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( $email->recipient ) . '</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 ); ?>
补充说明:为什么$user_email会为空?
默认情况下,WooCommerce在加载这个模板时,并没有把$user_email作为独立变量传递进来,而是把用户的所有数据封装在$user对象中,所以直接调用$user_email自然会得到空值。上面两种方法都是利用模板中已有的可用变量来获取邮箱,不会出现空值问题。
内容的提问来源于stack exchange,提问作者user9795198
相关产品推荐
相关产品推荐

