0

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

3
  • Your code snippet is not correct. However, your general approach is right. You just need to test for the right value and hide the fields you do not want to be seen.
    – nvahalik
    Commented Nov 9, 2015 at 21:13
  • The problem comes mainly from the line concerning the line item, right ? Would you have some documentation on this matter?
    – Frouite
    Commented Nov 10, 2015 at 14:27
  • How were you able to hide the Add to Cart?
    – IamOnStage
    Commented Aug 9, 2017 at 12:35

1 Answer 1

1

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

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

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

  foreach ($build['#node']->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->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']->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->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:

enter image description here

As opposed to this:

enter image description here

3
  • Thanks nvahalik for this detailed answer, it works perfectly. A thing: it hides fields for all the users. So that it works only for products bought by the current user, this function can work (basing on the sku)? I have to adapt the $purchased_products to load the purchased products by the current user?
    – Frouite
    Commented Nov 10, 2015 at 22:14
  • You'll need to edit the code (see my edit above) so that you grab a list of purchased products.
    – nvahalik
    Commented Nov 10, 2015 at 22:18
  • It works perfectly, I had forgotten of completed the command... thanks a lot!
    – Frouite
    Commented Nov 10, 2015 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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