How to get customer details from an order in WooCommerce
In this tutorial we are going to look at the different ways to get customer details from order in WooCommerce.
There are a few ways that you can get customer order details in WooCommerce. One way is to go to the orders page and click on the order that you want to view. This will take you to the order details page where you can see all of the information about the order, including some of customer details like the customer’s name, shipping address, and email. But if you want to access all of the customer details that your WooCommerce store has there isn’t an out of the box way to do it.
In this article we are going to explore two different ways to get customer details from order data. The first and the main method covered in this article will on how to get the customer details programmatically from the WooCommerce order object. For for that we first will need to get access to the WooCommerce order object.
How to get WooCommerce order data
If you have the order id available, you can access the order by using the wc_get_order WooCommerce function. You can use get methods from
WC_Order and
WC_Abstract_Order classes on the WC_Order
object instance by using the $order_id like this:
1 |
$order = wc_get_order( $order_id ); |
Now that we have the order object we have access to all the details associated with that specific order, including the customer details.
How to get customer order details programmatically from the WC_Order object
- get customer id and user id
- get the user object
- get the user roles
- get the customer billing email
- get customer phone number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// First we get an instance of the WC_Order Object from the Order ID $order = wc_get_order( $order_id ); // then we can 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 address $billing_email = $order->get_billing_email(); // Get the Customer billing phone $billing_phone = $order->get_billing_phone(); |
As you can see once you have access to the WooCommerce order object you can get a lot of customers details (and user info) that are related to that order. How let’s see how to get customer billing information and customer shipping information from the order data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Get 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(); // Get 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(); |
The code above demonstrates how to use the WooCommerce wc_get_order function to create an order object. Once you have created the object, you can use different methods to get information about the user, such as their name and address.
Unlock the Full Potential of Your WooCommerce Customer Data
How to use the the WC_Order get_data() method
You can also use the get_data() method, to get an unprotected data array from Order meta data like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// 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']; |
How to get customer details from order without code
If you want to get all the customer details based on a particular WooCommerce order but you are not looking for a coding solution you can do it with the help of Users Insights. If you have the plugin installed and active on your WooCommerce store, Users Insights will create an advanced WooCommerce customer profile that has all the available customer details in once place. The plugin also automatically links WooCommerce order details with the WooCommerce customer details. Additionally, it adds a link to every order item in WooCommerce to the corresponding WooCommerce customer profile. To find the customer details associated with a specific order first you will need to navigate the WooCommerce -> Orders list in your WordPress dashboard.
Once you find the specific order that you are looking for click and open the order page.
There under the customers name you will see a “Users Insights Profile →” link. This links to the WooCommerce customer profile page of the customer who have purchased the order and from there you can get all the customer details that you might need.