duanrong0738 2017-11-15 07:37
浏览 124
已采纳

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

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. I get the user id when I use this code in any php template.

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

  • 写回答

2条回答 默认 最新

  • douqiaoru2583 2017-11-15 11:36
    关注

    You can't get the current user in woocommerce email templates, but you can get the customer user ID from the Order.

    To get the customer User ID, you can set and get some data in the $GLOBAL variable:

    • Set the data - On your active theme function.php file:
    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化