WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

closed as off-topic by Rarst Mar 16 '14 at 14:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 13 down vote accepted

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.

share|improve this answer
WooCommerce WC_Order class comes with get_user() which returns the user object or false for guests and get_user_id() which returns the user id or zero for guests... You no longer need to worry about a callback if you are using those. – Nabil Kadimi Jan 1 '15 at 16:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.