You are here

function commerce_option_entity_load in Commerce Product Option 7.2

Implements hook_entity_load().

TODO: Put this code in getter callback?

File

./commerce_option.module, line 459

Code

function commerce_option_entity_load($entities, $type) {
  if ($type != 'commerce_line_item') {
    return;
  }

  // Attach the commerce options for this line item to the entity.
  foreach ($entities as $id => $entity) {

    // Only for products.
    if ($entity->type != 'product') {
      continue;
    }
    $product_id = $entity->commerce_product[LANGUAGE_NONE][0]['product_id'];
    $query = db_select('commerce_option', 'co')
      ->fields('co', array(
      'option_id',
    ))
      ->condition('line_item_id', $id, '=')
      ->condition('product_id', $product_id, '=');
    $result = $query
      ->execute()
      ->fetchCol();
    if (empty($result)) {
      continue;
    }
    foreach ($result as $option_id) {
      $commerce_option = commerce_option_load($option_id);
      $options_extracted = commerce_option_get_valuables($commerce_option);
      $entity->{$commerce_option->set_id} = serialize($options_extracted);
    }
  }
}