You are here

function _commerce_avatax_configure_tax_code_field in Drupal Commerce Connector for AvaTax 7.5

Configure the "tax code" field.

1 call to _commerce_avatax_configure_tax_code_field()
commerce_avatax_install_helper in ./commerce_avatax.install
Helper function to define and create the required fields & instances.

File

./commerce_avatax.install, line 104
Installation functions for Commerce Avatax module.

Code

function _commerce_avatax_configure_tax_code_field() {
  $field_name = COMMERCE_AVATAX_TAX_CODE_FIELD;
  $product_types = variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'product_types', array());

  // By default add the commerce_avatax_tax_code field to all product bundles.
  if (empty($product_types)) {
    $product_types = array_keys(commerce_product_types());
  }
  else {
    $all_bundles = array_keys(commerce_product_types());
    $types_to_remove = array_diff($all_bundles, $product_types);
    foreach ($types_to_remove as $bundle) {
      if ($instance = field_info_instance('commerce_product', $field_name, $bundle)) {
        field_delete_instance($instance);
      }
    }
  }

  // Create the field type if it doesn't exist.
  $field = field_info_field($field_name);
  if (!$field) {
    $field = field_create_field(array(
      'cardinality' => 1,
      'field_name' => $field_name,
      'settings' => array(
        'allowed_values' => array(
          0 => array(
            'vocabulary' => 'avatax_codes',
          ),
        ),
      ),
      'type' => 'taxonomy_term_reference',
    ));
  }

  // Create the field instance for the selected product types.
  foreach ($product_types as $type) {
    $instance = field_info_instance('commerce_product', $field_name, $type);
    if (!$instance) {
      field_create_instance(array(
        'bundle' => $type,
        // @todo: set default_value
        'default_value' => NULL,
        'display' => array(
          'default' => array(
            'type' => 'hidden',
          ),
          'teaser' => array(
            'type' => 'hidden',
          ),
        ),
        'entity_type' => 'commerce_product',
        'field_name' => $field_name,
        'label' => 'AvaTax code',
        'required' => TRUE,
        'widget' => array(
          'module' => 'taxonomy',
          'settings' => array(
            'autocomplete_path' => 'taxonomy/autocomplete',
            'size' => 60,
          ),
          'type' => 'taxonomy_autocomplete',
          'weight' => -4,
        ),
      ));
    }
  }
}