You are here

function commerce_coupon_type_configure in Commerce Coupon 7

Ensures a base price field is present on a coupon type bundle.

Parameters

$bundle: The coupon type string to configure.

$reset:

1 call to commerce_coupon_type_configure()
commerce_coupon_type_save in ./commerce_coupon.module
Saves a coupon type to the database.

File

./commerce_coupon.module, line 424
Coupon System for Drupal Commerce.

Code

function commerce_coupon_type_configure($bundle, $reset) {
  $entity_type = 'commerce_coupon';

  // Look for or add the specified coupon code
  $field_name = 'commerce_coupon_code';
  $field = field_info_field($field_name);
  $instance = field_read_instance($entity_type, $field_name, $bundle);
  if (empty($field) || $reset) {
    $field_data = array(
      'field_name' => $field_name,
      'type' => 'text',
      'cardinality' => 1,
      'entity_types' => array(
        $entity_type,
      ),
      'translatable' => FALSE,
      'locked' => TRUE,
      'settings' => array(),
    );
    if (empty($field)) {
      field_create_field($field_data);
    }
    else {
      field_update_field($field_data);
    }
  }
  if (empty($instance) || $reset) {
    $instance_data = array(
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'label' => 'Coupon Code',
      'required' => FALSE,
      'settings' => array(),
      'display' => array(),
    );
    if (empty($instance)) {
      field_create_instance($instance_data);
    }
    else {
      field_update_instance($instance_data);
    }
  }

  // Look for or add the number of uses.
  $field_name = 'commerce_coupon_number_of_uses';
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle);
  if (empty($field) || $reset) {
    $field_data = array(
      'field_name' => $field_name,
      'type' => 'number_integer',
      'cardinality' => 1,
      'entity_types' => array(
        $entity_type,
      ),
      'translatable' => FALSE,
      'locked' => FALSE,
      'settings' => array(),
    );
    if (empty($field)) {
      field_create_field($field_data);
    }
    else {
      field_update_field($field_data);
    }
  }
  if (empty($instance) || $reset) {
    $instance_data = array(
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'label' => 'Maximum number of Uses',
      'description' => 'Number of times that coupon code can be used by any customer on the site, before it is set to inactive',
      'required' => FALSE,
      'display' => array(),
      'settings' => array(
        'min' => '0',
      ),
      'default_value' => array(
        0 => array(
          'value' => 1,
        ),
      ),
    );
    if (empty($instance)) {
      field_create_instance($instance_data);
    }
    else {
      field_update_instance($instance_data);
    }
  }

  // Allow other modules to configure coupon types:
  module_invoke_all('commerce_coupon_type_configure', $bundle, $reset);
}