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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22843504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:18:46  来源:igfitidea点击:

How to get Customer details from Order in WooCommerce?

phpwordpresswoocommerce

提问by Alfonso Pérez

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

Is empty.

是空的。

Doing

正在做

var_dump($customer)

Produces:

产生:

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) }

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) } ? [”

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.

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

Any thoughts?

有什么想法吗?

采纳答案by ban-geoengineering

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.

经过尝试$customer = new WC_Customer();global $woocommerce; $customer = $woocommerce->customer;即使我以非管理员用户身份登录,我仍然得到空地址数据。

My solution was as follows:

我的解决方案如下:

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.

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

回答by Ratnakar - StoreApps

If you want customer's details that customer had entered while ordering, then you can use following code

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

$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

Apart from this, $customer = new WC_Customer( $order_id );can not get you customer details

除此之外,$customer = new WC_Customer( $order_id );无法获得您的客户详细信息

First of all, new WC_Customer()doesn't take any arguments

首先,new WC_Customer()不接受任何论据

Secondly, WC_Customerwill get customer's details only when the user is logged in & he/she is not on admin side, instead he/she should be on website's front-end like 'My Account', 'Shop', 'Cart', 'Checkout' page

其次,WC_Customer只有当用户登录并且他/她不在管理端时才会获取客户的详细信息,而他/她应该在网站的前端,如“我的帐户”、“商店”、“购物车”、“结帐” ' 页

Hope these information will be useful.

希望这些信息会有用。

回答by ColdTuna

Maybe look at the Woocomerce Orderclass? For example, to get the customer's email address:

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

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

Just a thought...

只是一个想法...

回答by Tosin Onikute

Although, this may not be advisable

虽然,这可能不是可取的

If you want to get Customer Details. Even when User doesn't create an account But only makes an Order. You could just query it, directly from the database.

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

Although, there may be performance issues, querying directly, But this surely works 100%

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

You can search by post_idand meta_keys

您可以通过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
        }

回答by AddWeb Solution Pvt Ltd

WooCommerce "Orders" are just a custom post type, so all the orders are stored in wp_posts and its order info in stored into wp_postmeta tables.

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

If you would like to get any details of WooCommerce "Order" then you can use below code.

如果您想获得 WooCommerce“订单”的任何详细信息,那么您可以使用以下代码。

$order_meta = get_post_meta($order_id); 

The above code returns an array of WooCommerce "Order" info. You can use that information as shown below:

上面的代码返回一组 WooCommerce“订单”信息。您可以使用该信息,如下所示:

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

To view all data exist in "$order_meta" array. You can use below code:

查看“$order_meta”数组中存在的所有数据。您可以使用以下代码:

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

回答by Рома Нечаев

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

回答by LoicTheAztec

2017-20020 WooCommerce versions 3+ and CRUD Objects

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

1) You can use getter methods from WC_Orderand WC_Abstract_Orderclasses on the WC_OrderObject instance like:

1) 您可以在Object 实例上使用 getter 方法WC_OrderWC_Abstract_Order类,WC_Order例如:

// 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) You can also use the WC_Orderget_data()method, to get an unprotected data array from order meta data like:

2)您还可以使用该WC_Orderget_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'];


Now to get the user account data (from an order ID):

现在获取用户帐户数据(来自订单 ID):

1) You can use the methods from WC_CustomerClass:

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) The WP_UserObject (Wordpress):

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;

回答by Josh Levinson

This is happening because the WC_Customer abstract doesn't hold hold address data (among other data) apart from within a session. This data is stored via the cart/checkout pages, but again—only in the session (as far as the WC_Customer class goes).

之所以发生这种情况,是因为 WC_Customer 抽象不包含会话中的地址数据(以及其他数据)。该数据通过购物车/结帐页面存储,但同样仅在会话中(就 WC_Customer 类而言)。

If you take a look at how the checkout page getsthe customer data, you'll follow it to the WC_Checkout class method get_value, which pulls it directly out of user meta. You'd do well to follow the same pattern :-)

如果您查看结帐页面如何获取客户数据,您将跟随它进入 WC_Checkout 类方法get_value,该方法将其直接从user meta 中拉出。你最好遵循相同的模式:-)

回答by Pieter van Kampen

A bit late, but I just dealt with this and came across this post. Depending on what you really want, you can get the details from the order like this:

有点晚了,但我刚刚处理了这个问题并遇到了这篇文章。根据您真正想要的内容,您可以从订单中获取详细信息,如下所示:

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

Where $field_name is '_billing_address_1' or '_shipping_address_1'or '_first_name'. You can google the other fields, but don't forget the "_" at the beginning.

其中 $field_name 是“_billing_address_1”或“_shipping_address_1”或“_first_name”。您可以谷歌搜索其他字段,但不要忘记开头的“_”。

If you want to retrieve the customer for this order, and get it's field directly, it works as in your solution, except you do not need to retrieve the full customer object:

如果您想检索此订单的客户,并直接获取它的字段,它的工作方式与您的解决方案一样,只是您不需要检索完整的客户对象:

$customer_id = (int)$order->user_id;

$field= get_user_meta($customer_id, $field_name,true)

; Now in this case, the $field_name does not start with "_" For example: 'first_name', and 'billing_address_1'

; 现在在这种情况下,$field_name 不以“_”开头 例如:“first_name”和“billing_address_1”

回答by Vikas Kumar

I was looking something like this. It works well. So get mobile number in woocommerce plugin like this -

我正在寻找这样的东西。它运作良好。所以像这样在woocommerce插件中获取手机号码 -

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