How to get Customer details from Order in WooComme

2020-02-02 05:45发布

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

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.

Any thoughts?

15条回答
我想做一个坏孩纸
2楼-- · 2020-02-02 06:23

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.

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'

查看更多
啃猪蹄的小仙女
3楼-- · 2020-02-02 06:23

And another example to get customer detail from database:

$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 );
查看更多
走好不送
4楼-- · 2020-02-02 06:24

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

If you take a look at how the checkout page gets the 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 :-)

查看更多
Ridiculous、
5楼-- · 2020-02-02 06:25

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%

You can search by post_id and 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
        }
查看更多
forever°为你锁心
6楼-- · 2020-02-02 06:25

Here in LoicTheAztec's answer is shown how to retrieve this information.

Only for Woocommerce v3.0+

Basically, you can call

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

This will return an array to the billing order data, including billing and shipping properties. Explore it by var_dump-ing it. Here's an example:

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

Greetings.

查看更多
beautiful°
7楼-- · 2020-02-02 06:31

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.

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.

查看更多
登录 后发表回答