You are here

function commerce_discount_update_7110 in Commerce Discount 7

Enable and update usage and date sub-modules into discount core.

File

./commerce_discount.install, line 1092
Install, update, and uninstall functions for the commerce discount module.

Code

function commerce_discount_update_7110() {

  // Fail early if the Date module is not present.
  if (!module_exists('date')) {
    throw new DrupalUpdateException('The Date module is required.');
  }
  commerce_discount_install_helper();
  $fields = field_read_fields(array(), array(
    'include_inactive' => TRUE,
  ));
  if (!db_table_exists('commerce_discount_usage')) {
    db_create_table('commerce_discount_usage', drupal_get_schema_unprocessed('commerce_discount', 'commerce_discount_usage'));
  }

  // Migrate from commerce_discount_max_uses to discount_usage_limit.
  foreach (entity_load('commerce_discount') as $discount) {
    $wrapper = entity_metadata_wrapper('commerce_discount', $discount);
    if ((!isset($wrapper->discount_usage_limit) || $wrapper->discount_usage_limit
      ->value() == FALSE) && isset($wrapper->commerce_discount_max_uses) && $wrapper->commerce_discount_max_uses
      ->value()) {
      $wrapper->discount_usage_limit = $wrapper->commerce_discount_max_uses
        ->value();
      entity_save('commerce_discount', $discount);
    }
  }
  if (!empty($fields['commerce_discount_max_uses'])) {
    field_delete_field('commerce_discount_max_uses');
  }
  $disable_modules = array(
    'commerce_discount_usage',
    'commerce_discount_date',
  );
  db_update('system')
    ->fields(array(
    'status' => 0,
  ))
    ->condition('name', $disable_modules)
    ->condition('type', 'module')
    ->execute();
  field_info_cache_clear();
  return t('Discount date and usage sub-modules are now in core.');
}