Commerce - check if this very product in presented in user's previous orders I use drupal commerce trying to build a website which sells content (nodes with restricted access). The thing is that, if user have bought the product once, he shouldn't have a possibility to do it again.

Currently i'm using form_commerce_add_to_cart_form_alter to check if user has already added this product to cart and if yes - hide "Add to Cart" button to prevent doing it twice and set a text that this very product is already in the cart. It works totally fine.

What I want to do next are two things:

1) to check if this current user has this product under active (not canceled and not complete) order, and if yes - hide "Add to cart button" and set a text that this product is under order processing at now.

2) to check if this current user has purchased this very product ever before and if he has, do the same trick - hide "Add to cart button" and set a text that User already owns this product.

My problem is that I'm very new to commerce and unfortunately totally don't understand how can I get all orders for the current user and check for some product IDs there... Will be very appreciated for any advice or a tip. Thanks a lot!
If you have the Entity API module installed, you can do it using entity_metadata_wrappers and EntityFieldQuery like this:

$query = new EntityFieldQuery();

$query-&gt;entityCondition('entity_type', 'commerce_order')
  -&gt;entityCondition('uid', $uid);
$result = $query-&gt;execute();

if (isset($result['commerce_order'])) {
  $user_order_ids = array_keys($result['commerce_order']);
}

// Array for storing user's products.
$products = array();

foreach($user_order_ids as $user_order_id) {

  $o = entity_metadata_wrapper('commerce_order', $user_order_id);

  $line_items = $o-&gt;commerce_line_items-&gt;value();

  foreach ($line_items as $line_item) {
    $l = entity_metadata_wrapper('commerce_line_item', $line_item-&gt;line_item_id);
    $products[$o-&gt;status-&gt;value()][] = $l-&gt;commerce_product-&gt;product_id-&gt;value();
  }
}


This should give you a 2 dimensional array of products like this:

... (Array, 1 element)
  cart(Array, 2 elements)
    0 (String, 3 characters ) 203
    1 (String, 3 characters ) 158
  other_state(Array, 2 elements)
    0 (String, 3 characters ) 187