从WooCommerce中的订单ID获取客户ID

时间:2017-05-05 21:41:04

标签: php wordpress woocommerce orders customer

我想得到" mycred"通过订单平衡客户,同时使用WP ALL Export将客户余额根据订单导出到电子表格。它实际上可能很简单。我能够获得订单ID,但不能获得客户ID

以下是我要测试的是否能获得客户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。

但是,通过这样做,我可以轻松获得订单号:

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余额函数来显示他们的余额。

有什么想法吗?我是php的新手,可能非常糟糕。

3 个答案:

答案 0 :(得分:19)

要从订单ID获取用户ID,您可以使用多种方式,其中有两种方式:

在WooCommerce 2.5到3.0+中,您可以这样使用get_post_meta()功能:

function get_customerorderid(){
    global $post;
    $order_id = $post->ID;

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

    return $user_id;
}

在WooCommerce 3.0+中,您可以这样使用the class WC_Order methods

function get_customerorderid(){
    global $post;
    $order_id = $post->ID;

    // Get an instance of the WC_Order object
    $order = wc_get_order($order_id);

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

    return $user_id;
}

答案 1 :(得分:5)

对于那些想要将客户的mycred余额从ORDER专门添加到WP All Export中的CSV表格的人来说,这里是我使用的一些代码。 感谢您帮助解决问题。

在WP ALL EXPORT中编辑ORDER导出时,添加一个新数据对象并单击它,然后" 导出PHP函数返回的值"然后在代码编辑器中添加以下函数:

function all_export_mycred($balance)
{
    global $woocommerce, $post;

    $order = new WC_Order($post->ID);
    $user_id = $order->get_user_id( );

    $balance = mycred_get_users_balance( $user_id );

            return $balance;

    }

然后确保添加" all_export_mycred"到php返回字段。

答案 2 :(得分:0)

您可以使用订单号使用数据库查询获取相应的客户ID,google wordpress元查询

相关问题