如何从 WooCommerce 中的订单中获取客户详细信息?
Posted
技术标签:
【中文标题】如何从 WooCommerce 中的订单中获取客户详细信息?【英文标题】:How can I get customer details from an order in WooCommerce? 【发布时间】:2014-05-15 15:04:38 【问题描述】:我有一个功能可以做到这一点:
$order = new WC_Order($order_id);
$customer = new WC_Customer($order_id);
如何从中获取客户详细信息?
我已经尝试了文档中的所有内容,但不知何故,只有一些细节存在,而其余的却没有。例如。
$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"]=> 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"]=> 字符串(0) "" ["is_vat_exempt"]=> bool(false) ["calculated_shipping"]=> bool(false) ? ["_changed":"WC_Customer":private]=> bool(false)
如您所见,城市存在,但其余的都是空的。我已经检查了wp_usermeta
数据库表和客户的管理员面板,所有数据都在那里。
【问题讨论】:
【参考方案1】:2017-2020 WooCommerce 版本 3+ 和 CRUD Objects
1)。您可以在 WC_Order
对象实例上使用来自 WC_Order
和 WC_Abstract_Order
类的 getter 方法,例如:
// Get an instance of the WC_Order Object from the Order ID (if required)
$order = wc_get_order( $order_id );
// Get the Customer ID (User ID)
$customer_id = $order->get_customer_id(); // Or $order->get_user_id();
// Get the WP_User Object instance
$user = $order->get_user();
// Get the WP_User roles and capabilities
$user_roles = $user->roles;
// Get the Customer billing email
$billing_email = $order->get_billing_email();
// Get the Customer billing phone
$billing_phone = $order->get_billing_phone();
// Customer billing information details
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();
$billing_company = $order->get_billing_company();
$billing_address_1 = $order->get_billing_address_1();
$billing_address_2 = $order->get_billing_address_2();
$billing_city = $order->get_billing_city();
$billing_state = $order->get_billing_state();
$billing_postcode = $order->get_billing_postcode();
$billing_country = $order->get_billing_country();
// Customer shipping information details
$shipping_first_name = $order->get_shipping_first_name();
$shipping_last_name = $order->get_shipping_last_name();
$shipping_company = $order->get_shipping_company();
$shipping_address_1 = $order->get_shipping_address_1();
$shipping_address_2 = $order->get_shipping_address_2();
$shipping_city = $order->get_shipping_city();
$shipping_state = $order->get_shipping_state();
$shipping_postcode = $order->get_shipping_postcode();
$shipping_country = $order->get_shipping_country();
2)。您还可以使用 WC_Order
get_data()
方法从 Order 元数据中获取不受保护的数据数组,例如:
// Get an instance of the WC_Order Object from the Order ID (if required)
$order = wc_get_order( $order_id );
// Get the Order meta data in an unprotected array
$data = $order->get_data(); // The Order data
$order_id = $data['id'];
$order_parent_id = $data['parent_id'];
// Get the Customer ID (User ID)
$customer_id = $data['customer_id'];
## BILLING INFORMATION:
$billing_email = $data['billing']['email'];
$billing_phone = $order_data['billing']['phone'];
$billing_first_name = $data['billing']['first_name'];
$billing_last_name = $data['billing']['last_name'];
$billing_company = $data['billing']['company'];
$billing_address_1 = $data['billing']['address_1'];
$billing_address_2 = $data['billing']['address_2'];
$billing_city = $data['billing']['city'];
$billing_state = $data['billing']['state'];
$billing_postcode = $data['billing']['postcode'];
$billing_country = $data['billing']['country'];
## SHIPPING INFORMATION:
$shipping_first_name = $data['shipping']['first_name'];
$shipping_last_name = $data['shipping']['last_name'];
$shipping_company = $data['shipping']['company'];
$shipping_address_1 = $data['shipping']['address_1'];
$shipping_address_2 = $data['shipping']['address_2'];
$shipping_city = $data['shipping']['city'];
$shipping_state = $data['shipping']['state'];
$shipping_postcode = $data['shipping']['postcode'];
$shipping_country = $data['shipping']['country'];
现在获取用户帐户数据(来自订单 ID):
1)。你可以使用WC_Customer
类中的方法:
// Get the user ID from an Order ID
$user_id = get_post_meta( $order_id, '_customer_user', true );
// Get an instance of the WC_Customer Object from the user ID
$customer = new WC_Customer( $user_id );
$username = $customer->get_username(); // Get username
$user_email = $customer->get_email(); // Get account email
$first_name = $customer->get_first_name();
$last_name = $customer->get_last_name();
$display_name = $customer->get_display_name();
// Customer billing information details (from account)
$billing_first_name = $customer->get_billing_first_name();
$billing_last_name = $customer->get_billing_last_name();
$billing_company = $customer->get_billing_company();
$billing_address_1 = $customer->get_billing_address_1();
$billing_address_2 = $customer->get_billing_address_2();
$billing_city = $customer->get_billing_city();
$billing_state = $customer->get_billing_state();
$billing_postcode = $customer->get_billing_postcode();
$billing_country = $customer->get_billing_country();
// Customer shipping information details (from account)
$shipping_first_name = $customer->get_shipping_first_name();
$shipping_last_name = $customer->get_shipping_last_name();
$shipping_company = $customer->get_shipping_company();
$shipping_address_1 = $customer->get_shipping_address_1();
$shipping_address_2 = $customer->get_shipping_address_2();
$shipping_city = $customer->get_shipping_city();
$shipping_state = $customer->get_shipping_state();
$shipping_postcode = $customer->get_shipping_postcode();
$shipping_country = $customer->get_shipping_country();
2)。 WP_User
对象(WordPress):
// Get the user ID from an Order ID
$user_id = get_post_meta( $order_id, '_customer_user', true );
// Get the WP_User instance Object
$user = new WP_User( $user_id );
$username = $user->username; // Get username
$user_email = $user->email; // Get account email
$first_name = $user->first_name;
$last_name = $user->last_name;
$display_name = $user->display_name;
// Customer billing information details (from account)
$billing_first_name = $user->billing_first_name;
$billing_last_name = $user->billing_last_name;
$billing_company = $user->billing_company;
$billing_address_1 = $user->billing_address_1;
$billing_address_2 = $user->billing_address_2;
$billing_city = $user->billing_city;
$billing_state = $user->billing_state;
$billing_postcode = $user->billing_postcode;
$billing_country = $user->billing_country;
// Customer shipping information details (from account)
$shipping_first_name = $user->shipping_first_name;
$shipping_last_name = $user->shipping_last_name;
$shipping_company = $user->shipping_company;
$shipping_address_1 = $user->shipping_address_1;
$shipping_address_2 = $user->shipping_address_2;
$shipping_city = $user->shipping_city;
$shipping_state = $user->shipping_state;
$shipping_postcode = $user->shipping_postcode;
$shipping_country = $user->shipping_country;
相关:How to get WooCommerce order details
【讨论】:
【参考方案2】:WooCommerce 正在使用此功能在客户资料中显示帐单和送货地址。所以这可能会有所帮助。
用户需要登录才能使用此功能获取地址。
wc_get_account_formatted_address( 'billing' );
或
wc_get_account_formatted_address( 'shipping' );
【讨论】:
【参考方案3】:LoicTheAztec's answer 中显示了如何检索此信息。
仅适用于 WooCommerce v3.0+
基本上可以调用
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
这将返回一个数组到帐单数据,包括帐单和运输属性。通过 var_dump-ing 来探索它。
这是一个例子:
$order_billing_data = array(
"first_name" => $order_data['billing']['first_name'],
"last_name" => $order_data['billing']['last_name'],
"company" => $order_data['billing']['company'],
"address_1" => $order_data['billing']['address_1'],
"address_2" => $order_data['billing']['address_2'],
"city" => $order_data['billing']['city'],
"state" => $order_data['billing']['state'],
"postcode" => $order_data['billing']['postcode'],
"country" => $order_data['billing']['country'],
"email" => $order_data['billing']['email'],
"phone" => $order_data['billing']['phone'],
);
【讨论】:
【参考方案4】:另一个从数据库中获取客户详细信息的示例:
$order = new WC_Order($order_id);
$order_detail['status'] = $order->get_status();
$order_detail['customer_first_name'] = get_post_meta($order_id, '_billing_first_name', true);
$order_detail['customer_last_name'] = get_post_meta($order_id, '_billing_last_name', true);
$order_detail['customer_email'] = get_post_meta($order_id, '_billing_email', true);
$order_detail['customer_company'] = get_post_meta($order_id, '_billing_company', true);
$order_detail['customer_address'] = get_post_meta($order_id, '_billing_address_1', true);
$order_detail['customer_city'] = get_post_meta($order_id, '_billing_city', true);
$order_detail['customer_state'] = get_post_meta($order_id, '_billing_state', true);
$order_detail['customer_postcode'] = get_post_meta($order_id, '_billing_postcode', true);
【讨论】:
【参考方案5】:我确实设法弄清楚了:
$order_meta = get_post_meta($order_id);
$email = $order_meta["_shipping_email"][0] ?: $order_meta["_billing_email"][0];
我确实知道送货电子邮件是否是元数据的一部分,但如果是这样,我宁愿拥有它而不是账单电子邮件 - 至少出于我的目的。
【讨论】:
【参考方案6】:我刚刚处理了这个。根据您真正想要的,您可以从订单中获取详细信息,如下所示:
$field = get_post_meta($order->id, $field_name, true);
其中 $field_name 是“_billing_address_1”或“_shipping_address_1”或“first_name”。其他字段可以google,但不要忘记开头的“”。
如果您想检索此订单的客户,并直接获取其字段,则它与您的解决方案一样,只是您不需要检索完整的客户对象:
$customer_id = (int)$order->user_id;
$field = get_user_meta($customer_id, $field_name, true);
现在在这种情况下,$field_name 不以“_”开头。例如:“first_name”和“billing_address_1”。
【讨论】:
【参考方案7】:WooCommerce“订单”只是一个自定义帖子类型,因此所有订单都存储在 wp_posts 中,其订单信息存储在 wp_postmeta 表中。
如果您想获取 WooCommerce 的“订单”的任何详细信息,则可以使用以下代码。
$order_meta = get_post_meta($order_id);
上面的代码返回一个 WooCommerce“订单”信息的数组。您可以使用该信息,如下所示:
$shipping_first_name = $order_meta['_shipping_first_name'][0];
要查看“$order_meta”数组中存在的所有数据,可以使用以下代码:
print("<pre>");
print_r($order_meta);
print("</pre>");
【讨论】:
【参考方案8】:虽然,这可能是不可取的。
如果您想获取客户详细信息,即使用户没有创建帐户,而只是下订单,您也可以直接从数据库中查询。
虽然,可能会有性能问题,直接查询。但这肯定 100% 有效。
您可以通过post_id
和meta_keys
进行搜索。
global $wpdb; // Get the global $wpdb
$order_id = Your Order Id
$table = $wpdb->prefix . 'postmeta';
$sql = 'SELECT * FROM `'. $table . '` WHERE post_id = '. $order_id;
$result = $wpdb->get_results($sql);
foreach($result as $res)
if( $res->meta_key == 'billing_phone')
$phone = $res->meta_value; // get billing phone
if( $res->meta_key == 'billing_first_name')
$firstname = $res->meta_value; // get billing first name
// You can get other values
// billing_last_name
// billing_email
// billing_country
// billing_address_1
// billing_address_2
// billing_postcode
// billing_state
// customer_ip_address
// customer_user_agent
// order_currency
// order_key
// order_total
// order_shipping_tax
// order_tax
// payment_method_title
// payment_method
// shipping_first_name
// shipping_last_name
// shipping_postcode
// shipping_state
// shipping_city
// shipping_address_1
// shipping_address_2
// shipping_company
// shipping_country
【讨论】:
【参考方案9】:我一直在寻找这样的东西。效果很好。
所以像这样在 WooCommerce 插件中获取手机号码 -
$customer_id = get_current_user_id();
print get_user_meta($customer_id, 'billing_phone', true);
【讨论】:
【参考方案10】:从订单对象中获取客户ID:
$order = new WC_Order($order_id);
// Here the customer data
$customer = get_userdata($order->customer_user);
echo $customer->display_name;
【讨论】:
【参考方案11】:如果您想要客户在订购时输入的客户详细信息,则可以使用以下代码:
$order = new WC_Order($order_id);
$billing_address = $order->get_billing_address();
$billing_address_html = $order->get_formatted_billing_address();
// For printing or displaying on the web page
$shipping_address = $order->get_shipping_address();
$shipping_address_html = $order->get_formatted_shipping_address(); // For printing or displaying on web page
除此之外,$customer = new WC_Customer( $order_id );
无法获取您的客户详细信息。
首先,new WC_Customer()
不接受任何参数。
其次,WC_Customer
只会在用户登录且他/她不在管理员端时获取客户的详细信息。相反,他/她应该在网站的前端,例如“我的帐户”、“商店”、“购物车”或“结帐”页面。
【讨论】:
但是有没有办法通过woocommerce直接访问wp_usermeta中存储的信息呢?是否有可能从订单 ID 中获取用户 ID?。 您可以使用此代码从订单 id 获取用户 id $user_id = get_post_meta( $order_id, '_customer_user', true ); 我遇到了与 OP 相同的问题,但即使我以非管理员用户身份登录,我仍然得到一个空地址。我已经尝试过$customer = new WC_Customer();
和global $woocommerce; $customer = $woocommerce->customer;
,但都给我留下了空的地址信息。
更多信息参见课程页面WC_Order 和WC_Abstract_Order
$order->get_billing_address() 不是 WC_Order 上的方法。【参考方案12】:
也许看看WooCommerce Order 类?例如,要获取客户的电子邮件地址:
$order = new WC_Order($order_id);
echo $order->get_billing_email();
只是一个想法......
【讨论】:
最短和最佳答案。我使用echo $order->get_billing_phone();
得到billing_phone
【参考方案13】:
尝试了$customer = new WC_Customer();
和global $woocommerce; $customer = $woocommerce->customer;
后,即使我以非管理员用户身份登录,地址数据仍然为空。
我的解决方案如下:
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;
...无论您是否以管理员身份登录,此代码都有效。
【讨论】:
实际上这是我解决此问题所采用的方法,因此我将此解决方案标记为已接受 如果在订单中购买产品的人是网站的客人怎么办?现在如何获取这些信息?不会有user_id 是的,我不确定。也许您可以从他们的电子邮件地址或(如果幸运的话)从会话中检索它。可能不可能,但可能值得作为一个新问题提出。 :-)【参考方案14】:$customer_id = get_current_user_id();
print get_user_meta( $customer_id, 'billing_first_name', true );
【讨论】:
请在您的回答中添加评论 这很有效,因为它将获取客户变量。它只是查询 WC 客户的详细信息。【参考方案15】:发生这种情况是因为 WC_Customer 抽象在会话中不保存地址数据(以及其他数据)。此数据通过购物车/结帐页面存储,但同样 - 仅在会话中(就 WC_Customer 类而言)。
如果您查看结帐页面获取客户数据的方式,您将跟随它到 WC_Checkout 类方法get_value
,它直接将其从user meta 中提取出来。你最好遵循同样的模式:-)
【讨论】:
以上是关于如何从 WooCommerce 中的订单中获取客户详细信息?的主要内容,如果未能解决你的问题,请参考以下文章