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

前端 未结 15 1468
北海茫月
北海茫月 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 01:02

    2017-2020 WooCommerce versions 3+ and CRUD Objects

    1). You can use getter methods from WC_Order and WC_Abstract_Order classes on the WC_Order object instance like:

    // 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_Order get_data() method, to get an unprotected data array from Order meta data like:

    // 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):

    1). You can use the methods from WC_Customer Class:

    // 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_User object (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;
    

    Related: How to get WooCommerce order details

    0 讨论(0)
  • 2020-12-01 01:05

    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'],
    );
    
    0 讨论(0)
  • 2020-12-01 01:06
    $customer_id = get_current_user_id();
    print get_user_meta( $customer_id, 'billing_first_name', true );
    
    0 讨论(0)
提交回复
热议问题