Hide price and add to cart form for purchased product I'm using Drupal 7 and Drupal Commerce.

I need to hide the price and the add to cart form when a product was bought by the current user. 

I found the solution for the add to cart form, but not for the price. 

I imagine that it is necessary to use the function hook_entity_view_alter, but my code doesn't work. Here he is :

function mymodule_entity_view_alter(&amp;$build, $type) {
  $purchased_product = mymodule_get_users_purchased_products();

  $line_item = commerce_unit_price[LANGUAGE_NONE]['0']['amount'];
  $product = commerce_product_load($line_item-&gt;commerce_product[LANGUAGE_NONE][0]['product_id']);

      if (in_array($product-&gt;product_id, $purchased_product)) {
        $build['product:commerce_price']['#access']=FALSE;
      }
    }


Function for load the purchased product :

mymodule_get_users_purchased_products() {
      $purchased_products = &amp;drupal_static(__FUNCTION__);

      if (!isset($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();

        $purchased_products = array_unique($result-&gt;fetchCol());
      }

      return $purchased_products;
    }


Do you know where from comes the problem?

Thanks for your help
Here's a function that you can use to accomplish your goals. I tested it with Commerce Kickstart 2.

function MYMODULE_entity_view_alter(&amp;$build, $type) {
  if ($build['#view_mode'] != 'full' &amp;&amp; $build['#entity_type'] != 'node' || empty($build['field_product'])) {
    return;
  }

  // Load your purchased products.
  $purchased_products = array('HAT2-BLK-OS');

  foreach ($build['#node']-&gt;field_product[LANGUAGE_NONE] as $product_data) {
    $products[] = $product_data['product_id'];
  }

  $all_products = commerce_product_load_multiple($products);
  if (count($all_products) == 1) {
    $product = reset($all_products);
    if (in_array($product-&gt;sku, $purchased_products)) {
      $build['product:sku']['#access'] = FALSE;
      $build['field_product']['#access'] = FALSE;
    }
  }
}


Let's step through it. The first thing that happens is that you need to get your SKUs that you'll base your hiding off of. Just replace this line with a line that loads the products that the current user has purchased.

  $purchased_products = array('HAT2-BLK-OS');


Then, you load the products:

  foreach ($build['#node']-&gt;field_product[LANGUAGE_NONE] as $product_data) {
    $products[] = $product_data['product_id'];
  }


If you just have a single product per page, you could grab a single product_id.

Then you need to load your products.

  $all_products = commerce_product_load_multiple($products);


I'm assuming a single SKU per product here, so it does reset($all_products) to grab the first one. Then, just do a check to see if that product's SKU is in the array of SKUs that were already purchased:

    if (in_array($product-&gt;sku, $purchased_products)) {
      $build['product:sku']['#access'] = FALSE;
      $build['field_product']['#access'] = FALSE;
    }


If it, then hide the sku (in product:sku) and the add to cart form (in field_product). The result will then be this:



As opposed to this: