如何从WooCommerce中的订单获取客户详细信息? [英] How to get Customer details from 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 docs, but somehow, just some details are present but the rest arent, 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] => array(14){["country"] =>字符串(2)"IT"> ["state"] =>字符串(0)" [邮政编码"] =>字符串(0)" [城市"] =>字符串(0)" [地址"] =>>字符串(0)" [地址_2"] =>字符串(0)" ["[国家/地区]] =>字符串(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"] => bool(false)}吗? ["_changed":"WC_Customer":private] => bool(false)}
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 the WP_usermeta and in the admin 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屋!