Why WC_Customer Returns Empty Data for Some Fields
If you are pulling order data in WooCommerce and finding that $customer->get_address() returns an empty string while your database clearly contains the values, the issue lies in how the WC_Customer class interprets the order context. The confusion stems from a fundamental distinction in WooCommerce architecture: billing address versus shipping address, and how the customer object is initialized relative to the specific order instance.
When you instantiate a customer directly using an order ID with $customer = new WC_Customer($order_id), you are creating a wrapper around the data stored specifically for that transaction. However, WooCommerce orders do not always store a dedicated "billing" address in the same way they store a shipping address. If the customer account has saved billing details (which is common), those live in the user meta table (wp_usermeta). The order object itself, however, only stores what was explicitly entered at checkout or what was mapped to the billing_* fields during the save process.
The discrepancy you observed in your var_dump outputâwhere _data contains keys like shipping_country but empty strings for standard address fieldsâis a strong indicator that the order record lacks explicit billing address data, even if the customer account has it. The WC_Customer object is designed to be stateless regarding saved profile data unless explicitly told to look there. By default, it only reads from the transient _data array associated with the specific order revision or snapshot.
Accessing Saved Profile Data vs. Order-Specific Data
To retrieve the full set of customer details, including those stored in wp_usermeta, you must ensure the WC_Customer object is aware that it should look at the user's profile rather than just the current order's transient data. This is often handled implicitly when accessing the customer via the cart or checkout context, but not always when constructing an object directly from a raw Order ID in a background job or custom plugin.
If the goal is to get the user's saved billing address regardless of what was entered during that specific order checkout, you should check if the customer has a valid user ID and then access their profile directly. The WC_Customer class extends WP_User, meaning it inherits methods to fetch data from the database tables.
Here is the approach to ensure you capture the saved billing information:
$order = new WC_Order($order_id);
$customer = $order->get_customer(); // Get the customer linked to this order
// Check if there is a valid user ID associated with the order/customer
if ($customer->get_id()) {
// Force the object to load full user data including meta
$customer->set_user_meta_cache('billing', false);
$data['Address'] = $customer->get_formatted_billing_address();
$data['City'] = $customer->get_billing_city();
$data['ZipCode'] = $customer->get_billing_postcode();
} else {
// Fallback to order-specific data if no customer exists
$data['Address'] = $order->get_billing_address();
$data['City'] = $order->get_billing_city();
$data['ZipCode'] = $order->get_billing_postcode();
}
The Role of _changed and Data Hydration
Your var_dump showed the _changed property as false. In WooCommerce's internal logic, this flag indicates whether the object has been modified since its initial load. If you are hydrating an order or customer from a raw ID without triggering a full re-query against the database for user meta, the object might only populate fields that were explicitly set on the order table row (e.g., wp_wc_orders).
If your specific order was placed as a guest checkout and then converted to an account, or if the billing address was not saved to the order revision table but exists only in the user profile, direct property access will fail. The solution is to ensure you are accessing the object in a way that forces a reload of the user meta cache.
Sometimes, simply calling WC()->customer inside a frontend loop works because it triggers a specific data refresh hook. When writing custom code, you may need to instantiate the object differently or explicitly call methods that trigger database queries for missing fields.
Best Practice for Robust Data Retrieval
For production environments where data consistency is critical, relying solely on $customer->get_address() can be risky if the order state varies between guest and registered users. The most robust pattern involves checking the user ID first. If a user ID exists, prioritize fetching from the WP_User meta table. Only fall back to the order-specific properties if no customer is associated with the transaction.
This approach aligns with how platforms like https://laravelcompany.com handle cross-platform integrations: you must understand the underlying data model (User vs. Order) before accessing the surface-level API methods. By explicitly handling the separation between order-transient data and user-permanent data, you eliminate the "empty string" mystery and ensure your application logic works regardless of how the customer placed their order.
