从 WooCommerce 中的订单 ID 获取客户 ID [英] Get the customer ID from an order ID in WooCommerce

查看:72
本文介绍了从 WooCommerce 中的订单 ID 获取客户 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过订单获取客户的mycred"余额,同时使用 WP ALL Export 将基于订单的客户余额导出到电子表格.其实很可能很简单.我可以获取订单 ID,但不能获取客户 ID

I want to get the "mycred" balance of a customer through the order while using WP ALL Export to export the customer balance based on orders to a spreadsheet. It's actually probably quite simple. I'm able to get the Order ID, but not the Customer ID

这是我正在做的测试是否可以获得客户 ID:

Here is what I'm doing to test if I can get the customer ID:

function get_customeruserid($value)
{
  global $woocommerce, $post;

  $order = new WC_Order($post->ID);
  $order_id = $order->get_order_number();

  $customer = new WC_Customer($post->ID);
  $user_id = $customer->get_ID();

  $value = $user_id;
  return $value;
}

这将返回 0.

但是,通过这样做,我可以很容易地获得订单号:

However, I can get the order number easily enough by doing this:

function get_customerorderid($value)
{
  global $woocommerce, $post;

  $order = new WC_Order($post->ID);
  $order_id = $order->get_order_number();

  $value = $order_id;
  return $value;
}

这会返回客户的订单号,这很好,但只是成功了一半.我现在想要客户 ID,所以我调用 mycred balance 函数来显示他们的余额.

This returns the customer's order number which is great, but only half the battle. I now want the Customer ID so I call call the mycred balance function to show their balance.

有什么想法吗?我是 php 的新手,可能很糟糕.

Any ideas? I'm a newbie at php and probably very bad.

推荐答案

从 Order ID 中获取 User ID 的方法有很多,这里列举 2 种:在 WooCommerce 3.0+ 中,您可以使用 WC_Order 类方法 这样:

To get the User ID from the Order ID, you can use many ways, Here are 2 of them: In WooCommerce 3.0+ you can use WC_Order Class methods this way:

function get_customerorderid(){
    global $order, $post;

    if( ! is_a($order, 'WC_Order') ) {
        $order_id = $post->ID;

        // Get an instance of the WC_Order object
        $order = wc_get_order($order_id);
    } else {
        $order_id = $order->id;
    }

    // Get the user ID from WC_Order methods
    $user_id = $order->get_user_id(); // or $order->get_customer_id();

    return $user_id;
}

在 WooCommerce 3.0 版本之前,您可以使用 get_post_meta() 函数是这样的:

Before WooCommerce 3.0 version, you can use get_post_meta() function this way:

function get_customerorderid(){
    global $order, $post;

    if( ! is_a($order, 'WC_Order') ) {
        $order_id = $post->ID;
    } else {
        $order_id = $order->id;
    }

    // Get the user ID
    $user_id = get_post_meta($order_id, '_customer_user', true);

    return $user_id;
}

这篇关于从 WooCommerce 中的订单 ID 获取客户 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆