简体   繁体   English

在Woocommerce电子邮件页脚模板中获取客户user_id

[英]Get the customer user_id in Woocommerce email footer template

I am trying to get current_user_id in woocommerce custom email template but it returns 0. Does anyone know how to get user_id in email template in wordpress. 我试图在woocommerce自定义电子邮件模板中获取current_user_id,但它返回0。有人知道如何在wordpress中获取电子邮件模板中的user_id。 I get the user id when I use this code in any php template. 当我在任何php模板中使用此代码时,都会得到用户ID。

Below is my code: 下面是我的代码:

<?php global $current_user;
      get_currentuserinfo();
      echo '<h4>Your code:</h3>' . $current_user->ID . '';
?>

Here is complete template code link: email-template-code 这是完整的模板代码链接: email-template-code

You can't get the current user in woocommerce email templates, but you can get the customer user ID from the Order. 您无法在woocommerce电子邮件模板中获取当前用户 ,但可以从订单中获取客户用户ID

To get the customer User ID, you can set and get some data in the $GLOBAL variable: 要获取客户用户标识,可以在$GLOBAL变量中设置并获取一些数据:

  • Set the data - On your active theme function.php file: 设置数据 -在活动主题function.php文件上:
add_action( 'woocommerce_email_header', 'wc_email_user_id', 10, 2 );
function wc_email_user_id( $email_heading, $email ) {
    // Set global variable data: user ID and Order ID
    $GLOBALS['emails_custom_data'] = array(
        'user_id' => get_post_meta( $email->object->ID, '_customer_user', true ), // Set the user ID
        'order_id' => $email->object->ID, // Set the Order ID
    );
}
  • Get the data - On your template file just after: 获取数据 -紧接着在模板文件上:
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

## --- Your custom Code start here --- ##

$refNameGlobalsVar = $GLOBALS; // Get the data
$custom_data = $refNameGlobalsVar['emails_custom_data']; // Set the data in a variable

$user_id = $custom_data['user_id']; // Get the user ID
$order_id = $custom_data['order_id']; // Get the order ID (in case of…)

// Testing output
echo '<p>The user ID is '.$user_id.' | From the Order ID: '.$order_id.'</p>';

## --- End of custom code --- ##

?>

This code is tested and works 此代码已经过测试并可以正常工作

I think you should use wordpress hooks like you already using in your email template: 我认为您应该使用已在电子邮件模板中使用过的wordpress挂钩:

apply_filters( 'woocommerce_email_footer_text', get_option( 'woocommerce_email_footer_text' )

Instead of directly writing like this 而不是像这样直接写

global $current_user;
get_currentuserinfo();
echo '<h4>Your code:</h3>' . $current_user->ID . '';

You could apply filter like this 您可以像这样应用过滤器

global $current_user;
apply_filters( 'woocommerce_custom_template_email', $current_user->ID );

And when email function responsible for sending emails is triggered you use add_filter like this 当负责发送电子邮件的电子邮件功能被触发时,您可以使用add_filter这样的

add_filters( 'woocommerce_custom_template_email', 'custom_email_function' );
function custom_email_function($user_id) {
    global $current_user;
    $user_id = $current_user->ID;
    return $user_id;
}

Maybe in this way you can get current user id. 也许通过这种方式,您可以获得当前的用户ID。 Alse make shure that you are trying to get current user id for logged in users. 还请确保您正在尝试获取登录用户的当前用户ID。

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

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