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(&$build, $type) {
$purchased_product = mymodule_get_users_purchased_products();
$line_item = commerce_unit_price[LANGUAGE_NONE]['0']['amount'];
$product = commerce_product_load($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
if (in_array($product->product_id, $purchased_product)) {
$build['product:commerce_price']['#access']=FALSE;
}
}
Function for load the purchased product :
mymodule_get_users_purchased_products() {
$purchased_products = &drupal_static(__FUNCTION__);
if (!isset($purchased_products)) {
global $user;
$query = db_select('commerce_order', 'corder');
$query->join('commerce_line_item', 'li', 'corder.order_id = li.order_id');
$query->join('field_data_commerce_product', 'prod', 'li.line_item_id = prod.entity_id');
$query->condition('corder.uid', $user->uid, '=')
->condition('corder.status', 'completed', '=')
->fields('prod', array('commerce_product_product_id'));
$result = $query->execute();
$purchased_products = array_unique($result->fetchCol());
}
return $purchased_products;
}
Do you know where from comes the problem?
Thanks for your help