How to get customer details from an order in WooCommerce
In this tutorial, we will look at the different ways to get customer details from orders in WooCommerce.
You can get customer order details in WooCommerce in a few ways. One way is to go to the orders page and click on the order 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 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.
This article will explore two ways to get customer details from order data. The first and main method will be to programmatically get customer details from the WooCommerce order object with some PHP code. To do so, we first need 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.
wc_get_order
wc_get_order
is a function in WooCommerce that retrieves an order object based on the order ID, allowing developers to access and manipulate order-related data programmatically. It’s a crucial component for customizing and extending WooCommerce functionality through code.
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:
$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
// 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 many customer details (and user info) related to that order. Let’s see how to get customer billing information and customer shipping information from the order data:
// 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 create an order object using the WooCommerce wc_get_order function. 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 WC_Order get_data() method
You can also use the get_data() method to get an unprotected data array from Order metadata 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'];
How to get WooCommerce customer details from an order without a code
If you want to get all the customer details based on a particular WooCommerce order but are not looking for a coding solution, you can do it with the help of Users Insights.
How to get WooCommerce order details from the table fields
The Users Insights plugin lets you get the data from the WooCommerce order info by combining user table fields with the WooCommerce module. This lets you filter and show specific WooCommerce order info from the WordPress dashboard. With the Users Insights plugin, you can access WooCommerce order details by customizing the user table to show or hide specific fields, such as Number of Orders, Order Status, Lifetime value, Billing country, Billing state, Billing city, Order Notes, Wishlist products, Subscriptions and more.
By combining these fields, you can create detailed reports and insights on customer behavior and order history within your WooCommerce store.
Using the plugin’s filtering options, you can drill down into the data to focus on particular customers, order statuses, or purchase patterns. This allows you to create detailed reports and gain insights into customer behavior and order trends directly within your WordPress dashboard.
How to get WooCommerce order details from profile data
Suppose you have the plugin installed and active on your WooCommerce store. In that case, Users Insights will create an advanced WooCommerce customer profile that has all the available customer details in one place. The plugin also automatically links WooCommerce order details with the WooCommerce customer details. Additionally, it adds a link to every ordered item in WooCommerce to the corresponding WooCommerce customer profile. To find the customer details associated with a specific order, you must first navigate the WooCommerce -> Orders list in your WordPress dashboard.
Once you find the specific order you are looking for, click and open the order page.
You will see a “Users Insights Profile →” link under the customer’s name. This links to the WooCommerce customer profile page of the customer who has purchased the order, and from there, you can get all the customer details you might need.