如何从 WooCommerce 中的订单获取客户详细信息? [英] How can I get customer details from an order in WooCommerce?
问题描述
我有一个函数可以做到这一点:
I have a function that does this:
$order = new WC_Order($order_id);
$customer = new WC_Customer($order_id);
我如何从中获取客户详细信息?
How can I get customer details from this?
我已经尝试了文档中的所有内容,但不知何故,只提供了一些细节,而其余的则没有.例如.
I have tried everything in the documentation, but somehow, just some details are present, but the rest aren't. For example.
$data['Address'] = $customer->get_address() . ' ' . $customer->get_address_2();
$data['ZipCode'] = $customer->get_postcode();
为空.
做
var_dump($customer)
产生:
object(WC_Customer)#654 (2) { [_data":protected]=>数组(14){[国家"]=>字符串(2)IT">[状态"]=>字符串(0)"[邮政编码"]=>字符串(0)"[城市"]=>字符串(0)"[地址"]=>>字符串(0)"[address_2"]=>字符串(0)"[shipping_country"]=>string(2) IT"[shipping_state"]=>字符串(2)BG"[shipping_postcode"]=>字符串(0)"[shipping_city"]=>>字符串(0)"[shipping_address"]=>字符串(0)"[shipping_address_2"]=>字符串(0)"[is_vat_exempt"]=>bool(false) [calculated_shipping"]=>布尔(假)} ?[_changed":WC_Customer":私有]=>布尔(假)}
object(WC_Customer)#654 (2) { ["_data":protected]=> array(14) { ["country"]=> string(2) "IT" >["state"]=> string(0) "" ["postcode"]=> string(0) "" ["city"]=> string(0) "" ["address"]=> >string(0) "" ["address_2"]=> string(0) "" ["shipping_country"]=> string(2) "IT" ["shipping_state"]=> string(2) "BG" ["shipping_postcode"]=> string(0) "" ["shipping_city"]=> >string(0) "" ["shipping_address"]=> string(0) "" ["shipping_address_2"]=> string(0) "" ["is_vat_exempt"]=> bool(false) ["calculated_shipping"]=> bool(false) } ? ["_changed":"WC_Customer":private]=> bool(false) }
如您所见,城市存在,但其余的都是空的.我已经检查了 wp_usermeta
数据库表和客户的管理员面板,所有数据都在那里.
As you can see, the city is present, but the rest are empty. I have checked in wp_usermeta
database table and in the administrator panel of the customer and all the data is there.
推荐答案
尝试了 $customer = new WC_Customer();
和 global $woocommerce;$customer = $woocommerce->customer;
即使我以非管理员用户身份登录,我仍然收到空地址数据.
Having tried $customer = new WC_Customer();
and global $woocommerce; $customer = $woocommerce->customer;
I was still getting empty address data even when I logged in as a non-admin user.
我的解决方案如下:
function mwe_get_formatted_shipping_name_and_address($user_id) {
$address = '';
$address .= get_user_meta( $user_id, 'shipping_first_name', true );
$address .= ' ';
$address .= get_user_meta( $user_id, 'shipping_last_name', true );
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_company', true );
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_address_1', true );
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_address_2', true );
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_city', true );
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_state', true );
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_postcode', true );
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_country', true );
return $address;
}
...无论您是否以管理员身份登录,此代码都有效.
...and this code works regardless of whether you are logged in as admin or not.
这篇关于如何从 WooCommerce 中的订单获取客户详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!