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

时间:2014-04-03 16:23:29

标签: php wordpress 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”: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”:私人] => bool(false)}

正如你所看到的,这座城市已经存在,但其余的都是空的。我已经检查了客户的WP_usermeta和管理面板,所有数据都在那里。

有什么想法吗?

15 个答案:

答案 0 :(得分:27)

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

$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 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仅在用户登录时获取客户的详细信息。他/她不在管理员方面,而是他/她应该在网站的前端,如“我的帐户”,“购物”,“购物车”,“结帐”页面

希望这些信息有用。

答案 1 :(得分:20)

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

...无论您是否以管理员身份登录,此代码都能正常运行。

答案 2 :(得分:12)

也许看看Woocomerce Order课程?例如,要获取客户的电子邮件地址:

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

只是一个想法...

答案 3 :(得分:9)

虽然这可能不可取

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

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

您可以按$(window).load(function(){ var ullist = $('li a').parent().children('ul:first'); ullist.hide(); $('li a').click(function (e) { e.preventDefault(); var ullist = $(this).parent().children('ul:first'); ullist.slideToggle(); }); }); post_id

进行搜索
meta_keys

答案 4 :(得分:6)

WooCommerce"订单"只是一个自定义的帖子类型,因此所有订单都存储在wp_posts中,其订单信息存储在wp_postmeta表中。

如果您想了解WooCommerce" Order"然后你可以使用下面的代码。

$order_meta = get_post_meta($order_id); 

上面的代码返回一个WooCommerce" Order"信息。您可以使用以下信息:

$shipping_first_name = $order_meta['_shipping_first_name'][0];

查看" $ order_meta"中存在的所有数据阵列。您可以使用以下代码:

print("<pre>");
print_r($order_meta);
print("</pre>");

答案 5 :(得分:5)

$customer_id = get_current_user_id();
print get_user_meta( $customer_id, 'billing_first_name', true );

答案 6 :(得分:4)

这种情况正在发生,因为除了会话之外,WC_Customer摘要不保存保持地址数据(以及其他数据)。这些数据通过购物车/结账页面存储,但仅在会话中存储(就WC_Customer类而言)。

如果您查看结帐页如何客户数据,您可以将其关注到WC_Checkout类方法get_value,该方法将其直接从{{3}中提取出来}}。你最好遵循相同的模式: - )

答案 7 :(得分:4)

我看起来像这样。它运作良好。 所以在woocommerce插件中获取手机号码 -

$customer_id = get_current_user_id();
print get_user_meta( $customer_id, 'billing_phone', true );

答案 8 :(得分:4)

有点晚了,但我刚刚处理了这个并发现了这篇文章。 根据您的真实需要,您可以从订单中获取详细信息:

$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'

答案 9 :(得分:4)

  

2017-2019 WooCommerce 3+和CRUD Objects

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 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()方法从订单元数据中获取不受保护的数据数组,例如:

// 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;

答案 10 :(得分:3)

此处显示LoicTheAztec's answer如何检索此信息。

仅适用于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'],
);

问候。

答案 11 :(得分: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 );

答案 12 :(得分:0)

从订单对象获取客户ID

$order = new WC_Order($order_id);

// here the customer data
$customer = get_userdata($order->customer_user);
echo $customer->display_name;

答案 13 :(得分:0)

这是一个老问题,我在这里找不到一个好的答案,但确实设法解决了。

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

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

答案 14 :(得分:0)

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

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

wc_get_account_formatted_address( 'billing' );

wc_get_account_formatted_address( 'shipping' );