如何从WooCommerce中的订单中获取客户详细信息


How can I get customer details from an order in WooCommerce?

我有一个函数可以做到这一点:

$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":p rotected]=> array(14) { ["country"]=> string(2) "IT">["state"]=> string(0) " ["邮政编码"]=> string(0) " ["city"]=> string(0) " ["address"]=>>string(0) " ["address_2"]=> string(0) "

["shipping_country"]=> 字符串(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":p rivate]=> bool(false) }

如您所见,这座城市存在,但其余的都是空的。我已经签入了wp_usermeta数据库表和客户的管理员面板,所有数据都在那里。

2017-2020 WooCommerce 版本 3+ 和 CRUD 对象

1). 您可以在WC_Order对象实例上使用来自WC_OrderWC_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;

相关: 如何获取WooCommerce订单详细信息

如果您需要客户在订购时输入的客户详细信息,则可以使用以下代码:

$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才会获得客户的详细信息。相反,他/她应该在网站的前端,如"我的帐户","商店","购物车"或"结帐"页面。

尝试$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;
}

。无论您是否以管理员身份登录,此代码都有效。

也许看看WooCommerce Order类?例如,要获取客户的电子邮件地址,请执行以下操作:

$order = new WC_Order($order_id);
echo $order->get_billing_email();

只是一个想法...

虽然,这可能不可取。

如果您想获取客户详细信息,即使用户没有创建帐户,而只是下订单,您也可以直接从数据库中查询它。

虽然,可能存在性能问题,但直接查询。但这肯定是 100% 有效的。

您可以按post_idmeta_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
        }
$customer_id = get_current_user_id();
print get_user_meta( $customer_id, 'billing_first_name', true );

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>");

我正在寻找这样的东西。效果很好。

因此,在WooCommerce插件中获取手机号码,如下所示-

$customer_id = get_current_user_id();
print get_user_meta($customer_id, 'billing_phone', true);
发生这种情况

是因为WC_Customer摘要不会在会话中保存保留地址数据(以及其他数据)。此数据通过购物车/结帐页面存储,但同样 - 仅在会话中存储(就WC_Customer类而言)。

如果您查看结帐页面如何获取客户数据,您将遵循它到 WC_Checkout 类方法 get_value ,它直接将其从用户元中提取出来。你最好遵循同样的模式:-)

我刚刚处理了这个问题。根据你真正想要的,你可以从订单中获取详细信息,如下所示:

$field = get_post_meta($order->id, $field_name, true);

其中 $field_name 是"_billing_address_1"或"_shipping_address_1"或"first_name"。你可以谷歌其他字段,但不要忘记开头的"。

如果要检索此订单的客户,并直接获取其字段,则它的工作方式与解决方案中相同,但不需要检索完整的客户对象:

$customer_id = (int)$order->user_id;
$field = get_user_meta($customer_id, $field_name, true);

在这种情况下,$field_name 不以"_"开头。例如:"first_name"和"billing_address_1"。

另一个从数据库中获取客户详细信息的示例:

$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);

在LoicTheAztec的答案中展示了如何检索这些信息。

仅适用于 WooCommerce v3.0+

基本上,您可以调用

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

这将返回一个数组到计费订单数据,包括计费和运输属性。通过var_dump来探索它。

下面是一个示例:

$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'],
);

从订单对象获取客户 ID:

$order = new WC_Order($order_id);
// Here the customer data
$customer = get_userdata($order->customer_user);
echo $customer->display_name;

我确实设法弄清楚了:

$order_meta    = get_post_meta($order_id);
$email         = $order_meta["_shipping_email"][0] ?: $order_meta["_billing_email"][0];

我确实知道运输电子邮件是否是元数据的一部分,但如果是这样,我宁愿拥有它而不是账单电子邮件 - 至少就我的目的而言。

WooCommerce正在使用此功能在客户资料中显示账单和送货地址。所以这可能会有所帮助。

用户需要登录才能使用此功能获取地址。

wc_get_account_formatted_address( 'billing' );

wc_get_account_formatted_address( 'shipping' );