( Woocommerce) How to get the user belonging to an order?

The question:

I am making a function which needs to be called after an order is completed within WooCommerce. For this I’m using the *’woocommerce_order_status_completed’* hook.
I want to check if the order has a product belonging to a specific product category.
If this is the case, the user will be added to a group within WordPress. To handle the group logic, I’m making use of another plugin’s API. This method needs the *group_id* and the *user_id*.
The only thing I’m missing is getting the *user_id*.

How can I get the user (in detail: the id), from my WooCommerce Order?


function isa_set_isa_member_group_after_order_completed($order_id) {
    $order = new WC_Order( $order_id );
    $items = $order->get_items();

    foreach($items as $item) {
        $product = $order->get_product_from_item($item);
        $has_membership_product = has_term('Lidmaatschap','product_cat',$product->post);

        if($has_membership_product) {

    $group = Groups_Group::read_by_name('ISA Leden');

    if(!group) {
        return; // TODO: Add error message + email?

    $user_id = 0 // TODO: Get user id from order.

    $user_group = array(
        "user_id" => $user_id,
        "group_id" => $group->group_id

    $result = Groups_User_Group::create($user_group);

    if(!result) {
        return; // TODO: Add error message + email?


Technical details:

WordPress version: 3.8

WooCommerce version: 2.1

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

From the __get() method in the WC_Order class you can see that the user_id property is taken from/stored as _customer_user post meta for the order in question.

 * __get function.
 * @access public
 * @param mixed $key
 * @return mixed
public function __get( $key ) {
    // Get values or default if not set
    if ( 'completed_date' == $key ) {
        $value = ( $value = get_post_meta( $this->id, '_completed_date', true ) ) ? $value : $this->modified_date;
    } elseif ( 'user_id' == $key ) {
        $value = ( $value = get_post_meta( $this->id, '_customer_user', true ) ) ? absint( $value ) : '';
    } else {
        $value = get_post_meta( $this->id, '_' . $key, true );

    return $value;

So in your code you can grab the user id like so:

$order = new WC_Order( $order_id );
$user_id = $order->user_id;

I presume you’re not going to allow guest checkout, but you might want some kind of fallback in case there isn’t a user id.

Update for WooCommerce 3.0

Disregard all of the above. As pointed out in the comments there are direct methods for getting this information. In fact, almost all “magic methods” have been removed and directly accessing object properties will throw PHP warnings.

$order = wc_get_order( $order_id );
$user = $order->get_user();
$user_id = $order->get_user_id();

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment