Best way to get current user purchased products I need to get purchased products by the current user (Drupal Commerce). 

I use this function to reach there :

function mymodule_user_purchased_products() {
    global $user;
    $query = db_select('commerce_order', 'corder');
    $query-&gt;join('commerce_line_item', 'li', 'corder.order_id = li.order_id');
    $query-&gt;join('field_data_commerce_product', 'prod', 'li.line_item_id = prod.entity_id');
    $query-&gt;condition('corder.uid', $user-&gt;uid, '=')
          -&gt;condition('corder.status', 'completed', '=')
          -&gt;fields('prod', array('commerce_product_product_id'));
    $result = $query-&gt;execute();
}


But I think I forget certain things, and this solution isn't optimal.

Is it the recommended way?
One one thing that I can think of to change. Make sure you only get a single result.

$query = db_select('commerce_order', 'corder');
$query-&gt;join('commerce_line_item', 'li', 'corder.order_id = li.order_id');
$query-&gt;join('field_data_commerce_product', 'prod', 'li.line_item_id = prod.entity_id');
$query-&gt;condition('corder.uid', $user-&gt;uid, '=')
      -&gt;condition('corder.status', 'completed', '=')
      -&gt;addExpression('DISTINCT prod.commerce_product_product_id', 'product_id');


Using this line:

-&gt;addExpression('DISTINCT prod.commerce_product_product_id', 'product_id');


instead of:

-&gt;fields('prod', array('commerce_product_product_id'));


will insure that you only get one product_id per row in your result. By using the DISTINCT SQL Keyword, it will only allow distinct values to be returned.