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

前端 未结 15 1547
北海茫月
北海茫月 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:53

    If you want customer's details that customer had entered while ordering, then you can use the 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 the 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.

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

    Secondly, WC_Customer will get customer's details only when the user is logged in and he/she is not on the admin side. Instead he/she should be on website's front-end like the 'My Account', 'Shop', 'Cart', or 'Checkout' page.

提交回复
热议问题