25

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?

code:

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) {
            break;
        }
    }

    $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

35

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();
  • 7
    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
  • 1
    beware when order belonging to guest on get_user_id will return 0 – Vincent Guesné Oct 24 '19 at 17:40

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