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

前端 未结 15 1512
北海茫月
北海茫月 2020-12-01 00:10

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 fr

15条回答
  •  遥遥无期
    2020-12-01 00:42

    I just dealt with this. 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 its 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'.

提交回复
热议问题