WooCommerce: How to get a Customer Details from an Order

I was asked a question the other day about retrieving a user’s details from an order programmatically, since WooCommerce 3.0 it is reasonably easy to extract a user’s details but there are a number of methods that can be used and  we also need to consider how WooCommerce stores address data for the “My Account” section of the site. In this article we’ll take a look the various ways we can extract user address data.

Accessing a User’s Data from the Order Object

Here’s some code we could use to access a user’s data from an order object

Learn How To Change Your Store Without Hiring a Coder
Click here to enroll in our Free WooCommerce Coding Course
//add your order id value here
$order_id = 105;

// Get an instance of the WC_Order Object using the wc_get_order function
// assuming here that we only have the order id in a variable,
// in some cases the order object will be passed directly to an action or filter
$order = wc_get_order( $order_id );

// Get the customer or user id from the order object
$customer_id = $order->get_customer_id();

//this should return exactly the same number as the code above
$user_id = $order->get_user_id();

// Get the WP_User Object instance object
$user = $order->get_user();

// Get the WP_User roles
$user_roles = $user->roles;

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

//note that by default WooCommerce does not collect email and phone number for the shipping address
//so these fields are only available on the billing address
$billing_email  = $order->get_billing_email();
$billing_phone  = $order->get_billing_phone();

$billing_display_data = Array("First Name" => $billing_first_name,
    "Last Name" => $billing_last_name,
    "Company" => $billing_company,
    "Address Line 1" => $billing_address_1,
    "Address Line 2" => $billing_address_2,
    "City" => $billing_city,
    "State" => $billing_state,
    "Post Code" => $billing_postcode,
    "Country" => $billing_country,
    "Email" => $billing_email,
    "Phone" => $billing_phone);

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

$shipping_display_data = Array("First Name" => $billing_first_name,
    "Last Name" => $shipping_last_name,
    "Company" => $shipping_company,
    "Address Line 1" => $shipping_address_1,
    "Address Line 2" => $shipping_address_2,
    "City" => $shipping_city,
    "State" => $shipping_state,
    "Post Code" => $shipping_postcode,
    "Country" => $shipping_country);

echo "Retrieving user information from order id - " . $order_id ."<br><br>";
echo "Customer id from order is " . $customer_id . " the user id is " . $user_id . ", these values should be exactly the same<br><br>";

echo "Here is the customer's billing address from the order - <br>";
foreach ( $billing_display_data as $key => $value ) {
    echo $key . ' - ' . $value . "<br>";
}
echo "<br>";

echo "Here is the customer's shipping address from the order - <br>";
foreach ( $shipping_display_data as $key => $value ) {
    echo $key . ' - ' . $value . "<br>";
}
echo "<br>";

echo "The customer is a member of the following roles - <br>";

foreach ( $user_roles as $value ) {
    echo $value . "<br>";
}

In the code above we firstly create an order object using the WooCommerce wc_get_order function. We then use the various methods available on that object to get the user information and output it to the screen.

Here’s a screen shot of the output from the code above –

I’ve written a book about learning the basics of WooCommerce development, click here to find out more.

Accessing the My Account Address Information

Once a user has created an account then WooCommerce saves a billing and shipping address and it populates the Checkout screen with these details when a user with an account checks out (providing they logged into the site at the time of checkout). The user can also edit these details via the “My Account” section of the site

The code required to access these details is different to the code required to withdraw to address details from an order, here’s how to do it

//add your order id value here
$order_id = 105;

// Get the user ID from an order without creating a order object first
// this could also be achieved by
// $order = wc_get_order( $order_id );
// $customer_id = $order->get_customer_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();

$user_account_display_data = Array("First Name" => $first_name,
    "Last Name" => $last_name,
    "Username" => $username,
    "Display Name" => $display_name,
    "Email" => $user_email);

echo "Here are the customer's account details - <br>";
foreach ( $user_account_display_data as $key => $value ) {
    echo $key . ' - ' . $value . "<br>";
}
echo "<br>";

// Get the customer's billing information details from the account information
$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();

$billing_account_display_data = Array("First Name" => $billing_first_name,
    "Last Name" => $billing_last_name,
    "Company" => $billing_company,
    "Address Line 1" => $billing_address_1,
    "Address Line 2" => $billing_address_2,
    "City" => $billing_city,
    "State" => $billing_state,
    "Post Code" => $billing_postcode,
    "Country" => $billing_country,
    "Email" => $billing_email,
    "Phone" => $billing_phone);

echo "Here is the customer's billing address from their account details - <br>";
foreach ( $billing_account_display_data as $key => $value ) {
    echo $key . ' - ' . $value . "<br>";
}
echo "<br>";

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

$shipping_account_display_data = Array("First Name" => $billing_first_name,
    "Last Name" => $shipping_last_name,
    "Company" => $shipping_company,
    "Address Line 1" => $shipping_address_1,
    "Address Line 2" => $shipping_address_2,
    "City" => $shipping_city,
    "State" => $shipping_state,
    "Post Code" => $shipping_postcode,
    "Country" => $shipping_country);

echo "Here is the customer's shipping address from their account details - <br>";
foreach ( $shipping_account_display_data as $key => $value ) {
    echo $key . ' - ' . $value . "<br>";
}

The output from the code above will look very similar to the output we saw from the first code snippet.

Allowing a User To Choose from Multiple Addresses During Checkout

If you want to allow customer’s to save previous addresses, and choose which to use on the checkout screen I would recommend using the Multiple Addresses for WooCommerce plugin that allows user to save multiple addresses in the “My Account” section and then choose which one to use at checkout.

If you want to allow customers to ship an individual order to multiple addresses then I would take a look at Yith’s Multiple Shipping Addresses For Woocommerce plugin.

Final Thoughts

Hopefully these snippets have helped you to access the address information you need, if you have any problems or any further questions or queries then please don’t hesitate to let me know in the comments.


Warning: Undefined array key "format" in H:\root\home\bravenewworld101-001\www\hardworkingnerd\wp-content\plugins\convertkit\includes\class-convertkit-resource-forms.php on line 330

Leave a Comment