Skip to content

Instantly share code, notes, and snippets.

@yanniboi
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save yanniboi/11370169 to your computer and use it in GitHub Desktop.

Select an option

Save yanniboi/11370169 to your computer and use it in GitHub Desktop.
Generate commerce file license
<?php
$fid = ''; // ID of the file you want to license.
$uid = ''; // ID of the user receiving the license.
$line_item_id = ''; // Optional - Id of line item in an order owned by the user.
// Get the entities.
$user = user_load($uid);
$file = file_load($fid);
$values = array(
'uid' => $user->uid,
'status' => 'active',
);
$license_new = commerce_file_license_create($values);
// Attach the file to the license.
$license_new->commerce_file_license_file[LANGUAGE_NONE][] = array(
'fid' => $file->fid,
'data' => array(
'duration' => 0,
'download_limit' => 0,
'access_limit' => 0,
),
);
// Optional if line item exists.
$license_new->commerce_file_license_line_items[LANGUAGE_NONE][] = array(
'line_item_id' => $line_item_id,
);
// Save.
commerce_file_license_save($license_new);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment