You are here

function commerce_discount_update_7107 in Commerce Discount 7

Add fields to support a new "Shipping service upgrade" offer type.

File

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

Code

function commerce_discount_update_7107() {
  if (module_exists('commerce_shipping')) {
    $fields = field_read_fields(array(), array(
      'include_inactive' => TRUE,
    ));
    if (empty($fields['commerce_shipping_upgrade_target'])) {
      $field = array(
        'entity_types' => array(
          'commerce_discount_offer',
        ),
        'field_name' => 'commerce_shipping_upgrade_target',
        'type' => 'list_text',
        'locked' => TRUE,
        'settings' => array(
          'allowed_values' => array(),
          'allowed_values_function' => 'commerce_shipping_service_options_list',
        ),
      );
      field_create_field($field);
    }
    if (empty($fields['commerce_shipping_upgrade_source'])) {
      $field = array(
        'entity_types' => array(
          'commerce_discount_offer',
        ),
        'field_name' => 'commerce_shipping_upgrade_source',
        'type' => 'list_text',
        'locked' => TRUE,
        'settings' => array(
          'allowed_values' => array(),
          'allowed_values_function' => 'commerce_shipping_service_options_list',
        ),
      );
      field_create_field($field);
    }
    field_sync_field_status();
    field_info_cache_clear();
    $instances = field_info_instances();
    if (empty($instances['commerce_discount_offer']['shipping_upgrade']['commerce_shipping_upgrade_target'])) {
      $instance = array(
        'field_name' => 'commerce_shipping_upgrade_target',
        'entity_type' => 'commerce_discount_offer',
        'bundle' => 'shipping_upgrade',
        'label' => t('Let customers select this service'),
        'required' => TRUE,
        'widget' => array(
          'type' => 'options_select',
        ),
      );
      field_create_instance($instance);
    }
    if (empty($instances['commerce_discount_offer']['shipping_upgrade']['commerce_shipping_upgrade_source'])) {
      $instance = array(
        'field_name' => 'commerce_shipping_upgrade_source',
        'entity_type' => 'commerce_discount_offer',
        'bundle' => 'shipping_upgrade',
        'label' => t('For the same price as this service'),
        'required' => TRUE,
        'widget' => array(
          'type' => 'options_select',
        ),
      );
      field_create_instance($instance);
    }
  }
  return t('Fields added to support the new "Shipping service upgrade" offer type.');
}