I have a case where users (sellers) on my site can post files for other users (buyers) to purchase. The problem is that if a seller edits or removes a file that has previously been purchased, the file also disappears from the buyer's list of licensed files. One solution that I've thought of is to prevent sellers from editing or removing a file if it's already been purchased. Is there a way to achieve this?

Comments

bojanz’s picture

Status: Active » Fixed

Yes, implement hook_form_alter (or hook_inline_entity_form_entity_form_alter if you are using Inline Entity Form for products).
In the hook, do an EntityFieldQuery on commerce_licenses by the found product, and if any are found, set #access => FALSE or #disabled => TRUE on the commerce_file element.

So kinda like this:

// If this product already has licenses, prevent the commerc_file field from being changed.
$query = new EntityFieldQuery;
$query
        ->entityCondition('entity_type', 'commerce_license')
        ->entityCondition('bundle', 'file')
        ->propertyCondition('status', COMMERCE_LICENSE_ACTIVE)
        ->propertyCondition('product_id', $product_id)
       ->count();
$count = $query->execute();
if ($count) {
   // or use #disabled
  $form['commerce_file']['#access'] = FALSE;
}

Share your final code so we can put it in a documentation page :)

b3nji’s picture

Thank you for the code. Pardon my ignorance but how do you get the $product_id? I'm using Inline Entity form for products.

b3nji’s picture

Ok got it. The following code works for me. Thanks bojanz!

function mymodule_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
  // Change commerce_product to your entity_type
  if ($entity_form['#entity_type'] == 'commerce_product') {
    $product = $entity_form['#entity'];
    $product_id = $product->product_id;
    // If this product already has licenses, prevent the commerce_file field from being changed.
    $query = new EntityFieldQuery;
    $query
        ->entityCondition('entity_type', 'commerce_license')
        ->entityCondition('bundle', 'file')
        ->propertyCondition('status', COMMERCE_LICENSE_ACTIVE)
        ->propertyCondition('product_id', $product_id)
        ->count();
    $count = $query->execute();
      if ($count) {
      // or use #access
      $entity_form['commerce_file']['#disabled'] = TRUE;
      // optionally set a message to warn user
      $entity_form['commerce_file']['und'][0]['#description'] = t('You may not delete this file');
      }
  }
}

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

b3nji’s picture

Status: Closed (fixed) » Active

Upon further testing, the code works for Inline Entity form with single values. Do you have any suggestions for how to make it work with inline entity form with multiple values?

bojanz’s picture

The hook is invoked the same way for both widgets (single value and multiple value). So the code should be fine.
Which part is failing?

b3nji’s picture

Licenses are active for a given product however the user is still able to remove the file (commerce_file field is not being disabled).

bojanz’s picture

Yes, I understood that part, but does the hook run?
Does "if ($entity_form['#entity_type'] == 'commerce_product') {" execute?
Does it try to alter $entity_form?

Some step along the way fails.

b3nji’s picture

As far as I can tell yes. Could you kindly help me debug? I can send you site details via pm.

b3nji’s picture

Aaah sorry, I had the module disabled... *sigh*

b3nji’s picture

Status: Active » Closed (fixed)