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 from this?
I have tried everything in the documentation, but somehow, just some details are present, but the rest aren’t. For example.
$data['Address'] = $customer->get_address() . ' ' . $customer->get_address_2();
$data['ZipCode'] = $customer->get_postcode();
Is empty.
Doing
var_dump($customer)
Produces:
object(WC_Customer)#654 (2) { [“_data”:protected]=> array(14) { [“country”]=> string(2) “IT” >[“state”]=> string(0) “” [“postcode”]=> string(0) “” [“city”]=> string(0) “” [“address”]=> >string(0) “” [“address_2”]=> string(0) “” [“shipping_country”]=> string(2) “IT”
[“shipping_state”]=> string(2) “BG” [“shipping_postcode”]=> string(0) “” [“shipping_city”]=> >string(0) “” [“shipping_address”]=> string(0) “” [“shipping_address_2”]=> string(0) “”
[“is_vat_exempt”]=> bool(false) [“calculated_shipping”]=> bool(false) } ?
[“_changed”:”WC_Customer”:private]=> bool(false) }
As you can see, the city is present, but the rest are empty. I have checked in wp_usermeta
database table and in the administrator panel of the customer and all the data is there.
2017-2020 WooCommerce versions 3+ and CRUD Objects
1). You can use getter methods from
WC_Order
andWC_Abstract_Order
classes on theWC_Order
object instance like:2). You can also use the
WC_Order
get_data()
method, to get an unprotected data array from Order meta data like:Now to get the user account data (from an Order ID):
1). You can use the methods from
WC_Customer
Class:2). The
WP_User
object (WordPress):Related: How to get WooCommerce order details
If you want customer’s details that customer had entered while ordering, then you can use the following code:
Apart from this,
$customer = new WC_Customer( $order_id );
can not get you customer details.First of all,
new WC_Customer()
doesn’t take any arguments.Secondly,
WC_Customer
will get customer’s details only when the user is logged in and he/she is not on the admin side. Instead he/she should be on website’s front-end like the ‘My Account’, ‘Shop’, ‘Cart’, or ‘Checkout’ page.Having tried
$customer = new WC_Customer();
andglobal $woocommerce; $customer = $woocommerce->customer;
I was still getting empty address data even when I logged in as a non-admin user.My solution was as follows:
…and this code works regardless of whether you are logged in as admin or not.
Maybe look at the WooCommerce Order class? For example, to get the customer’s email address:
Just a thought…
Although, this may not be advisable.
If you want to get customer details, even when the user doesnât create an account, but only makes an order, you could just query it, directly from the database.
Although, there may be performance issues, querying directly. But this surely works 100%.
You can search by
post_id
andmeta_keys
.WooCommerce “Orders” are just a custom post type, so all the orders are stored in wp_posts and its order information in stored into wp_postmeta tables.
If you would like to get any details of WooCommerce’s “Order” then you can use the below code.
The above code returns an array of WooCommerce “Order” information. You can use that information as shown below:
To view all data that exist in “$order_meta” array, you can use the below code:
I was looking for something like this. It works well.
So get the mobile number in WooCommerce plugin like this –
This is happening because the WC_Customer abstract doesn’t hold hold address data (among other data) apart from within a session. This data is stored via the cart/checkout pages, but againâonly in the session (as far as the WC_Customer class goes).
If you take a look at how the checkout page gets the customer data, you’ll follow it to the WC_Checkout class method
get_value
, which pulls it directly out of user meta. You’d do well to follow the same pattern 🙂I just dealt with this. Depending on what you really want, you can get the details from the order like this:
Where $field_name is ‘_billing_address_1’ or ‘_shipping_address_1’or ‘first_name’. You can google the other fields, but don’t forget the “” at the beginning.
If you want to retrieve the customer for this order, and get its field directly, it works as in your solution, except you do not need to retrieve the full customer object:
Now in this case, the $field_name does not start with “_”. For example: ‘first_name’ and ‘billing_address_1’.
And another example to get the customer details from the database:
Here in LoicTheAztec’s answer is shown how to retrieve this information.
Only for WooCommerce v3.0+
Basically, you can call
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:
Get the customer id from the order object:
I did manage to figure it out:
I do know know for sure if the shipping email is part of the metadata, but if so I would rather have it than the billing email – at least for my purposes.
WooCommerce is using this function to show billing and shipping addresses in the customer profile. So this will might help.
The user needs to be logged in to get address using this function.
or