You are here

function commerce_file_refresh_line_item in Commerce File 7

Refresh a line item with the current product license data

2 calls to commerce_file_refresh_line_item()
commerce_file_commerce_cart_order_refresh in ./commerce_file.module
Implements hook_commerce_cart_order_refresh().
commerce_file_license_issue_by_commerce_line_item in includes/commerce_file.entities.inc
Issue licenses for files in a line item

File

./commerce_file.module, line 1007
Provides integration of file licenses with Commerce

Code

function commerce_file_refresh_line_item($line_item_wrapper_dirty, $order_wrapper = NULL) {
  $refreshed =& drupal_static(__FUNCTION__, array());
  $line_item_field_name = _commerce_file_get_field_names('line_item_files');
  $line_item_has_changed = FALSE;

  // wrap line with clean wrapper
  $line_item_wrapper = _commerce_file_clean_line_item_wrapper($line_item_wrapper_dirty);
  if (empty($line_item_wrapper)) {
    return;
  }
  $line_item_id = $line_item_wrapper
    ->getIdentifier();

  // exit if refreshed already
  if (isset($refreshed[$line_item_id])) {
    return;
  }
  $refreshed[$line_item_id] = TRUE;

  // exit if no file field set up on line item
  if (empty($line_item_wrapper->{$line_item_field_name})) {
    return;
  }

  // get referenced product
  if (empty($line_item_wrapper->commerce_product)) {
    return;
  }
  $product_wrapper = $line_item_wrapper->commerce_product;

  // find all instances of commerce file field
  $product_file_instances = _commerce_file_field_info_instances('commerce_product', $product_wrapper
    ->getBundle(), COMMERCE_FILE_FIELD_TYPE);
  if (empty($product_file_instances)) {
    return;
  }

  // transfer product file fields to line item
  $all_product_file_items = array();
  foreach ($product_file_instances as $product_file_fieldname => $product_file_instance) {
    if (empty($product_wrapper->{$product_file_fieldname})) {
      continue;
    }

    // get product field items
    $product_file_instance_items = $product_wrapper->{$product_file_fieldname}
      ->value();
    if (empty($product_file_instance_items)) {
      continue;
    }

    // handle a single value field
    if (isset($product_file_instance_items['fid'])) {
      $product_file_instance_items = array(
        $product_file_instance_items,
      );
    }

    // resolve settings of all items
    foreach ($product_file_instance_items as $product_file_instance_item) {
      $all_product_file_items[] = _commerce_file_license_resolve_settings($product_file_instance_item, $product_file_instance);
    }
  }

  // set line item field to all file items found on all product fields
  $line_item_wrapper->{$line_item_field_name} = $all_product_file_items;
  _commerce_file_line_item_wrapper_save($line_item_wrapper);
  return $line_item_wrapper;
}