You are here

function commerce_discount_install_helper in Commerce Discount 7

Helper function to define and create the required fields & instances.

Ensure creation of an entityreference field on commerce discount, referencing commerce discount offer. The instance will be added for every newly created bundle.

3 calls to commerce_discount_install_helper()
commerce_discount_flush_caches in ./commerce_discount.module
Implements hook_flush_caches().
commerce_discount_install in ./commerce_discount.install
Implements hook_install().
commerce_discount_update_7110 in ./commerce_discount.install
Enable and update usage and date sub-modules into discount core.

File

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

Code

function commerce_discount_install_helper() {
  $fields = field_read_fields(array(), array(
    'include_inactive' => TRUE,
  ));
  $field_types = field_info_field_types();

  // Clear field info cache, so entity reference, inline_conditions and
  // commerce_product_reference field types can be used.
  if (!isset($field_types['entityreference'], $field_types['inline_conditions'], $field_types['commerce_product_reference'], $field_types['datestamp'])) {
    field_info_cache_clear();

    // Overwrites variable, because during installation module after clears the
    // field info cache the field type 'entityreference' not containing
    // in variables. Under this the field, use this field type is not created.
    $field_types = field_info_field_types();
  }

  // Create the discount offer entity reference field for use on all discounts.
  if (empty($fields['commerce_discount_offer']) && isset($field_types['entityreference'])) {
    $field = array(
      'entity_types' => array(
        'commerce_discount',
      ),
      'settings' => array(
        'handler' => 'base',
        'target_type' => 'commerce_discount_offer',
        'handler_settings' => array(
          // Default to fixed_amount, and others will be selectable from the
          // IEF widget.
          'target_bundles' => array(
            'fixed_amount',
          ),
        ),
      ),
      'field_name' => 'commerce_discount_offer',
      'type' => 'entityreference',
      'locked' => TRUE,
    );
    field_create_field($field);
  }

  // Create the discount compatibility strategy field for use on all discounts.
  if (empty($fields['commerce_compatibility_strategy'])) {
    $field = array(
      'type' => 'list_text',
      'field_name' => 'commerce_compatibility_strategy',
      'locked' => TRUE,
      'settings' => array(
        'allowed_values' => array(),
        'allowed_values_function' => 'commerce_discount_compatibility_strategies',
      ),
    );
    field_create_field($field);
  }

  // Create the selected discounts field for use on all discounts.
  if (empty($fields['commerce_compatibility_selection'])) {
    $field = array(
      'type' => 'entityreference',
      'field_name' => 'commerce_compatibility_selection',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'locked' => TRUE,
      'settings' => array(
        'target_type' => 'commerce_discount',
        'handler' => 'base',
        'handler_settings' => array(
          'sort' => array(
            'type' => 'property',
            'property' => 'label',
            'direction' => 'ASC',
          ),
        ),
      ),
    );
    field_create_field($field);
  }
  if (empty($fields['commerce_fixed_amount'])) {

    // Add price field to the commerce discount offer "fixed_amount" bundle.
    $field = array(
      'entity_types' => array(
        'commerce_discount_offer',
      ),
      'field_name' => 'commerce_fixed_amount',
      'type' => 'commerce_price',
      'locked' => TRUE,
    );
    field_create_field($field);
  }
  if (empty($fields['commerce_percentage'])) {

    // Add decimal field to the commerce discount offer "percentage" bundle.
    $field = array(
      'entity_types' => array(
        'commerce_discount_offer',
      ),
      'field_name' => 'commerce_percentage',
      'type' => 'number_decimal',
      'locked' => TRUE,
    );
    field_create_field($field);
  }
  if (empty($fields['commerce_discounts'])) {
    $field = array(
      'entity_types' => array(
        'commerce_order',
      ),
      'settings' => array(
        'handler' => 'base',
        'target_type' => 'commerce_discount',
        'handler_settings' => array(
          'target_bundles' => array(),
        ),
      ),
      'field_name' => 'commerce_discounts',
      'type' => 'entityreference',
      'locked' => FALSE,
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    );
    field_create_field($field);
  }
  if (empty($fields['inline_conditions']) && isset($field_types['inline_conditions'])) {
    $field = array(
      'entity_types' => array(
        'commerce_discount',
      ),
      'field_name' => 'inline_conditions',
      'type' => 'inline_conditions',
      'instance_settings' => array(
        'entity_type' => 'commerce_order',
      ),
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    );
    field_create_field($field);
  }
  if (empty($fields['commerce_discount_date'])) {
    $field = array(
      'entity_types' => array(
        'commerce_discount',
      ),
      'settings' => array(
        'granularity' => array(
          'month' => 'month',
          'day' => 'day',
          'year' => 'year',
        ),
        'tz_handling' => 'none',
        'timezone_db' => '',
        'todate' => 'optional',
        'handler' => 'base',
        'target_type' => 'commerce_discount',
        'handler_settings' => array(
          'target_bundles' => array(),
        ),
      ),
      'field_name' => 'commerce_discount_date',
      'type' => 'datestamp',
      'locked' => TRUE,
    );
    field_create_field($field);
  }
  if (empty($fields['discount_usage_per_person'])) {

    // Create entity reference field.
    $field = array(
      'entity_types' => array(
        'commerce_discount',
      ),
      'field_name' => 'discount_usage_per_person',
      'type' => 'number_integer',
      'locked' => TRUE,
    );
    field_create_field($field);
  }

  // Discount usage.
  if (empty($fields['discount_usage_limit'])) {

    // Create entity reference field.
    $field = array(
      'entity_types' => array(
        'commerce_discount',
      ),
      'field_name' => 'discount_usage_limit',
      'type' => 'number_integer',
      'locked' => TRUE,
    );
    field_create_field($field);
  }

  // Create fields for the shipping related offer types.
  if (module_exists('commerce_shipping')) {

    // Creating the shipping service text field and checkbox for the "Free
    // shipping" offer.
    if (empty($fields['commerce_free_shipping'])) {
      $field = array(
        'entity_types' => array(
          'commerce_discount_offer',
        ),
        'field_name' => 'commerce_free_shipping',
        'type' => 'text',
        'locked' => TRUE,
      );
      field_create_field($field);
    }
    if (empty($fields['commerce_free_shipping_strategy'])) {
      $field = array(
        'type' => 'list_text',
        'field_name' => 'commerce_free_shipping_strategy',
        'locked' => TRUE,
        'settings' => array(
          'allowed_values' => array(),
          'allowed_values_function' => 'commerce_discount_free_shipping_strategies',
        ),
      );
      field_create_field($field);
    }

    // Create the percentage off and shipping service text fields for the "% off
    // of shipping" offer.
    if (empty($fields['commerce_percent_off_shipping'])) {
      $field = array(
        'entity_types' => array(
          'commerce_discount_offer',
        ),
        'field_name' => 'commerce_percent_off_shipping',
        'type' => 'number_decimal',
        'locked' => TRUE,
      );
      field_create_field($field);
    }
    if (empty($fields['commerce_percent_off_ship_serv'])) {
      $field = array(
        'entity_types' => array(
          'commerce_discount_offer',
        ),
        'field_name' => 'commerce_percent_off_ship_serv',
        'type' => 'text',
        'locked' => TRUE,
      );
      field_create_field($field);
    }

    // Create the target and source shipping service text fields for the
    // "Shipping service upgrade" offer.
    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);
    }
  }

  // Creating field for free bonus products offer type.
  if (empty($fields['commerce_free_products']) && isset($field_types['commerce_product_reference'])) {
    $field = array(
      'entity_types' => array(
        'commerce_discount_offer',
      ),
      'field_name' => 'commerce_free_products',
      'type' => 'commerce_product_reference',
      'locked' => TRUE,
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    );
    field_create_field($field);
  }

  // Make any existing-but-inactive fields active, and clear the cache to get
  // instances for those fields.
  field_sync_field_status();
  field_info_cache_clear();

  // Create instances for above fields.
  $instances = field_info_instances();
  foreach (commerce_discount_types() as $type => $value) {
    if (empty($instances['commerce_discount'][$type]['commerce_discount_offer'])) {
      $instance = array(
        'field_name' => 'commerce_discount_offer',
        'entity_type' => 'commerce_discount',
        'bundle' => $type,
        'label' => t('Discount offer reference'),
        'required' => TRUE,
        'widget' => array(
          'module' => 'inline_entity_form',
          'type' => 'inline_entity_form_single',
          'weight' => -12,
        ),
      );
      field_create_instance($instance);
    }
    if (empty($instances['commerce_discount'][$type]['commerce_compatibility_strategy'])) {
      $instance = array(
        'field_name' => 'commerce_compatibility_strategy',
        'label' => t('Compatibility with other discounts'),
        'entity_type' => 'commerce_discount',
        'bundle' => $type,
        'required' => TRUE,
        'widget' => array(
          'weight' => -10,
          'type' => 'options_buttons',
          'module' => 'options',
        ),
        'default_value' => array(
          0 => array(
            'value' => 'any',
          ),
        ),
      );
      field_create_instance($instance);
    }
    if (empty($instances['commerce_discount'][$type]['commerce_compatibility_selection'])) {
      $instance = array(
        'field_name' => 'commerce_compatibility_selection',
        'label' => t('Selected discounts'),
        'entity_type' => 'commerce_discount',
        'bundle' => $type,
        'required' => FALSE,
        'widget' => array(
          'weight' => -9,
          'type' => 'entityreference_autocomplete',
          'module' => 'entityreference',
        ),
      );
      field_create_instance($instance);
    }
    if (empty($instances['commerce_discount'][$type]['commerce_discount_date'])) {
      $instance = array(
        'field_name' => 'commerce_discount_date',
        'entity_type' => 'commerce_discount',
        'bundle' => $type,
        'label' => t('Discount dates'),
        'widget' => array(
          'module' => 'date',
          'type' => 'date_popup',
          'weight' => -11,
          'settings' => array(
            'no_fieldset' => TRUE,
          ),
        ),
        'settings' => array(
          'default_value' => 'blank',
          'default_value2' => 'blank',
        ),
      );
      field_create_instance($instance);
    }
  }
  if (empty($instances['commerce_discount_offer']['fixed_amount']['commerce_fixed_amount'])) {
    $instance = array(
      'field_name' => 'commerce_fixed_amount',
      'entity_type' => 'commerce_discount_offer',
      'bundle' => 'fixed_amount',
      'label' => t('Fixed amount'),
      'required' => TRUE,
    );
    field_create_instance($instance);
  }
  if (empty($instances['commerce_discount_offer']['percentage']['commerce_percentage'])) {
    $instance = array(
      'field_name' => 'commerce_percentage',
      'entity_type' => 'commerce_discount_offer',
      'bundle' => 'percentage',
      'label' => t('Percentage'),
      'settings' => array(
        'suffix' => '%',
      ),
      'required' => TRUE,
    );
    field_create_instance($instance);
  }

  // Add discount field to all commerce_order bundles.
  $order_entity_info = entity_get_info('commerce_order');
  foreach (array_keys($order_entity_info['bundles']) as $bundle) {
    if (empty($instances['commerce_order'][$bundle]['commerce_discounts'])) {
      $instance = array(
        'field_name' => 'commerce_discounts',
        'entity_type' => 'commerce_order',
        'bundle' => $bundle,
        'label' => t('Discount reference'),
      );
      field_create_instance($instance);
    }
  }
  foreach (commerce_discount_types() as $type => $value) {
    if (empty($instances['commerce_discount'][$type]['inline_conditions']) && isset($field_types['inline_conditions'])) {
      $instance = array(
        'field_name' => 'inline_conditions',
        'entity_type' => 'commerce_discount',
        'bundle' => $type,
        'label' => t('!type_label conditions', array(
          '!type_label' => $value['label'],
        )),
        'required' => FALSE,
        'settings' => array(
          'entity_type' => $value['entity type'],
        ),
        'widget' => array(
          'type' => 'inline_conditions',
          'weight' => -13,
        ),
      );
      field_create_instance($instance);
    }
  }
  foreach (commerce_discount_types() as $type => $info) {
    if (empty($instances['commerce_discount'][$type]['discount_usage_per_person'])) {
      $instance = array(
        'field_name' => 'discount_usage_per_person',
        'entity_type' => 'commerce_discount',
        'bundle' => $type,
        'label' => t('Maximum usage per customer'),
        'description' => t('Enter the maximum number of times a specific person (as identified by email) may use this discount. Leave blank for unlimited.'),
        'required' => FALSE,
        'widget' => array(
          'weight' => 100,
        ),
        'settings' => array(
          'min' => 0,
        ),
      );
      field_create_instance($instance);
    }
    if (empty($instances['commerce_discount'][$type]['discount_usage_limit'])) {
      $instance = array(
        'field_name' => 'discount_usage_limit',
        'entity_type' => 'commerce_discount',
        'bundle' => $type,
        'label' => t('Maximum overall usage'),
        'description' => t('Enter the maximum number of times this discount may be used on the site, by anyone. Leave blank for unlimited.'),
        'required' => FALSE,
        'widget' => array(
          'weight' => 100,
        ),
        'settings' => array(
          'min' => 0,
        ),
      );
      field_create_instance($instance);
    }
  }

  // Create field instances for the shipping related offer types.
  if (module_exists('commerce_shipping')) {

    // Add the shipping service text field to the "Free shipping" offer.
    if (empty($instances['commerce_discount_offer']['free_shipping']['commerce_free_shipping'])) {
      $instance = array(
        'field_name' => 'commerce_free_shipping',
        'entity_type' => 'commerce_discount_offer',
        'bundle' => 'free_shipping',
        'label' => t('Shipping service'),
        'required' => TRUE,
        'widget' => array(
          'type' => 'options_select',
        ),
      );
      field_create_instance($instance);
    }
    if (empty($instances['commerce_discount_offer']['free_shipping']['commerce_free_shipping_strategy'])) {
      $instance = array(
        'field_name' => 'commerce_free_shipping_strategy',
        'entity_type' => 'commerce_discount_offer',
        'bundle' => 'free_shipping',
        'label' => t('Free shipping discount strategy'),
        'required' => TRUE,
        'widget' => array(
          'type' => 'options_buttons',
          'module' => 'options',
        ),
        'default_value' => array(
          0 => array(
            'value' => 'only_selected',
          ),
        ),
      );
      field_create_instance($instance);
    }

    // Add the percentage off and shipping service text fields to the "% off of
    // shipping" offer.
    if (empty($instances['commerce_discount_offer']['percent_off_shipping']['commerce_percent_off_shipping'])) {
      $instance = array(
        'field_name' => 'commerce_percent_off_shipping',
        'entity_type' => 'commerce_discount_offer',
        'bundle' => 'percent_off_shipping',
        'label' => t('Percentage off of shipping'),
        'settings' => array(
          'suffix' => '%',
        ),
        'required' => TRUE,
      );
      field_create_instance($instance);
    }
    if (empty($instances['commerce_discount_offer']['percent_off_shipping']['commerce_percent_off_ship_serv'])) {
      $instance = array(
        'field_name' => 'commerce_percent_off_ship_serv',
        'entity_type' => 'commerce_discount_offer',
        'bundle' => 'percent_off_shipping',
        'label' => t('Shipping service to take % off shipping'),
        'widget' => array(
          'type' => 'options_select',
        ),
      );
      field_create_instance($instance);
    }

    // Add the target and source shipping service text fields to the "Shipping
    // service upgrade" offer.
    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);
    }
  }
  if (empty($instances['commerce_discount_offer']['free_products']['commerce_free_products'])) {
    $instance = array(
      'field_name' => 'commerce_free_products',
      'entity_type' => 'commerce_discount_offer',
      'bundle' => 'free_products',
      'label' => t('Select bonus products'),
      'description' => t('Enter a comma-separated list of SKUs. Each product is added to customer order with a quantity of 1.'),
      'required' => TRUE,
      'widget' => array(
        'type' => 'commerce_product_reference_autocomplete',
      ),
    );
    field_create_instance($instance);
  }

  // Update properties of product_discount bundle.
  commerce_line_item_configure_line_item_fields(array(
    'commerce_discount',
  ));
}