php 从 WooCommerce 中的订单 ID 获取客户 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43814132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the customer ID from an order ID in WooCommerce
提问by Fluke Fox
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
我想通过订单获取客户的“mycred”余额,同时使用 WP ALL Export 将基于订单的客户余额导出到电子表格。其实很可能很简单。我可以获取订单 ID,但不能获取客户 ID
Here is what I'm doing to test if I can get the customer 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;
}
This returns a 0.
这将返回 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;
}
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.
这将返回客户的订单号,这很好,但只是成功了一半。我现在想要客户 ID,所以我调用 mycred balance 函数来显示他们的余额。
Any ideas? I'm a newbie at php and probably very bad.
有任何想法吗?我是 php 的新手,可能非常糟糕。
回答by LoicTheAztec
To get the User ID from the Order ID, you can use many ways, Here are 2 of them:
要从订单 ID 中获取用户 ID,可以使用多种方法,以下是其中 2 种:
In WooCommerce 2.5 to 3.0+ you can use get_post_meta()
function this way:
在 WooCommerce 2.5 到 3.0+ 中,您可以这样使用get_post_meta()
函数:
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;
}
In WooCommerce 3.0+ you can use WC_Order
Class methodsthis way:
在 WooCommerce 3.0+ 中,您可以这样使用WC_Order
Class 方法:
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;
}
回答by Fluke Fox
For those who want to specifically add the customer mycred balance from an ORDER into the CSV sheet within WP All Export here is the bit of code I used. Thank you for your help getting it solved.
对于那些想要专门将订单中的客户 mycred 余额添加到 WP All Export 中的 CSV 表中的人,这里是我使用的一些代码。感谢您帮助解决问题。
While editing an ORDER export in WP ALL EXPORT, add a new data object and click on it and "Export the value returned by a PHP function" then add the following function in the code editor:
在 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;
}
Then make sure to add the "all_export_mycred" to the php return field.
然后确保将“all_export_mycred”添加到 php 返回字段。